Module: OM::XML::Accessors::ClassMethods
- Defined in:
- lib/om/xml/accessors.rb
Instance Attribute Summary collapse
-
#accessors ⇒ Object
Returns the value of attribute accessors.
Instance Method Summary collapse
- #accessor(accessor_name, opts = {}) ⇒ Object
- #accessor_generic_name(*pointers) ⇒ Object
- #accessor_hierarchical_name(*pointers) ⇒ Object
-
#accessor_info(*pointers) ⇒ Object
Returns the configuration info for the selected accessor.
- #accessor_xpath(*pointers) ⇒ Object
- #add_position_predicate(xpath_query, array_index_value) ⇒ Object
- #insert_accessor(accessor_name, accessor_opts, parent_names = []) ⇒ Object
-
#pointers_to_flat_array(pointers, include_indices = true) ⇒ Object
Converts an array of accessor pointers into a flat array.
Instance Attribute Details
#accessors ⇒ Object
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_generic_name(*pointers) ⇒ Object
125 126 127 |
# File 'lib/om/xml/accessors.rb', line 125 def accessor_generic_name(*pointers) pointers_to_flat_array(pointers, false).join("_") end |
#accessor_hierarchical_name(*pointers) ⇒ Object
129 130 131 |
# File 'lib/om/xml/accessors.rb', line 129 def accessor_hierarchical_name(*pointers) pointers_to_flat_array(pointers, true).join("_") end |
#accessor_info(*pointers) ⇒ 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 63 64 |
# File 'lib/om/xml/accessors.rb', line 43 def accessor_info(*pointers) info = @accessors # flatten the pointers array, excluding node indices pointers = pointers_to_flat_array(pointers, false) pointers.each do |pointer| unless pointers.index(pointer) == 0 info = info.fetch(:children, nil) if info.nil? return nil end end info = info.fetch(pointer, nil) if info.nil? return nil end end return info end |
#accessor_xpath(*pointers) ⇒ Object
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/om/xml/accessors.rb', line 67 def accessor_xpath(*pointers) keys = [] xpath = "//" pointers.each do |pointer| if pointer.kind_of?(Hash) k = pointer.keys.first index = pointer[k] else k = pointer index = nil end keys << k # key_index = keys.index(k) pointer_index = pointers.index(pointer) # accessor_info = accessor_info(*keys[0..key_index]) accessor_info = accessor_info(*keys) 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 unless index.nil? relative_path = add_position_predicate(relative_path, index) end if accessor_info.has_key?(:default_content_path) relative_path << "/"+accessor_info[:default_content_path] end end if pointer_index > 0 relative_path = "/"+relative_path end xpath << relative_path end return xpath end |
#add_position_predicate(xpath_query, array_index_value) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/om/xml/accessors.rb', line 113 def add_position_predicate(xpath_query, array_index_value) modified_query = xpath_query.dup position_function = "position()=#{array_index_value + 1}" if xpath_query.include?("]") modified_query.insert(xpath_query.rindex("]"), " and #{position_function}") else modified_query << "[#{position_function}]" end return modified_query 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 |
#pointers_to_flat_array(pointers, include_indices = true) ⇒ Object
Converts an array of accessor pointers into a flat array. ie. [:conference=>0, :role=>1, :text] becomes [:conference, 0, :role, 1, :text]
if include_indices is set to false,
[{:conference=>0}, {:role=>1}, :text] becomes [:conference, :role, :text]
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/om/xml/accessors.rb', line 139 def pointers_to_flat_array(pointers, include_indices=true) flat_array = [] pointers.each do |pointer| if pointer.kind_of?(Hash) flat_array << pointer.keys.first if include_indices flat_array << pointer.values.first end else flat_array << pointer end end return flat_array end |