sax_stream 1.1.1 → 1.2.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: 0b92d01207ce176bccc6b89b91049660feb74e10
4
- data.tar.gz: d05a361e24adbbe12b17d8efb5e4337b3c90b670
3
+ metadata.gz: aa93757eca0ca716b93e420f7f075a959af5c2ba
4
+ data.tar.gz: 01311ae0231a5ab69e839074450fb56ef4c73c93
5
5
  SHA512:
6
- metadata.gz: 8bfbe467ae02c01a98a7080f0df91eb954fd113f0a64cd08a85d57b9073c2f61f29244939bf9752738be0037e00139e3634a7e115a2ae79be3ee626693e9b8ad
7
- data.tar.gz: 2c7e2f0d6b83820e018c3bc008098827c539b67785cdb15d9230c0cd83d3acf558982e46c35b0945ce32959c5b857367cb82f0c51a9c518b7acb1612e9daf222
6
+ metadata.gz: 7ca37019ef5614199291253c4f0d1010e8a36e0e6f4feeee8aca0ab18053f91c93d70cda6d9c3941cc7bb11c612d0068bea67085cf29f7e32d8f6907d15a6cf3
7
+ data.tar.gz: 9f33b1075f1a7b99f5e41d65810e414c6ef7be2fd7b80d809f6fe6da4e5849465d8976a43caf1dd95270c0412d7789ef84137c7dfa755936507c9e24013a5aea
@@ -1,3 +1,5 @@
1
+ require 'sax_stream/internal/mappings/wildcard'
2
+
1
3
  module SaxStream
2
4
  module Internal
3
5
  class FieldMappings
@@ -6,6 +8,7 @@ module SaxStream
6
8
  @element = element
7
9
  @key = key
8
10
  @attributes = parse_attributes(key) if key.is_a?(String)
11
+ @map_all = false
9
12
  end
10
13
 
11
14
  def element
@@ -78,9 +81,17 @@ module SaxStream
78
81
  end
79
82
  result.compact!
80
83
 
81
- result.detect do |one_result|
84
+ element = result.detect do |one_result|
82
85
  one_result.element if one_result.allows_mapping?(key, attributes)
83
86
  end
87
+
88
+ return element if element
89
+
90
+ return @map_all if @map_all && @map_all.allows_mapping?(key, attributes)
91
+ end
92
+
93
+ def set_map_all(options = {})
94
+ @map_all = Mappings::Wildcard.new(options)
84
95
  end
85
96
 
86
97
  private
@@ -19,7 +19,7 @@ module SaxStream
19
19
  raise "could not split #{@path.inspect} for #{@name.inspect}"
20
20
  end
21
21
 
22
- def map_value_onto_object(object, value)
22
+ def map_value_onto_object(object, key, value)
23
23
  end
24
24
 
25
25
  def find_or_insert_parent_node(doc, base)
@@ -4,15 +4,11 @@ module SaxStream
4
4
  module Internal
5
5
  module Mappings
6
6
  class Element < Base
7
- def map_value_onto_object(object, value)
7
+ def map_value_onto_object(object, key, value)
8
8
  if value && @parser
9
9
  value = @parser.parse(value)
10
10
  end
11
- if object.respond_to?(setter_method)
12
- object.send(setter_method, value)
13
- else
14
- object[@name] = value
15
- end
11
+ write_value_to_object object, value
16
12
  end
17
13
 
18
14
  def string_value_from_object(object)
@@ -34,8 +30,17 @@ module SaxStream
34
30
  end
35
31
  end
36
32
 
37
- def setter_method
38
- "#{@name}=".to_sym
33
+ def setter_method(name = nil)
34
+ name ||= @name
35
+ "#{name}=".to_sym
36
+ end
37
+
38
+ def write_value_to_object(object, value, name = nil)
39
+ if object.respond_to?(setter_method(name))
40
+ object.send(setter_method(name), value)
41
+ else
42
+ object[name || @name] = value
43
+ end
39
44
  end
40
45
 
41
46
  def process_conversion_type(as)
