sax_stream 1.0.10 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9395f1b5e7aa07c6f15c8c8c469390d4a5ce513b
4
- data.tar.gz: 6becdfc0f14741255c572618536a150239efb27b
3
+ metadata.gz: 82bb680bba7d2bcd8a63f9a4c2aa32df0bfe54b4
4
+ data.tar.gz: da983be14a8e7987696782d022e968977e8cb433
5
5
  SHA512:
6
- metadata.gz: 56051cea3ad5495498b5f5b0d5454d67b37ad99d5b29d3e890b3b1ff33cc091c4b88ee7461ec39b1ddf5a75e36fcbc94e017f9aeb9b624562150eccba48a68b6
7
- data.tar.gz: 86bfa74d30e96e52b8226a540e34d39946bf5b78a57124e34e94e1733bfa1568e1fcc85e1dad143d3c555fa56e2091f9974d4be954a74eef5df48881f8fb4ed0
6
+ metadata.gz: 2a05a4969707727138d3e6990c669219728f5a6602b88ac8710c766c6e92fa94f58bf049ba375011d769e58de8f059df14a9f6659235cfdcbba4513b61c56c68
7
+ data.tar.gz: e8bc73f976ccac10a0541eb8f7dbaccc2db2d2789e848a19bce8848ec5b5a13a9de00be8c71f988549f17a09515d69a0af371673407e73f66c6ec6a6c3d96c7b
@@ -19,6 +19,10 @@ module SaxStream
19
19
  end
20
20
  end
21
21
 
22
+ def relative_attributes
23
+ @attrs
24
+ end
25
+
22
26
  def content
23
27
  @content
24
28
  end
@@ -77,6 +81,10 @@ module SaxStream
77
81
  @elements.last.attributes(path)
78
82
  end
79
83
 
84
+ def relative_attributes
85
+ @elements.last.relative_attributes
86
+ end
87
+
80
88
  def record_characters(string)
81
89
  # indented_puts " record: #{string.inspect}"
82
90
  if @elements.last
@@ -0,0 +1,124 @@
1
+ module SaxStream
2
+ module Internal
3
+ class FieldMappings
4
+ class MappingOptions
5
+ def initialize(element, key)
6
+ @element = element
7
+ @key = key
8
+ @attributes = parse_attributes(key) if key.is_a?(String)
9
+ end
10
+
11
+ def element
12
+ @element
13
+ end
14
+
15
+ def allows_mapping?(key, attributes)
16
+ compare_attributes(attributes)
17
+ end
18
+
19
+ def method_missing(name, *params)
20
+ @element.send(name, *params)
21
+ end
22
+
23
+ private
24
+
25
+ def parse_attributes(key)
26
+ matches = key.match(/\[(.*)\]/)
27
+ if matches
28
+ return hashify_attribute(matches[1])
29
+ end
30
+ end
31
+
32
+ def hashify_attribute(string)
33
+ parts = string.split('=')
34
+ {parts[0] => parts[1]}
35
+ end
36
+
37
+ def compare_attributes(attributes)
38
+ return true unless @attributes
39
+
40
+ attributes_hash = Hash[*attributes.flatten]
41
+
42
+ result = !@attributes.detect do |key, value|
43
+ inner_result = attributes_hash[key] != value
44
+ inner_result
45
+ end
46
+ result
47
+ end
48
+ end
49
+
50
+ def initialize
51
+ @mappings ||= CoreExtensions::OrderedHash.new
52
+ end
53
+
54
+ def store(key, mapping)
55
+ parsed_key = build_key_from_array(key)
56
+ new_value = MappingOptions.new(mapping, key)
57
+
58
+ if class_mappings[parsed_key]
59
+ unless class_mappings[parsed_key].is_a?(Array)
60
+ old_value = class_mappings[parsed_key]
61
+ class_mappings[parsed_key] = [old_value]
62
+ end
63
+ class_mappings[parsed_key] << new_value
64
+ puts "stored array for #{parsed_key}"
65
+ else
66
+ class_mappings[parsed_key] = new_value
67
+ end
68
+ end
69
+
70
+ def class_mappings
71
+ @mappings
72
+ end
73
+
74
+ def field_mapping(key, attributes = [])
75
+ result = find_non_regex_mapping(key) || regex_field_mapping(key)
76
+
77
+ unless result.is_a?(Array)
78
+ result = [result]
79
+ end
80
+ result.compact!
81
+
82
+ result.detect do |one_result|
83
+ one_result.element if one_result.allows_mapping?(key, attributes)
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def find_non_regex_mapping(key)
90
+ class_mappings[key]
91
+ end
92
+
93
+ def regex_field_mapping(key)
94
+ regex_mappings.each do |regex, mapping|
95
+ return mapping if regex =~ key
96
+ end
97
+ nil
98
+ end
99
+
100
+ def regex_mappings
101
+ @regex_mappings ||= class_mappings.reject do |key, mapping|
102
+ !key.is_a?(Regexp)
103
+ end
104
+ end
105
+
106
+ def build_key_from_array(key_array)
107
+ if key_array.is_a?(Array)
108
+ joined_key = "(#{key_array.join('|')})".gsub('*', '[^/]+')
109
+ Regexp.new(joined_key)
110
+ else
111
+ build_key_regex(key_array)
112
+ end
113
+ end
114
+
115
+ def build_key_regex(key)
116
+ if key.include?('*')
117
+ Regexp.new(key.gsub('*', '[^/]+'))
118
+ else
119
+ key.sub(/\[.*\]/, '')
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -76,7 +76,7 @@ module SaxStream
76
76
  end
