Class: SaxStream::Internal::FieldMappings

Inherits:
Object
  • Object
show all
Defined in:
lib/sax_stream/internal/field_mappings.rb

Defined Under Namespace

Classes: MappingOptions

Instance Method Summary collapse

Constructor Details

#initializeFieldMappings

Returns a new instance of FieldMappings.



53
54
55
# File 'lib/sax_stream/internal/field_mappings.rb', line 53

def initialize
  @mappings ||= CoreExtensions::OrderedHash.new
end

Instance Method Details

#class_mappingsObject



72
73
74
# File 'lib/sax_stream/internal/field_mappings.rb', line 72

def class_mappings
  @mappings
end

#field_mapping(key, attributes = []) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sax_stream/internal/field_mappings.rb', line 76

def field_mapping(key, attributes = [])
  result = find_non_regex_mapping(key) || regex_field_mapping(key)

  unless result.is_a?(Array)
    result = [result]
  end
  result.compact!

  element = result.detect do |one_result|
    one_result.element if one_result.allows_mapping?(key, attributes)
  end

  return element if element

  return @map_all if @map_all && @map_all.allows_mapping?(key, attributes)
end

#set_map_all(options = {}) ⇒ Object



93
94
95
# File 'lib/sax_stream/internal/field_mappings.rb', line 93

def set_map_all(options = {})
  @map_all = Mappings::Wildcard.new(options)
end

#store(key, mapping) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sax_stream/internal/field_mappings.rb', line 57

def store(key, mapping)
  parsed_key = build_key_from_array(key)
  new_value = MappingOptions.new(mapping, key)

  if class_mappings[parsed_key]
    unless class_mappings[parsed_key].is_a?(Array)
      old_value = class_mappings[parsed_key]
      class_mappings[parsed_key] = [old_value]
    end
    class_mappings[parsed_key] << new_value
  else
    class_mappings[parsed_key] = new_value
  end
end