@@ -0,0 +1,52 @@
1
+ require 'sax_stream/internal/mappings/element_attribute'
2
+
3
+ module SaxStream
4
+ module Internal
5
+ module Mappings
6
+ class Wildcard < ElementAttribute
7
+ def initialize(options = {})
8
+ @recursive = options[:recursive]
9
+ end
10
+
11
+ def map_value_onto_object(object, key, value)
12
+ child_parts = key.split('/')
13
+ name_part = child_parts.shift
14
+
15
+ if child_parts.empty?
16
+ name = scrub_attribute_name name_part
17
+ write_value_to_object object, value, name
18
+ elsif @recursive
19
+ object[name_part] ||= {}
20
+ write_into_hash(object[name_part], child_parts, value)
21
+ end
22
+ end
23
+
24
+ def allows_mapping?(key, attributes)
25
+ leaf = key.split('/').last
26
+ leaf && leaf[0] == '@'
27
+ end
28
+
29
+ private
30
+
31
+ def base_attribute_name
32
+ path_parts.last.sub(/^@/, '')
33
+ end
34
+
35
+ def write_into_hash(hash, keys, value)
36
+ keys.each_with_index do |key, index|
37
+ if index == keys.length - 1
38
+ hash[scrub_attribute_name(key)] = value
39
+ else
40
+ hash[key] ||= {}
41
+ hash = hash[key]
42
+ end
43
+ end
44
+ end
45
+
46
+ def scrub_attribute_name(key)
47
+ key.sub(/^@/, '')
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -22,6 +22,10 @@ module SaxStream
22
22
  store_field_mapping(options[:to], mapping)
23
23
  end
24
24
 
25
+ def map_all(*params)
26
+ field_mappings.set_map_all(*params)
27
+ end
28
+
25
29
  def attribute_group(group_name)
26
30
  self.mapping_options = {group_name: group_name}
27
31
  yield
@@ -89,7 +93,7 @@ module SaxStream
89
93
  if value
90
94
  mapping = field_mapping(key, attributes)
91
95
  if mapping
92
- mapping.map_value_onto_object(object, value)
96
+ mapping.map_value_onto_object(object, key, value)
93
97
  end
94
98
  end
95
99
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sax_stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.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: 2015-02-09 00:00:00.000000000 Z
11
+ date: 2015-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 1.5.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.5.2
27
27
  description: A streaming XML parser which builds objects and passes them to a collecter
@@ -32,8 +32,8 @@ executables: []
32
32
  extensions: []
33
33
  extra_rdoc_files: []
34
34
  files:
35
- - ./LICENSE
36
- - ./README.markdown
35
+ - "./LICENSE"
36
+ - "./README.markdown"
37
37
  - lib/sax_stream/collectors/block_collector.rb
38
38
  - lib/sax_stream/collectors/naive_collector.rb
39
39
  - lib/sax_stream/core_extensions/ordered_hash.rb
@@ -49,6 +49,7 @@ files:
49
49
  - lib/sax_stream/internal/mappings/element.rb
50
50
  - lib/sax_stream/internal/mappings/element_attribute.rb
51
51
  - lib/sax_stream/internal/mappings/element_content.rb
52
+ - lib/sax_stream/internal/mappings/wildcard.rb
52
53
  - lib/sax_stream/internal/sax_handler.rb
53
54
  - lib/sax_stream/internal/singular_relationship_collector.rb
54
55
  - lib/sax_stream/internal/xml_builder.rb
@@ -67,17 +68,17 @@ require_paths:
67
68
  - lib
68
69
  required_ruby_version: !ruby/object:Gem::Requirement
69
70
  requirements:
70
- - - '>='
71
+ - - ">="
71
72
  - !ruby/object:Gem::Version
72
73
  version: '0'
73
74
  required_rubygems_version: !ruby/object:Gem::Requirement
74
75
  requirements:
75
- - - '>='
76
+ - - ">="
76
77
  - !ruby/object:Gem::Version
77
78
  version: '0'
78
79
  requirements: []
79
80
  rubyforge_project:
80
- rubygems_version: 2.2.2
81
+ rubygems_version: 2.4.6
81
82
  signing_key:
82
83
  specification_version: 4
83
84
  summary: A streaming XML parser which builds objects and passes them to a collecter