Module: OM::XML::Accessors::ClassMethods

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#accessorsObject

Returns the value of attribute accessors.



4
5
6
# File 'lib/om/xml/accessors.rb', line 4

def accessors
  @accessors
end

Instance Method Details

#accessor(accessor_name, opts = {}) ⇒ Object



6
7
8
9
# File 'lib/om/xml/accessors.rb', line 6

def accessor(accessor_name, opts={})
  @accessors ||= {}
  insert_accessor(accessor_name, opts)
end

#accessor_info(*args) ⇒ Object

Returns the configuration info for the selected accessor. Ingores any integers in the array (ie. nodeset indices intended for use in other accessor convenience methods)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/om/xml/accessors.rb', line 43

def accessor_info(*args)
  # Ignore any nodeset indexes in the args array
  keys = args.select {|x| !x.kind_of?(Integer) }
  info = @accessors
  keys.each do |k|
    unless keys.index(k) == 0
      info = info.fetch(:children, nil)
      if info.nil?
        debugger
        return nil
      end
    end
    
    info = info.fetch(k, nil)
    if info.nil?
      return nil
    end
  end
  return info
end

#accessor_xpath(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/om/xml/accessors.rb', line 65

def accessor_xpath(*args)
  keys = even_values(args)
  indices = odd_values(args)
  xpath = "//"
  keys.each do |k|
    key_index = keys.index(k)
    accessor_info = accessor_info(*keys[0..key_index])
    relative_path = accessor_info[:relative_xpath]
    
    if relative_path.kind_of?(Hash)
      if relative_path.has_key?(:attribute)
        relative_path = "@"+relative_path[:attribute]
      end
    else
    
      if indices[key_index]
        add_position_predicate!(relative_path, indices[key_index])
      end
    
      if accessor_info.has_key?(:default_content_path)
        relative_path << "/"+accessor_info[:default_content_path]
      end
    
    end
    if key_index > 0
      relative_path.insert(0, "/")
    end
    xpath << relative_path 
  end
  
  return xpath
end

#add_position_predicate!(xpath_query, array_index_value) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/om/xml/accessors.rb', line 98

def add_position_predicate!(xpath_query, array_index_value)
  position_function = "position()=#{array_index_value + 1}"
  
  if xpath_query.include?("]")
    xpath_query.insert(xpath_query.rindex("]"), " and #{position_function}")
  else
    xpath_query << "[#{position_function}]"
  end
end

#even_values(array) ⇒ Object



111
112
113
# File 'lib/om/xml/accessors.rb', line 111

def even_values(array)
  array.values_at(* array.each_index.select {|i| i.even?})
end

#insert_accessor(accessor_name, accessor_opts, parent_names = []) ⇒ Object



11
12
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
# File 'lib/om/xml/accessors.rb', line 11

def insert_accessor(accessor_name, accessor_opts, parent_names=[])
  unless accessor_opts.has_key?(:relative_xpath)
    accessor_opts[:relative_xpath] = "oxns:#{accessor_name}"
  end
  
  destination = @accessors
  parent_names.each do |parent_name|
    destination = destination[parent_name][:children]
  end
  
  destination[accessor_name] = accessor_opts
  
  # Recursively call insert_accessor for any children
  if accessor_opts.has_key?(:children)
    children_array = accessor_opts[:children].dup
    accessor_opts[:children] = {}
    children_array.each do |child| 
      if child.kind_of?(Hash)
        child_name =  child.keys.first
        child_opts =  child.values.first
      else
        child_name = child
        child_opts = {}
      end
      insert_accessor(child_name, child_opts, parent_names+[accessor_name] )           
    end
  end
    
end

#odd_values(array) ⇒ Object



108
109
110
# File 'lib/om/xml/accessors.rb', line 108

def odd_values(array)
  array.values_at(* array.each_index.select {|i| i.odd?})
end