Module: Quarto::ElementWrapper::Children::ClassMethods
- Defined in:
- lib/quarto/children.rb
Overview
ElementWrapper::Base subclasses can define parent and child elements, resulting in handy accessor methods. For example:
class Company < ElementWrapper::Base
children :employees
end
class Employee < ElementWrapper::Base
parent :company
element_attr 'name'
end
and in generate.rb:
company = Company.find :first
company.employees.each do |employee|
puts employee.name
end
Instance Method Summary collapse
-
#child(method_name, options = {}) ⇒ Object
Creates an attribute for a child element.
-
#children(method_name, options = {}) ⇒ Object
Creates an attribute for child elements.
-
#has_child_named?(method_name) ⇒ Boolean
:nodoc:.
-
#has_children_named?(method_name) ⇒ Boolean
:nodoc:.
-
#has_parent_named?(method_name) ⇒ Boolean
:nodoc:.
-
#parent(method_name, options = {}) ⇒ Object
Defines the element’s parent.
Instance Method Details
#child(method_name, options = {}) ⇒ Object
Creates an attribute for a child element. el_name must be the singular form.
Options:
-
:element_name- The name of the child element. Defaults tomethod_name. -
:wrapper_class-:wrapper_class- The subclass of ElementWrapper::Base to use. Defaults to the element name.
Example:
<company>
<boss>
<name>Joe Schmoe</name>
</boss>
</company>
class Company
child :boss
end
100 101 102 |
# File 'lib/quarto/children.rb', line 100 def child(method_name, = {}) write_inheritable_hash(:singleton_children, {method_name.to_sym => }) end |
#children(method_name, options = {}) ⇒ Object
Creates an attribute for child elements.
Options:
-
:element_name- The XML element of each individual child. Default to the singular form ofmethod_name. -
:collection_element- By default, ElementWrapper assums that all children are wrapped in a collection element whose name ismethod_name. You can override this with:collection_element. If the child elements are not wrapped in a collection element at all, use:collection_element => nil. -
:wrapper_class- The subclass of ElementWrapper::Base to use. Defaults to the singular form of the element name.
Example:
<company>
<employees>
<employee>
</employee>
</employees>
</company>
class Company < ElementWrapper::Base
children :employees
end
129 130 131 132 133 134 |
# File 'lib/quarto/children.rb', line 129 def children(method_name, = {}) write_inheritable_hash(:children, {method_name.to_sym => { :element_name => method_name.to_s.singularize, :collection_element => method_name.to_s }.merge()}) end |
#has_child_named?(method_name) ⇒ Boolean
:nodoc:
136 137 138 139 |
# File 'lib/quarto/children.rb', line 136 def has_child_named?(method_name) # :nodoc: return false if read_inheritable_attribute(:singleton_children).nil? read_inheritable_attribute(:singleton_children).has_key?(method_name.to_sym) end |
#has_children_named?(method_name) ⇒ Boolean
:nodoc:
141 142 143 144 |
# File 'lib/quarto/children.rb', line 141 def has_children_named?(method_name) # :nodoc: return false if read_inheritable_attribute(:children).nil? read_inheritable_attribute(:children).has_key?(method_name.to_sym) end |
#has_parent_named?(method_name) ⇒ Boolean
:nodoc:
146 147 148 149 |
# File 'lib/quarto/children.rb', line 146 def has_parent_named?(method_name) # :nodoc: return false if read_inheritable_attribute(:parent).nil? read_inheritable_attribute(:parent)[:method] == method_name.to_sym end |
#parent(method_name, options = {}) ⇒ Object
Defines the element’s parent. Options:
-
:element_name- The name of the parent element. Defaults tomethod_name. -
:wrapper_class- The subclass of ElementWrapper::Base to use. Defaults to the element name.
Example:
<company>
<employees>
<employee>
</employee>
</employees>
</company>
class Employee < ElementWrapper::Base
parent :company
end
169 170 171 |
# File 'lib/quarto/children.rb', line 169 def parent(method_name, = {}) write_inheritable_attribute(:parent, {:method => method_name.to_sym, :element_name => method_name.to_s}.merge()) end |