Class: SaxStream::Internal::Mappings::Wildcard

Inherits:
ElementAttribute show all
Defined in:
lib/sax_stream/internal/mappings/wildcard.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from ElementAttribute

#update_parent_node

Methods inherited from Element

#find_or_insert_node, #string_value_from_object

Methods inherited from Base

#find_or_insert_parent_node, #handler_for, #path_parts, #update_parent_node

Constructor Details

#initialize(options = {}) ⇒ Wildcard

Returns a new instance of Wildcard.



7
8
9
# File 'lib/sax_stream/internal/mappings/wildcard.rb', line 7

def initialize(options = {})
  @recursive = options[:recursive]
end

Instance Method Details

#allows_mapping?(key, attributes) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/sax_stream/internal/mappings/wildcard.rb', line 24

def allows_mapping?(key, attributes)
  leaf = key.split('/').last
  leaf && leaf[0] == '@'
end

#map_value_onto_object(object, key, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sax_stream/internal/mappings/wildcard.rb', line 11

def map_value_onto_object(object, key, value)
  child_parts = key.split('/')
  name_part = child_parts.shift

  if child_parts.empty?
    name = scrub_attribute_name name_part
    write_value_to_object object, value, name
  elsif @recursive
    object[name_part] ||= {}
    write_into_hash(object[name_part], child_parts, value)
  end
end