Module: Prototype::Prototypical::ClassMethods

Defined in:
lib/alib-0.5.1/prototype.rb,
lib/alib-0.5.1/prototype-0.3.0.rb

Instance Method Summary collapse

Instance Method Details

#__prototype_eval__(&b) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/alib-0.5.1/prototype.rb', line 48

def __prototype_eval__ &b
  tid = Thread.current.object_id.abs
  src, dst = "method_missing", "__method_missing_#{ tid }__"
  aliased = false

  __prototype_singleton_class__{
    unless respond_to? dst 
      alias_method dst, src
      aliased = true 
    end

    def method_missing m, *a, &b
      __prototype_barewords_can_set_and_get_ivars_and_define_methods__ m, *a, &b
    end

    def __prototype_barewords_can_set_and_get_ivars_and_define_methods__ m, *a, &b
      if b
        define_method m, *a, &b
      else
        ivar = "@#{ m }"
        if a.size == 0
          defined?(ivar) ? instance_variable_get(ivar) : super
        elsif a.size == 1
          instance_variable_set ivar, a.shift
        else
          __method_missing__(m, *a, &b) 
        end
      end
    end
  }

  module_eval &b

ensure
  __prototype_singleton_class__{ alias_method src, dst } if aliased
end

#__prototype_prototype__(&b) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/alib-0.5.1/prototype.rb', line 94

def __prototype_prototype__ &b
  return unless b

  __prototype_eval__ &b

  table =
    instance_variables.inject({}) do |t,ivar|
      value = 
        instance_eval do
          begin
            instance_variable_get ivar
          ensure
            remove_instance_variable ivar
          end
        end
      t.update ivar => value
    end

  __prototype_table__.update table
end

#__prototype_singleton_class__(&b) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/alib-0.5.1/prototype.rb', line 85

def __prototype_singleton_class__ &b
  sc =
    class << self
      self
    end
  sc.module_eval &b if b
  sc
end

#__prototype_table__Object

uses closure/init to avoid instance-var



33
34
35
36
37
38
39
# File 'lib/alib-0.5.1/prototype.rb', line 33

def __prototype_table__  # uses closure/init to avoid instance-var 
  __prototype_singleton_class__{
    table = {}
    define_method('__prototype_table__'){ table }
  }
  __prototype_table__    # does not recurse!
end

#__prototype_table_inject__(other) ⇒ Object



41
42
43
44
45
46
# File 'lib/alib-0.5.1/prototype.rb', line 41

def __prototype_table_inject__ other
  __prototype_table__.each do |ivar, value|
    other.instance_variable_set ivar, value
  end
  __prototype_table__.clear
end