Class: Quarto::ElementWrapper::ChildrenProxy
- Inherits:
-
Object
- Object
- Quarto::ElementWrapper::ChildrenProxy
- Includes:
- Enumerable
- Defined in:
- lib/quarto/children.rb
Overview
Any call to a children accessor method returns an instance of ChildrenProxy. For example, consider this class:
class Company < ElementWrapper::Base
children :employees
end
If you call #employees on an instance of Company, you’ll get a ChildrenProxy object.
Instance Attribute Summary collapse
-
#collection_element ⇒ Object
readonly
Returns the REXML::Element for the children collection.
Instance Method Summary collapse
-
#each ⇒ Object
Iterates over all children.
-
#empty? ⇒ Boolean
Returns true if there are no children.
-
#first ⇒ Object
Returns the first child in the collection.
-
#initialize(wrapped_parent, el_name, options = {}) ⇒ ChildrenProxy
constructor
:nodoc:.
-
#last ⇒ Object
Returns the last child in the collection.
-
#length ⇒ Object
(also: #size)
Returns the number of children.
-
#to_a(xpath = nil) ⇒ Object
Returns an array of all children.
Constructor Details
#initialize(wrapped_parent, el_name, options = {}) ⇒ ChildrenProxy
:nodoc:
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/quarto/children.rb', line 206 def initialize(wrapped_parent, el_name, = {}) # :nodoc: @wrapped_parent = wrapped_parent @el_name = el_name.to_s if [:collection_element].nil? # The subclass says there is no collection element wrapping the children @collection_element = nil else @collection_element = @wrapped_parent.element.elements[[:collection_element]] end @wrapper_class = Kernel.const_get([:wrapper_class] || @el_name.classify) end |
Instance Attribute Details
#collection_element ⇒ Object (readonly)
Returns the REXML::Element for the children collection.
189 190 191 |
# File 'lib/quarto/children.rb', line 189 def collection_element @collection_element end |
Instance Method Details
#each ⇒ Object
Iterates over all children.
192 193 194 |
# File 'lib/quarto/children.rb', line 192 def each to_a.each { |child| yield child } end |
#empty? ⇒ Boolean
Returns true if there are no children.
197 198 199 |
# File 'lib/quarto/children.rb', line 197 def empty? to_a.empty? end |
#first ⇒ Object
Returns the first child in the collection.
202 203 204 |
# File 'lib/quarto/children.rb', line 202 def first to_a.first end |
#last ⇒ Object
Returns the last child in the collection.
219 220 221 |
# File 'lib/quarto/children.rb', line 219 def last to_a.last end |
#length ⇒ Object Also known as: size
Returns the number of children.
224 225 226 |
# File 'lib/quarto/children.rb', line 224 def length to_a.length end |
#to_a(xpath = nil) ⇒ Object
Returns an array of all children. Each is an instance of ElementWrapper::Base. If xpath is provided, the results will be filtered. xpath is relative to the parent element
229 230 231 232 233 |
# File 'lib/quarto/children.rb', line 229 def to_a(xpath = nil) @all ||= (@collection_element || @wrapped_parent.element).elements.to_a(xpath || @el_name).collect do |el| @wrapper_class.new(el) end end |