Module: OM::XML::PropertyValueOperators

Defined in:
lib/om/xml/property_value_operators.rb

Instance Method Summary collapse

Instance Method Details

#property_value_delete(opts = {}) ⇒ Object

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/om/xml/property_value_operators.rb', line 120

def property_value_delete(opts={})
  parent_select = Array( opts[:parent_select] )
  parent_index = opts[:parent_index]
  child_index = opts[:child_index]
  xpath_select = opts[:select]
  
  if !xpath_select.nil?
    node = lookup(xpath_select, nil).first
  else
    parent_nodeset = lookup(parent_select, parent_select)
    # parent_nodeset = lookup(parent_select[0])
    
    if parent_index.nil?
      node = node_from_set(parent_nodeset, child_index)
    else
      parent = node_from_set(parent_nodeset, parent_index)
      # this next line is a hack around the fact that element_children() sometimes doesn't work.
      node = node_from_set(parent.xpath("*"), child_index)
    end
  end
  
  node.remove
end

#property_value_update(node_select, child_index, new_value, opts = {}) ⇒ Object



111
112
113
114
115
# File 'lib/om/xml/property_value_operators.rb', line 111

def property_value_update(node_select,child_index,new_value,opts={})
  # template = opts.fetch(:template,nil)
  node = lookup(node_select, nil)[child_index]
  node.content = new_value
end

#property_values(*lookup_args) ⇒ Object



7
8
9
10
11
# File 'lib/om/xml/property_value_operators.rb', line 7

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

#property_values_append(opts = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/om/xml/property_value_operators.rb', line 74

def property_values_append(opts={})
  parent_select = Array( opts[:parent_select] )
  child_index = opts[:child_index]
  template = opts[:template]
  new_values = Array( 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    
  
  parent_nodeset = lookup(parent_select[0], parent_select[1])
  parent_node = node_from_set(parent_nodeset, child_index)
  
  if parent_node.nil?
    raise OM::XML::ParentNodeNotFoundError, "Failed to find a parent node to insert values into based on :parent_select #{parent_select.inspect} with :child_index #{child_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

#update_properties(params = {}) ⇒ Object

example properties values hash: [{“:person”=>“0”, “role”, “text”]=>“1”=>“role2”, “2”=>“role3”, [:person=>1, :family_name]=>“Andronicus”, [“person”=>“1”,:given_name]=>,[:person=>1,:role,:text]=> }



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/om/xml/property_value_operators.rb', line 15

def update_properties(params={})
  # remove any fields from params that this datastream doesn't recognize    
  params.delete_if do |field_key,new_values| 
    if field_key.kind_of?(String)
      true
    else
      self.class.accessor_xpath(*OM.destringify(field_key) ).nil?
    end
  end
  
  result = params.dup
  
  params.each_pair do |property_pointer,new_values|
    pointer = OM.destringify(property_pointer)
    template = OM.pointers_to_flat_array(pointer,false)
    hn = self.class.accessor_hierarchical_name(*pointer)
    
    case new_values
    when Hash
    when Array
      nv = new_values.dup
      new_values = {}
      nv.each {|v| new_values[nv.index(v).to_s] = v}
    else
      new_values = {"0"=>new_values}
    end
    
    result.delete(property_pointer)
    result[hn] = new_values.dup
    
    current_values = property_values(*pointer)
    new_values.delete_if do |y,z| 
      if current_values[y.to_i]==z and y.to_i > -1
        true
      else
        false
      end
    end 
    xpath = self.class.accessor_xpath(*pointer)
    parent_pointer = pointer.dup
    parent_pointer.pop
    parent_xpath = self.class.accessor_xpath(*parent_pointer)
    new_values.each do |y,z|         
      if retrieve(*pointer)[y.to_i].nil? || y.to_i == -1
        result[hn].delete(y)
        property_values_append(:parent_select=>parent_xpath,:child_index=>0,:template=>template,:values=>z)
        new_array_index = retrieve(*pointer).length - 1
        result[hn][new_array_index.to_s] = z
      else
        property_value_update(xpath, y.to_i, z)
      end
    end
    # current_values.delete_if {|x| x == :delete || x == "" || x == nil}
    # instance_eval("#{field_accessor_method}=(current_values)") #write it back to the ds
    # result[field_name].delete("-1")
  end
  return result
end