Module: Thingiverse::DynamicAttributes

Included in:
Categories, Files, Images, Tags, Things, Users
Defined in:
lib/thingiverse/dynamic_attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/thingiverse/dynamic_attributes.rb', line 16

def method_missing(method_sym, *arguments, &block)
  method = (method_sym.instance_of? Symbol) ? method_sym.to_s : method_sym.clone
  
  if method.end_with? "="
    method.chomp! "="
    @attributes[method] = arguments[0]
    add_attribute(method, arguments[0])
  else
    super(method_sym, *arguments, &block)
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/thingiverse/dynamic_attributes.rb', line 7

def attributes
  @attributes
end

Instance Method Details

#add_attribute(name, value) ⇒ Object



28
29
30
31
# File 'lib/thingiverse/dynamic_attributes.rb', line 28

def add_attribute(name, value)
  eigenclass.class_eval{ attr_reader name.to_sym }
  instance_variable_set("@#{name}", value)
end

#eigenclassObject



3
4
5
# File 'lib/thingiverse/dynamic_attributes.rb', line 3

def eigenclass
  class << self; self; end
end

#initialize(attributes = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/thingiverse/dynamic_attributes.rb', line 9

def initialize(attributes={})
  @attributes = attributes
  @attributes.each do |k,v|
    add_attribute(k, v)
  end
end