Module: OX::PropertyValuesHelper

Defined in:
lib/opinionated-xml/ox_property_values_helper.rb

Instance Method Summary collapse

Instance Method Details

#property_value_set(property_ref, query_opts, node_index, new_value) ⇒ Object



59
60
# File 'lib/opinionated-xml/ox_property_values_helper.rb', line 59

def property_value_set(property_ref, query_opts, node_index, new_value)
end

#property_values(lookup_args) ⇒ Object



7
8
9
10
11
# File 'lib/opinionated-xml/ox_property_values_helper.rb', line 7

def property_values(lookup_args)
  result = []
  lookup(lookup_args).each {|node| result << node.text }
  return result
end

#property_values_append(opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opinionated-xml/ox_property_values_helper.rb', line 13

def property_values_append(opts={})
  parent_select = opts[:parent_select] 
  parent_index = opts[:parent_index]
  template = opts[:template]
  new_values = opts[:values]
  
  # If template is a string, use it as the template, otherwise use it as arguments to builder_template
  unless template.instance_of?(String)
    template_args = Array(template)
    if template_args.last.kind_of?(Hash)
      template_opts = template_args.delete_at(template_args.length - 1)
    else
      template_opts = {}
    end
    template = self.class.builder_template( template_args, template_opts )
  end
  
  new_values = Array(new_values)
  # template = self.class.builder_template(property_ref) 
  
  parent_select = Array(parent_select)
  parent_nodeset = lookup(parent_select[0], parent_select[1])
  
  if parent_index.kind_of?(Integer)
    parent_node = parent_nodeset[parent_index]
  elsif parent_index.kind_of?(Symbol) && parent_nodeset.respond_to?(parent_index) 
    parent_node = parent_nodeset.send(parent_index)
  end
  
  if parent_node.nil?
    raise OX::ParentNodeNotFoundError, "Failed to find a parent node to insert values into based on :parent_select #{parent_select.inspect} with :parent_index #{parent_index.inspect}"
  end
  
  builder = Nokogiri::XML::Builder.with(parent_node) do |xml|
    new_values.each do |builder_new_value|
      builder_arg = eval('"'+ template + '"') # this inserts builder_new_value into the builder template
      eval(builder_arg)
    end
  end
      
  # Nokogiri::XML::Node.new(builder.to_xml, foo)
  
  return parent_node
  
end