Commits

Michele Bini committed 3037cda

reflective coffee objects experiment

Comments (0)

Files changed (7)

-node_modules/
+node_modules/*
+!node_modules/*.js
+!node_modules/*.coffee

node_modules/lodash-underscore.js

+lodash/dist/lodash.underscore.min.js

pensive/coffee-function.coffee

+{eval: coffeeEval} = require 'coffee-script'
+
+exports.coffeeFunction =
+  (x) -> do (r = coffeeEval x) ->
+    r.coffeeSource = x
+    r
+.

pensive/reflective-coffee-object.coffee

+{coffeeFunction} = require './coffee-function'
+
+Reflective = exports.Reflective = class
+  create: (methods) ->
+    o = new Reflective;
+    for n,c of methods
+      o[n] = coffeeFunction c
+    o
+
+  setMethod: (n, c) ->
+    this[n] = coffeeFunction c
+    this
+
+  getMethod: (n) ->
+    do (x = this[n]) ->
+      x? and x.coffeeSource
+
+  delMethod: (n) ->
+    delete this[n]
+
+  getDullSource: () ->
+    ["class\n"].concat(
+       for x,y of this when (c = y.coffeeSource)?
+         "  " + x + ":\n" + (c.replace /^/g, "    ") + "\n"
+    ).join("")
+
+  getSource: () ->
+    [
+      """
+      (require "reflective-coffee-object").Reflective.create
+      """
+    ].concat(
+       for n,f of this when (c = f.coffeeSource)?
+         "  " + n + ": \"" + (c.replace /"/g, "\\\"") + "\"" # "
+    ).concat([""]).join("\n")
+
+Reflective.create = Reflective.prototype.create

pensive/self-printing-script.coffee

+{Reflective} = require "reflective-coffee-object"
+
+SelfPrinting = Reflective.create
+  selfPrintScript:
+    "-> console.log @getSource() + \".selfPrintScript()\""
+
+SelfPrinting.selfPrintScript()

reflective-html-element.coffee

-{_} = require "lodash-underscore"
-
-