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_constrained_xpath(pointers, constraint) ⇒ 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_node_index_predicate(xpath_query, array_index_value) ⇒ Object
Adds xpath xpath node index predicate to the end of your xpath query Example: add_node_index_predicate(“//oxns:titleInfo”,0) => “//oxns:titleInfo”.
-
#add_position_predicate(xpath_query, array_index_value) ⇒ Object
Adds xpath:position() method call to the end of your xpath query Examples: .
- #add_predicate(xpath_query, predicate) ⇒ Object
-
#generate_accessors_from_properties ⇒ Object
Generates accessors from the object’s @properties hash.
- #insert_accessor(accessor_name, accessor_opts, parent_names = []) ⇒ Object
-
#insert_accessor_from_property(property_ref, property_info, parent_names = []) ⇒ Object
Recurses through a property’s info and convenience methods, adding accessors as necessary.
- #pointers_to_flat_array(pointers, include_indices = true) ⇒ Object
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_constrained_xpath(pointers, constraint) ⇒ Object
143 144 145 146 147 |
# File 'lib/om/xml/accessors.rb', line 143 def accessor_constrained_xpath(pointers, constraint) constraint_function = "contains(., \"#{constraint}\")" xpath = self.accessor_xpath(*pointers) xpath = self.add_predicate(xpath, constraint_function) end |
#accessor_generic_name(*pointers) ⇒ Object
185 186 187 |
# File 'lib/om/xml/accessors.rb', line 185 def accessor_generic_name(*pointers) pointers_to_flat_array(pointers, false).join("_") end |
#accessor_hierarchical_name(*pointers) ⇒ Object
189 190 191 |
# File 'lib/om/xml/accessors.rb', line 189 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)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/om/xml/accessors.rb', line 66 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/om/xml/accessors.rb', line 90 def accessor_xpath(*pointers) if pointers.first.kind_of?(String) return pointers.first end 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) # Return nil if there is no accessor info to work with if accessor_info.nil? then return nil end 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_node_index_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_node_index_predicate(xpath_query, array_index_value) ⇒ Object
Adds xpath xpath node index predicate to the end of your xpath query Example: add_node_index_predicate(“//oxns:titleInfo”,0)
=> "//oxns:titleInfo[1]"
add_node_index_predicate(“//oxns:titleInfo”,0)
=> "//oxns:titleInfo[@lang=\"finnish\"][1]"
156 157 158 159 |
# File 'lib/om/xml/accessors.rb', line 156 def add_node_index_predicate(xpath_query, array_index_value) modified_query = xpath_query.dup modified_query << "[#{array_index_value + 1}]" end |
#add_position_predicate(xpath_query, array_index_value) ⇒ Object
Adds xpath:position() method call to the end of your xpath query Examples:
add_position_predicate(“//oxns:titleInfo”,0)
> “//oxns:titleInfo”
add_position_predicate(“//oxns:titleInfo”,0)
> “//oxns:titleInfo[@lang="finnish" and position()=1]”
169 170 171 172 |
# File 'lib/om/xml/accessors.rb', line 169 def add_position_predicate(xpath_query, array_index_value) position_function = "position()=#{array_index_value + 1}" self.add_predicate(xpath_query, position_function) end |
#add_predicate(xpath_query, predicate) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/om/xml/accessors.rb', line 174 def add_predicate(xpath_query, predicate) modified_query = xpath_query.dup # if xpath_query.include?("]") if xpath_query[xpath_query.length-1..xpath_query.length] == "]" modified_query.insert(xpath_query.rindex("]"), " and #{predicate}") else modified_query << "[#{predicate}]" end return modified_query end |
#generate_accessors_from_properties ⇒ Object
Generates accessors from the object’s @properties hash. If no properties have been declared, it doesn’t do anything.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/om/xml/accessors.rb', line 43 def generate_accessors_from_properties if self.properties.nil? || self.properties.empty? return nil end @accessors ||= {} # Skip the :unresolved portion of the properties hash accessorizables = self.properties.dup accessorizables.delete(:unresolved) accessorizables.each_pair do |property_ref, property_info| insert_accessor_from_property(property_ref, property_info) end 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 |
#insert_accessor_from_property(property_ref, property_info, parent_names = []) ⇒ Object
Recurses through a property’s info and convenience methods, adding accessors as necessary
57 58 59 60 61 62 |
# File 'lib/om/xml/accessors.rb', line 57 def insert_accessor_from_property(property_ref, property_info, parent_names=[]) insert_accessor(property_ref,{:relative_xpath => property_info[:xpath_relative], :children=>[]}, parent_names) property_info.fetch(:convenience_methods,{}).each_pair do |cm_name, cm_info| insert_accessor_from_property(cm_name, cm_info, parent_names+[property_ref] ) end end |
#pointers_to_flat_array(pointers, include_indices = true) ⇒ Object
193 194 195 |
# File 'lib/om/xml/accessors.rb', line 193 def pointers_to_flat_array(pointers, include_indices=true) OM.pointers_to_flat_array(pointers, include_indices) end |