Class: Muxml::Base::Relation::Many

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/muxml/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj, relation, options = {}) ⇒ Many

Returns a new instance of Many.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/muxml/relation.rb', line 34

def initialize(obj, relation, options = {})
  @obj = obj
  @relation = relation
  @tag = options.delete(:tag) || relation
  @class_name = options.delete(:class_name)
  @query = options.delete(:select)
  @options = options

  raise ArgumentError, "#{obj.class.to_s} :class_name is nil" if @class_name.nil?
  raise ArgumentError, "#{obj.class.to_s} :tag is nil" if @tag.nil?
  unless @query.nil?
    attributes = @query.map{|k,v| "@#{k}='#{v}'"}.join
    @find_query = "#{@tag}[#{attributes}]"
  else
    @find_query = "#{@tag}"
  end
  
end

Instance Method Details

#eachObject



53
54
55
56
57
# File 'lib/muxml/relation.rb', line 53

def each
  @obj.xml.elements.each("#{@find_query}") { |element|
    yield  Object.const_get(@class_name).new(element)
  }
end