77
77
 
78
78
  def start_child_node(name, attrs)
79
- handler = @mapper_class.child_handler_for(prefix_with_element_stack(name), @collector, @stack, @current_object)
79
+ handler = @mapper_class.child_handler_for(prefix_with_element_stack(name), attrs, @collector, @stack, @current_object)
80
80
  if handler
81
81
  @stack.push(handler)
82
82
  handler.start_element(name, attrs)
@@ -1,6 +1,7 @@
1
1
  require 'sax_stream/internal/mapping_factory'
2
2
  require 'sax_stream/internal/xml_builder'
3
3
  require 'sax_stream/core_extensions/ordered_hash'
4
+ require 'sax_stream/internal/field_mappings'
4
5
 
5
6
  module SaxStream
6
7
  # Include this module to make your class map an XML node. For usage examples, see the READEME.
@@ -78,23 +79,23 @@ module SaxStream
78
79
  end
79
80
 
80
81
  def map_element_stack_top_onto_object(object, element_stack)
81
- map_key_onto_object(object, element_stack.path, element_stack.content)
82
+ map_key_onto_object(object, element_stack.path, element_stack.content, element_stack.relative_attributes)
82
83
  element_stack.attributes.each do |key, value|
83
84
  map_key_onto_object(object, key, value)
84
85
  end
85
86
  end
86
87
 
87
- def map_key_onto_object(object, key, value)
88
+ def map_key_onto_object(object, key, value, attributes = [])
88
89
  if value
89
- mapping = field_mapping(key)
90
+ mapping = field_mapping(key, attributes)
90
91
  if mapping
91
92
  mapping.map_value_onto_object(object, value)
92
93
  end
93
94
  end
94
95
  end
95
96
 
96
- def child_handler_for(key, collector, handler_stack, current_object)
97
- mapping = field_mapping(key)
97
+ def child_handler_for(key, attributes, collector, handler_stack, current_object)
98
+ mapping = field_mapping(key, attributes)
98
99
  if mapping
99
100
  mapping.handler_for(key, collector, handler_stack, current_object)
100
101
  end
@@ -108,12 +109,6 @@ module SaxStream
108
109
  @mappings_incuding_inherited ||= parent_class_values(:mappings, CoreExtensions::OrderedHash.new).merge(class_mappings).freeze
109
110
  end
110
111
 
111
- def regex_mappings
112
- @regex_mappings ||= mappings.reject do |key, mapping|
113
- !key.is_a?(Regexp)
114
- end
115
- end
116
-
117
112
  def should_collect?
118
113
  @collect
119
114
  end
@@ -123,52 +118,39 @@ module SaxStream
123
118
  @group_keys[group_name] ||= []
124
119
  end
125
120
 
