Module: Base4R::ItemAttributeMethods::ClassMethods

Defined in:
lib/item.rb

Instance Method Summary collapse

Instance Method Details

#attribute_definition(name) ⇒ Object

return the attribute definition for a specific attribute


60
61
62
# File 'lib/item.rb', line 60

def attribute_definition(name)#:nodoc:
  attribute_definitions[name.to_sym]
end

#attribute_definitionsObject

return all actual and inherited definitions


52
53
54
55
56
57
# File 'lib/item.rb', line 52

def attribute_definitions#:nodoc
  @all_attribute_definitions ||=
    (superclass.respond_to?(:attribute_definitions) ?
      superclass.attribute_definitions :
      {}).merge(@attribute_definitions||{})
end

#define_attributes(options) ⇒ Object

set the attribute definitions and define setters and getters


28
29
30
31
32
# File 'lib/item.rb', line 28

def define_attributes(options)
  @attribute_definitions ||= {}
  @attribute_definitions.update(options)
  options.each {|k,v| define_setter_method(k, v)}
end

#define_child_accessor(parent, child) ⇒ Object

Define setter and getter methods. The example will define author_name and author_name=

define_child_method :author, :name

44
45
46
47
48
49
# File 'lib/item.rb', line 44

def define_child_accessor(parent, child)
  class_eval "    def \#{parent}_\#{child};     child_value(:\#{parent}, :\#{child});        end\n    def \#{parent}_\#{child}=(v); set_child_value(:\#{parent}, :\#{child}, v); end\n  EOF\nend\n"

#define_setter_method(method, options = {}) ⇒ Object

define setter methods on the object


35
36
37
38
39
40
# File 'lib/item.rb', line 35

def define_setter_method(method, options={})
  class_eval "    def set_\#{method}(value); add_attribute(:\#{method}, value); end\n    alias_method \"\#{method}=\", \"set_\#{method}\"\n  EOS\nend\n", __FILE__, __LINE__