Module: OM::XML::Properties::ClassMethods

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

Overview

Class Methods – These methods will be available on classes that include this Module

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ox_namespacesObject

Returns the value of attribute ox_namespaces.



8
9
10
# File 'lib/om/xml/properties.rb', line 8

def ox_namespaces
  @ox_namespaces
end

#propertiesObject (readonly)

Returns the value of attribute properties.



9
10
11
# File 'lib/om/xml/properties.rb', line 9

def properties
  @properties
end

#root_configObject

Returns the value of attribute root_config.



8
9
10
# File 'lib/om/xml/properties.rb', line 8

def root_config
  @root_config
end

#root_property_refObject

Returns the value of attribute root_property_ref.



8
9
10
# File 'lib/om/xml/properties.rb', line 8

def root_property_ref
  @root_property_ref
end

Instance Method Details

#builder_template(property_ref, opts = {}) ⇒ Object

Builder Support



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/om/xml/properties.rb', line 279

def builder_template(property_ref, opts={})
  property_info = property_info_for(property_ref)

  prop_info = property_info.merge(opts) 
  
  if prop_info.nil?
    return nil
  else
    node_options = []
    node_child_template = ""
    if prop_info.has_key?(:default_content_path)
      node_child_options = ["\':::builder_new_value:::\'"]
      node_child_template = " { xml.#{property_info[:default_content_path]}( #{delimited_list(node_child_options)} ) }"
    else
      node_options = ["\':::builder_new_value:::\'"]
    end
    # if opts.has_key?(:attributes) ...
    # ...
    if prop_info.has_key?(:attributes)
      applicable_attributes( prop_info[:attributes] ).each_pair do |k,v|
        node_options << ":#{k}=>\'#{v}\'"
      end
    end
    template = "xml.#{prop_info[:path]}( #{delimited_list(node_options)} )" + node_child_template
    return template.gsub( /:::(.*?):::/ ) { '#{'+$1+'}' }
  end
end

#condense_value(value) ⇒ Object



331
332
333
334
335
336
337
# File 'lib/om/xml/properties.rb', line 331

def condense_value(value)
  if value.kind_of?(Array)
    return value.first
  else
    return value
  end
end

#configure_paths(prop_hash = root_config) ⇒ Object

Generates appropriate xpath queries for a property described by the given hash putting the results into the hash under keys like :xpath and :xpath_constrained This is also done recursively for all subelements and convenience methods described in the hash.



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
73
74
75
76
77
78
79
80
81
# File 'lib/om/xml/properties.rb', line 38

def configure_paths(prop_hash=root_config)
  prop_hash[:path] ||= ""
  # prop_hash[:path] = Array( prop_hash[:path] )
  xpath_opts = {}
  
  if prop_hash.has_key?(:variant_of)
    xpath_opts[:variations] = prop_hash
  end
  
  xpath_constrained_opts = xpath_opts.merge({:constraints=>:default})
  
  relative_xpath = generate_xpath(prop_hash, xpath_opts.merge(:relative=>true))
  # prop_hash[:xpath] = generate_xpath(prop_hash, xpath_opts)
  prop_hash[:xpath] = "//#{relative_xpath}"
  prop_hash[:xpath_relative] = relative_xpath
  prop_hash[:xpath_constrained] = generate_xpath(prop_hash, xpath_constrained_opts).gsub('"', '\"')
  
  prop_hash[:convenience_methods].each_pair do |cm_name, cm_props|
    cm_xpath_relative_opts = cm_props.merge(:variations => cm_props, :relative=>true)
    cm_constrained_opts = xpath_opts.merge(:constraints => cm_props)
    
    cm_relative_xpath = generate_xpath(cm_props, cm_xpath_relative_opts)
    prop_hash[:convenience_methods][cm_name][:xpath_relative] = cm_relative_xpath
    # prop_hash[:convenience_methods][cm_name][:xpath] = generate_xpath(cm_xpath_hash, cm_xpath_opts)
    prop_hash[:convenience_methods][cm_name][:xpath] = prop_hash[:xpath] + "/" + cm_relative_xpath
    prop_hash[:convenience_methods][cm_name][:xpath_constrained] = generate_xpath(prop_hash, cm_constrained_opts).gsub('"', '\"')
  end
  
  if prop_hash.has_key?(:subelements) 
    prop_hash[:subelements].each do |se|
      configure_subelement_paths(se, prop_hash, xpath_opts)
    end
  end
  
  if properties[:unresolved].has_key?(prop_hash[:ref])
    ref = prop_hash[:ref]
    properties[:unresolved][ref].each do |parent_prop_hash|
      logger.debug "Resolving #{ref} subelement for #{parent_prop_hash[:ref]} property"
      configure_subelement_paths(ref, parent_prop_hash, xpath_opts)
    end
    properties[:unresolved].delete(ref)
  end

end

#configure_property(prop_hash = root_config) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/om/xml/properties.rb', line 26

def configure_property(prop_hash=root_config)
  if prop_hash.has_key?(:variant_of)
    properties[prop_hash[:ref]] = properties[prop_hash[:variant_of]].deep_copy.merge(prop_hash)
  end
  if !prop_hash.has_key?(:convenience_methods)
    prop_hash[:convenience_methods] = {}
  end
end

#configure_subelement_paths(se, parent_prop_hash, parent_xpath_opts) ⇒ Object



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/om/xml/properties.rb', line 83

def configure_subelement_paths(se, parent_prop_hash, parent_xpath_opts)
  # se_xpath_opts = parent_xpath_opts.merge(:subelement_of => parent_prop_hash[:ref])
  se_xpath_opts = parent_xpath_opts
  new_se_props = {}
  if parent_prop_hash.has_key?(:variant_of)
    se_xpath_opts[:variations] = parent_prop_hash
  end
  
  if se.instance_of?(String)
    
    new_se_props[:path] = se
    new_se_props[:xpath_relative] = generate_xpath({:path=>se}, :relative=>true)
    new_se_props[:xpath] = parent_prop_hash[:xpath] + "/" + new_se_props[:xpath_relative]
    se_xpath_constrained_opts = se_xpath_opts.merge({:constraints=>{:path=>se}})
    new_se_props[:xpath_constrained] = generate_xpath(parent_prop_hash, se_xpath_constrained_opts)
    
  elsif se.instance_of?(Symbol) 
    
    if properties.has_key?(se)
      
      se_props = properties[se]
      
      new_se_props = se_props.deep_copy
      # new_se_props[:path] = se_props[:path]
      # new_se_props[:xpath_relative] = se_props[:xpath_relative]
      new_se_props[:xpath] = parent_prop_hash[:xpath] + "/" + new_se_props[:xpath_relative] 
      se_xpath_constrained_opts = parent_xpath_opts.merge(:constraints => se_props, :subelement_of => parent_prop_hash[:ref])        
      new_se_props[:xpath_constrained] = generate_xpath(parent_prop_hash, se_xpath_constrained_opts)
            
    else
      properties[:unresolved] ||= {}
      properties[:unresolved][se] ||= []
      properties[:unresolved][se] << parent_prop_hash
      logger.debug("Added #{se.inspect} to unresolved properties with parent #{parent_prop_hash[:ref]}")
    end                
  else
    logger.info("failed to generate path for #{se.inspect}")
    se_xpath = ""
    se_xpath_constrained = ""
  end

  if new_se_props.has_key?(:xpath_constrained)
    new_se_props[:xpath_constrained].gsub!('"', '\"')
  end
  
  properties[ parent_prop_hash[:ref] ][:convenience_methods][se.to_sym] = new_se_props  

end

#delimited_list(values_array, delimiter = ", ") ⇒ Object



270
271
272
# File 'lib/om/xml/properties.rb', line 270

def delimited_list( values_array, delimiter=", ")
  result = values_array.collect{|a| a + delimiter}.to_s.chomp(delimiter)
end

#generate_xpath(property_info, opts = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/om/xml/properties.rb', line 132

def generate_xpath( property_info, opts={})
  prefix = "oxns"
  property_info[:path] ||= "" 
  path = property_info[:path]
  path_array = Array( path )
  template = ""
  template << "//" unless opts[:relative]
  template << "#{prefix}:"
  template << delimited_list(path_array, "/#{prefix}:")

  predicates = []   
  default_content_path = property_info.has_key?(:default_content_path) ? property_info[:default_content_path] : ""
  subelement_path_parts = []
  
  # Skip everything if a template was provided
  if opts.has_key?(:template)
    template = eval('"' + opts[:template] + '"')
  else
    # Apply variations
    if opts.has_key?(:variations)
      if opts[:variations].has_key?(:attributes)
        opts[:variations][:attributes].each_pair do |attr_name, attr_value|
          predicates << "@#{attr_name}=\"#{attr_value}\""
        end
      end
      if opts[:variations].has_key?(:subelement_path) 
        if opts[:variations][:subelement_path].instance_of?(Array)
          opts[:variations][:subelement_path].each do |se_path|
            subelement_path_parts << "#{prefix}:#{se_path}"  
          end
        else
          subelement_path_parts << "#{prefix}:#{opts[:variations][:subelement_path]}"  
        end
      end
    end
  
    # Apply constraints
    if opts.has_key?(:constraints)  
      arguments_for_contains_function = []
      if opts[:constraints] == :default
        if property_info.has_key?(:default_content_path)
          default_content_path = property_info[:default_content_path]
          arguments_for_contains_function << "#{prefix}:#{default_content_path}"
        end
      elsif opts[:constraints].has_key?(:path)
        constraint_predicates = []
        if opts.has_key?(:subelement_of)
          constraint_path = "#{prefix}:#{opts[:constraints][:path]}"
          if opts[:constraints].has_key?(:default_content_path)
            constraint_path << "/#{prefix}:#{opts[:constraints][:default_content_path]}"
          end 
        else
         constraint_path = "#{prefix}:#{opts[:constraints][:path]}"
        end
        arguments_for_contains_function << constraint_path
        
        if opts[:constraints].has_key?(:attributes) && opts[:constraints][:attributes].kind_of?(Hash)
          opts[:constraints][:attributes].each_pair do |attr_name, attr_value|
            constraint_predicates << "@#{attr_name}=\"#{attr_value}\""
          end
        end
      
        unless constraint_predicates.empty?
          arguments_for_contains_function.last << "[#{delimited_list(constraint_predicates)}]"
        end
      
      end
      arguments_for_contains_function << "\":::constraint_value:::\""
    
      predicates << "contains(#{delimited_list(arguments_for_contains_function)})"
    
    end
        
    unless predicates.empty? 
      template << "["
      template << delimited_list(predicates, " and ")
      template << "]"
    end
    
    unless subelement_path_parts.empty?
      subelement_path_parts.each {|path_part| template << "/#{path_part}"}
    end
  end
  
  # result = eval( '"' + template + '"' )
  return template.gsub( /:::(.*?):::/ ) { '#{'+$1+'}' }
end

#loggerObject



339
340
341
# File 'lib/om/xml/properties.rb', line 339

def logger      
  @logger ||= defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : Logger.new(STDOUT)
end

#property(property_ref, opts = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/om/xml/properties.rb', line 19

def property( property_ref, opts={})
  @properties ||= {}
  @properties[property_ref] = opts.merge({:ref=>property_ref})
  configure_property @properties[property_ref]
  configure_paths @properties[property_ref]
end

#property_info_for(property_ref) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/om/xml/properties.rb', line 251

def property_info_for(property_ref)
  if property_ref.instance_of?(Array) && property_ref.length == 1
    property_ref = property_ref[0]      
  end
  if property_ref.instance_of?(Symbol)
    property_info = properties[property_ref]
  elsif property_ref.kind_of?(Array)
    prop_ref = property_ref[0]
    cm_name = property_ref[1]
    if properties.has_key?(prop_ref)
      property_info = properties[prop_ref][:convenience_methods][cm_name]
    end
  else
    property_info = nil
  end
  return property_info 
end

#root_property(property_ref, path, namespace, opts = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/om/xml/properties.rb', line 11

def root_property( property_ref, path, namespace, opts={})
  # property property_ref, path, opts.merge({:path=>path, :ref=>property_ref})
  @root_config = opts.merge({:namespace=>namespace, :path=>path, :ref=>property_ref})
  @root_property_ref = property_ref
  @ox_namespaces = {'oxns'=>root_config[:namespace]}
  @schema_url = opts[:schema]
end

#xpath_query_for(property_ref, query_opts = {}, opts = {}) ⇒ Object

Convenience Methods for Retrieving Generated Info



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/om/xml/properties.rb', line 224

def xpath_query_for( property_ref, query_opts={}, opts={} )

  if property_ref.instance_of?(String)
    xpath_query = property_ref
  else
    property_info = property_info_for( property_ref )

    if !property_info.nil?
      if query_opts.kind_of?(String)
        constraint_value = query_opts
        xpath_template = property_info[:xpath_constrained]
        xpath_query = eval( '"' + xpath_template + '"' )
      elsif query_opts.kind_of?(Hash) && !query_opts.empty?       
        key_value_pair = query_opts.first 
        constraint_value = key_value_pair.last
        xpath_template = property_info[:convenience_methods][key_value_pair.first][:xpath_constrained]
        xpath_query = eval( '"' + xpath_template + '"' )          
      else 
        xpath_query = property_info[:xpath]
      end
    else
      xpath_query = nil
    end
  end
  return xpath_query
end