Module: Base4R::ItemAttributeMethods::InstanceMethods

Defined in:
lib/item.rb

Instance Method Summary collapse

Instance Method Details

#add_attribute(attribute_name, value) ⇒ Object

Add the attribute to the item

item.add_attribute :price, :value => 5, :units => 'USD'
item.add_attribute :item_type, 'Products'


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/item.rb', line 90

def add_attribute(attribute_name, value)
  # get the options passed in from the value
  options = options_from_value(value)

  attr_def = self.class.attribute_definition(attribute_name)||{}
  attr_class =  type_to_attribute_class(options[:type]||attr_def[:type]||:text)

  # add a namespace param if there is one
  options[:namespace] ||= attr_def[:namespace] if attr_def.has_key?(:namespace)

  #raise options.inspect if attribute_name.to_s == 'customme'
  # create a new attribute
  attr = attr_class.new(attr_def[:name]||attribute_name, options)

  @attributes << attr
  attr
end

#child_value(parent_name, child_name) ⇒ Object



66
67
68
69
70
# File 'lib/item.rb', line 66

def child_value(parent_name, child_name)
  parent = get_attribute(parent_name)
  child = parent.send(child_name) if parent
  child.value if child
end

#get_attribute(name) ⇒ Object



79
# File 'lib/item.rb', line 79

def get_attribute(name);  attributes.detect {|a| a.name.to_s == name.to_s }; end

#get_attributes(name) ⇒ Object

return the attribute objects that correspond to this element



78
# File 'lib/item.rb', line 78

def get_attributes(name); attributes.select {|a| a.name.to_s == name.to_s }; end

#set_attribute_private(name, is_private = true) ⇒ Object

set the google private flag for the specified attribute

item.set_attribute_private :location


83
84
85
# File 'lib/item.rb', line 83

def set_attribute_private(name, is_private=true)
   attributes.each { |attr| attr.private_attribute = is_private if name.to_s == attr.name.to_s }
end

#set_child_value(parent_name, child_name, value) ⇒ Object



72
73
74
75
# File 'lib/item.rb', line 72

def set_child_value(parent_name, child_name, value)
  parent = get_attribute(parent_name)||add_attribute(parent_name, nil)
  parent.add_child(child_name, value)
end

#type_to_attribute_class(type_name) ⇒ Object

convert type field to the corresponding Attribute class Example :text is AttributeText



109
110
111
112
113
# File 'lib/item.rb', line 109

def type_to_attribute_class(type_name)#:nodoc:
  return type_name if type_name.is_a?(Attribute)
 
  Base4R.const_get "#{type_name.to_s.gsub(/(?:^|_)(.)/) { $1.upcase } }Attribute"
end