Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Factory
- include Enumerable
- class << self
- def new(*attrs, &block)
- class_name = attrs.shift if attrs[0].is_a?(String)
- my_struct_class = Class.new(self) do
- def initialize(*attrs, &block)
- attr_accessor(*attrs)
- define_method :initialize do |*values|
- values.each_with_index { |value, i| send("#{attrs[i]}=", value) }
- end
- define_method :[] do |attribute|
- if attribute.is_a? Numeric
- send("#{attributes[attribute]}")
- else
- send(attribute)
- end
- end
- class_eval(&block) if block_given?
- # redefinition of the method new, so that the heresy did not happen
- # struct_class is a subclass of Factory , so we had to redefine method :new
- my_struct_class.define_singleton_method(:new, Object.method(:new))
- class_name ? const_set(class_name.to_s, my_struct_class) : my_struct_class
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement