Module: Doodle::Factory

Defined in:
lib/doodle.rb

Constant Summary collapse

RX_IDENTIFIER =
/^[A-Za-z_][A-Za-z_0-9]+\??$/

Class Method Summary collapse

Class Method Details

.factory(konst) ⇒ Object

create a factory function in appropriate module for the specified class



1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
# File 'lib/doodle.rb', line 1148

def self.factory(konst)
  #p [:factory, :ancestors, konst, konst.ancestors]
  #p [:factory, :lookup, Module.nesting]
  name = konst.to_s
  #p [:factory, :name, name]
  anon_class = false
  if name =~ /#<Class:0x[a-fA-F0-9]+>::/
    #p [:factory_anon_class, name]
    anon_class = true
  end
  names = name.split(/::/)
  name = names.pop
  # TODO: the code below is almost the same - refactor
  #p [:factory, :names, names, name]
  if names.empty? && !anon_class
    #p [:factory, :top_level_class]
    # top level class - should be available to all
    parent_class = Object
    method_defined = begin
                       method(name)
                       true
                     rescue Object
                       false
                     end

    if name =~ Factory::RX_IDENTIFIER && !method_defined && !parent_class.respond_to?(name) && !eval("respond_to?(:#{name})", TOPLEVEL_BINDING)
      eval("def #{ name }(*args, &block); ::#{name}.new(*args, &block); end", ::TOPLEVEL_BINDING, __FILE__, __LINE__)
    end
  else
    #p [:factory, :other_level_class]
    parent_class = Object
    if !anon_class
      parent_class = names.inject(parent_class) {|c, n| c.const_get(n)}
      #p [:factory, :parent_class, parent_class]
      if name =~ Factory::RX_IDENTIFIER && !parent_class.respond_to?(name)
        parent_class.module_eval("def self.#{name}(*args, &block); #{name}.new(*args, &block); end", __FILE__, __LINE__)
      end
    else
      # NOTE: ruby 1.9.1 specific
      parent_class_name = names.join('::')
      #p [:factory, :parent_class_name, parent_class_name]
      #p [:parent_class_name, parent_class_name]
      # FIXME: this is truly horrible...
      hex_object_id = parent_class_name.match(/:(0x[a-zA-Z0-9]+)/)[1]
      oid = hex_object_id.to_i(16) >> 1
#             p [:object_id, oid, hex_object_id, hex_object_id.to_i(16) >> 1]
      parent_class = ObjectSpace._id2ref(oid)

      #p [:parent_object_id, parent_class.object_id, names, parent_class, parent_class_name, parent_class.name]
#             p [:names, :oid, "%x" % (oid << 1), :konst, konst, :pc, parent_class, :names, names, :self, self]
      if name =~ Factory::RX_IDENTIFIER && !parent_class.respond_to?(name)
        #p [:context, context]
        parent_class.module_eval("def #{name}(*args, &block); #{name}.new(*args, &block); end", __FILE__, __LINE__)
      end
    end
    # TODO: check how many times this is being called
  end
end

.included(other) ⇒ Object

inherit the factory function capability



1208
1209
1210
1211
1212
1213
# File 'lib/doodle.rb', line 1208

def self.included(other)
  #p [:included, other]
  super
  # make +factory+ method available
  factory other
end