121
+ def field_mapping(key, attributes = [])
122
+ field_mappings.field_mapping(key, attributes) || parent_field_mapping(key, attributes)
123
+ end
124
+
126
125
  private
127
126
 
127
+ def class_mappings
128
+ field_mappings.class_mappings
129
+ end
130
+
128
131
  def store_relation_mapping(key, mapping)
129
132
  class_relation_mappings << mapping
130
133
  store_field_mapping(key, mapping)
131
134
  end
132
135
 
133
136
  def store_field_mapping(key, mapping)
134
- key = build_key_from_array(key)
135
- class_mappings[key] = mapping
137
+ field_mappings.store(key, mapping)
136
138
  end
137
139
 
138
- def build_key_from_array(key_array)
139
- if key_array.is_a?(Array)
140
- joined_key = "(#{key_array.join('|')})".gsub('*', '[^/]+')
141
- Regexp.new(joined_key)
142
- else
143
- build_key_regex(key_array)
144
- end
140
+ def field_mappings
141
+ @field_mappings ||= Internal::FieldMappings.new
145
142
  end
146
143
 
147
- def build_key_regex(key)
148
- key.include?('*') ? Regexp.new(key.gsub('*', '[^/]+')) : key
149
- end
150
-
151
- def field_mapping(key)
152
- mappings[key] || regex_field_mapping(key)
153
- end
154
-
155
- def regex_field_mapping(key)
156
- regex_mappings.each do |regex, mapping|
157
- return mapping if regex =~ key
158
- end
159
- nil
144
+ def parent_field_mapping(*params)
145
+ parent_class_values :field_mapping, nil, *params
160
146
  end
161
147
 
162
148
  def class_relation_mappings
163
149
  @relation_mappings ||= []
164
150
  end
165
151
 
166
- def class_mappings
167
- @mappings ||= CoreExtensions::OrderedHash.new
168
- end
169
-
170
- def parent_class_values(method_name, default)
171
- superclass && superclass.respond_to?(method_name) ? superclass.send(method_name) : default
152
+ def parent_class_values(method_name, default = nil, *params)
153
+ superclass && superclass.respond_to?(method_name) ? superclass.send(method_name, *params) : default
172
154
  end
173
155
 
174
156
  def mapping_options=(values)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sax_stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.10
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Craig Ambrose
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-07 00:00:00.000000000 Z
11
+ date: 2015-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -32,12 +32,15 @@ executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
+ - ./LICENSE
36
+ - ./README.markdown
35
37
  - lib/sax_stream/collectors/block_collector.rb
36
38
  - lib/sax_stream/collectors/naive_collector.rb
37
39
  - lib/sax_stream/core_extensions/ordered_hash.rb
38
40
  - lib/sax_stream/errors.rb
39
41
  - lib/sax_stream/internal/combined_handler.rb
40
42
  - lib/sax_stream/internal/element_stack.rb
43
+ - lib/sax_stream/internal/field_mappings.rb
41
44
  - lib/sax_stream/internal/handler_stack.rb
42
45
  - lib/sax_stream/internal/mapper_handler.rb
43
46
  - lib/sax_stream/internal/mapping_factory.rb
@@ -51,12 +54,10 @@ files:
51
54
  - lib/sax_stream/internal/xml_builder.rb
52
55
  - lib/sax_stream/mapper.rb
53
56
  - lib/sax_stream/parser.rb
57
+ - lib/sax_stream/types.rb
54
58
  - lib/sax_stream/types/boolean.rb
55
59
  - lib/sax_stream/types/decimal.rb
56
60
  - lib/sax_stream/types/integer.rb
57
- - lib/sax_stream/types.rb
58
- - ./LICENSE
59
- - ./README.markdown
60
61
  homepage: http://github.com/craigambrose/sax_stream
61
62
  licenses: []
62
63
  metadata: {}
@@ -76,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
77
  version: '0'
77
78
  requirements: []
78
79
  rubyforge_project:
79
- rubygems_version: 2.0.3
80
+ rubygems_version: 2.2.2
80
81
  signing_key:
81
82
  specification_version: 4
82
83
  summary: A streaming XML parser which builds objects and passes them to a collecter