Class: Prototype

Inherits:
Object show all
Defined in:
lib/more/facets/prototype.rb

Overview

Prototype class

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Prototype

New prototype object.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/more/facets/prototype.rb', line 17

def initialize(&block)
  @traits = []

  instance_eval(&block)

  h = {}
  iv = instance_variables
  iv.each { |k| h[k[1..-1].to_sym] = instance_eval{ instance_variable_get(k) } }
  meta.class_eval do
    h.each do |k,v|
      case v
      when Proc
        #define_method(k){ |*args| v[*args] }
        attr_reader k
      else
        attr_accessor k
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a, &b) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/more/facets/prototype.rb', line 59

def method_missing(s, *a, &b)
  if trait = traits.find{ |t| t.method_defined?(s) }
    trait.send(s,*a,&b)
  else
    super
  end
end

Instance Method Details

#fn(&blk) ⇒ Object



38
39
40
# File 'lib/more/facets/prototype.rb', line 38

def fn(&blk)
  proc(&blk)
end

#metaObject



47
48
49
# File 'lib/more/facets/prototype.rb', line 47

def meta
 (class << self; self; end)
end

#new(o = nil) ⇒ Object



42
43
44
45
# File 'lib/more/facets/prototype.rb', line 42

def new(o=nil)
  return o.clone if o
  return clone
end

#trait(obj) ⇒ Object



55
56
57
# File 'lib/more/facets/prototype.rb', line 55

def trait(obj)
  traits << obj.new
end

#traitsObject



51
52
53
# File 'lib/more/facets/prototype.rb', line 51

def traits
  @traits
end