Module: Base4R::ChildAttributeMethods

Included in:
Attribute
Defined in:
lib/attribute.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



64
65
66
# File 'lib/attribute.rb', line 64

def self.included(base)
  base.send :extend,  ClassMethods
end

Instance Method Details

#add_child(*args) ⇒ Object

add a child. The accepted arguments are the same as initializing a new Attribute

shipping_attribute.add_child(:region, 'WA')
shipping_attribute.add_child(:region, :value => 'WA', :namespace => :g)


102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/attribute.rb', line 102

def add_child(*args)
  #parse out the options specified here into keys
  parsed_options = options_from_args(args, :skip_defaults => true)

  child_name = parsed_options[:name].to_sym

  # merge the default options with the extra options with the namespace, name, type options
  child_options = (self.class.default_child_options[child_name]||{}).merge(parsed_options)
  klass = child_options.delete(:klass)||BareAttribute

  children_map[child_name] = klass.new(child_options)
end

#add_child_attribute(attribute) ⇒ Object

Add a custom prebuilt child attribute - an item of class Attribute

shipping_attribute.add_custom_child(BareAttribute.new(:region, 'WA'))


95
96
97
# File 'lib/attribute.rb', line 95

def add_child_attribute(attribute)
  children_map[attribute.name.to_sym] = attribute
end

#child(child_name) ⇒ Object

get a specific child



116
# File 'lib/attribute.rb', line 116

def child(child_name); children_map[child_name.to_sym]; end

#childrenObject

All the children Attribute objects



121
# File 'lib/attribute.rb', line 121

def children;       children_map.values; end

#children_mapObject

a map of all the children names to Attribute class values



119
# File 'lib/attribute.rb', line 119

def children_map; @children_map||={}; end

#children_namesObject

All the names of the children



123
# File 'lib/attribute.rb', line 123

def children_names; children_map.keys;   end