Class: Quarto::ElementWrapper::ChildrenProxy

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {}) # :nodoc:
  @wrapped_parent = wrapped_parent
  @el_name = el_name.to_s
  if options[:collection_element].nil?
    # The subclass says there is no collection element wrapping the children
    @collection_element = nil
  else
    @collection_element = @wrapped_parent.element.elements[options[:collection_element]]
  end
  @wrapper_class = Kernel.const_get(options[:wrapper_class] || @el_name.classify)
end

Instance Attribute Details

#collection_elementObject (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

#eachObject

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.

Returns:

  • (Boolean)


197
198
199
# File 'lib/quarto/children.rb', line 197

def empty?
  to_a.empty?
end

#firstObject

Returns the first child in the collection.



202
203
204
# File 'lib/quarto/children.rb', line 202

def first
  to_a.first
end

#lastObject

Returns the last child in the collection.



219
220
221
# File 'lib/quarto/children.rb', line 219

def last
  to_a.last
end

#lengthObject 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