Class: Base4R::Attribute

Inherits:
Object
  • Object
show all
Includes:
AdditionalXmlAttributeMethods, ChildAttributeMethods
Defined in:
lib/attribute.rb

Overview

Attributes are typed key-value pairs that describe content. Attributes describe the Base Item. Client code should use subclasses of Attribute such as TextAttribute, BareAttribute, DateTimeAttribute, IntAttribute, FloatUnitAttribute, UrlAttribute, LocationAttribute, BooleanAttribute. Each of these can represent themselves as required by the Atom format.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AdditionalXmlAttributeMethods

#additional_xml_attribute_map, included, #xml_attribute_value, #xml_attributes

Methods included from ChildAttributeMethods

#add_child, #add_child_attribute, #child, #children, #children_map, #children_names, included

Instance Attribute Details

#nameObject

Returns the value of attribute name.



134
135
136
# File 'lib/attribute.rb', line 134

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



136
137
138
# File 'lib/attribute.rb', line 136

def namespace
  @namespace
end

#optionsObject

Returns the value of attribute options.



137
138
139
# File 'lib/attribute.rb', line 137

def options
  @options
end

#private_attributeObject

Returns the value of attribute private_attribute.



138
139
140
# File 'lib/attribute.rb', line 138

def private_attribute
  @private_attribute
end

#valueObject

Returns the value of attribute value.



135
136
137
# File 'lib/attribute.rb', line 135

def value
  @value
end

Class Method Details

.to_xml(parent, name, value, options = {}) ⇒ Object

Create an xml object with this

BareAttribute.new(parent, :myattr, 'love', :private_attribute => true)


182
183
184
# File 'lib/attribute.rb', line 182

def to_xml(parent, name, value, options={})
  new( name, value, options).to_xml(parent)
end

.type_nameObject

keep this pure ruby instead of using rails stuff



177
178
179
# File 'lib/attribute.rb', line 177

def type_name
  self.to_s.gsub(/^Base4R::(\w)(\w*)Attribute$/) { "#{$1.downcase}#{$2}" }
end

Instance Method Details

#private_attribute?Boolean

Returns:

  • (Boolean)


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

def private_attribute?; private_attribute.is_a?(TrueClass); end

#to_xml(parent, options = {}) ⇒ Object

Represent this Attribute as an XML element that is a child of parent.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/attribute.rb', line 144

def to_xml(parent, options={})

  
  return if value.nil? && children.empty? && !options[:force].is_a?(TrueClass)


 
  el = parent.add_element(calc_el_name)
  
  el.add_attribute('type', type_name)   if type_name && !type_name.empty?
  el.add_attribute('access', 'private') if private_attribute?




  #add additional attributes like href=http://meh.com
  additional_xml_attribute_map.each { |k,v| el.add_attribute(k.to_s, v.to_s) if v }

  el.text = value.to_s if value

  children.each {|child| child.to_xml(el) }
  el
end

#type_nameObject

the type name for this attribute



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

def type_name; @type_name || self.class.type_name; end

#type_name=(value) ⇒ Object



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

def type_name=(value); @type_name=value.to_s; end