pump 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c60ca511d26b937009e1b16e1473b4e664509ab9
4
- data.tar.gz: 0f6c9cf439f290237b710c56ed25d87b79ccca5b
3
+ metadata.gz: b23d242fff172d8de112af6c2fc6f326e101bd14
4
+ data.tar.gz: 4e369b0d6f692526571a487c1e692199b21d1efb
5
5
  SHA512:
6
- metadata.gz: cf21d8aa68ff75a6bb584390bdc16880a48c600057834dcb2aac8457c76268a20e294c8900056854a3019f13065f2eccd8e19ac5e70cac17d4f4b3700914a941
7
- data.tar.gz: d1441b8fb1321f5452a25d98568ac35bc9e0a5d0b72866eae2bc795b2bdccb3570def280d700e84c203d5d72f05c081dd28a519014f4475c8d093e2b956b6f84
6
+ metadata.gz: 3593cfcda6c0a33bd2f1cdf0d7010fb6f1b627a11a33c675fa2ab0854771e8b3cd109137d26bb8cf557e41ea21345b4c2b018f0b6a0b3105fa73bfc1c16917e3
7
+ data.tar.gz: 6c366d3a85852a28c5bb536bd2daed8eaf23f1e2d14972e73fd3c951c00d0dd8dd1895450b65a21d6ab33d1d2ad13529fa0ebc5e949616c7e42969f4019d3c24
data/CHANGES.md CHANGED
@@ -1,6 +1,13 @@
1
1
  ### dev
2
2
 
3
- [full changelog](http://github.com/yolk/pump/compare/v0.6.2...master)
3
+ [full changelog](http://github.com/yolk/pump/compare/v0.6.3...master)
4
+
5
+ ### 0.6.3 / 2013-07-26
6
+
7
+ [full changelog](http://github.com/yolk/valvat/compare/v0.6.2...v0.6.3)
8
+
9
+ * Pump::Xml allow partial output with :fields option
10
+ * Pump::JSON allow partial output with :fields option
4
11
 
5
12
  ### 0.6.2 / 2013-07-26
6
13
 
data/lib/pump/encoder.rb CHANGED
@@ -41,6 +41,7 @@ module Pump
41
41
  @root_name = root_name
42
42
  merge_base
43
43
 
44
+ compile_field_map
44
45
  compile
45
46
  end
46
47
 
@@ -53,7 +54,14 @@ module Pump
53
54
  # @return [String]
54
55
  def encode(object, options={})
55
56
  object = object.to_a if defined?(ActiveRecord::Relation) && object.is_a?(ActiveRecord::Relation)
56
- object.is_a?(Array) ? encode_array(object, options) : encode_single(object, options)
57
+ fields_to_hash(options)
58
+ if object.is_a?(Array)
59
+ encode_array(object, options)
60
+ elsif options[:fields]
61
+ encode_partial_single(object, options)
62
+ else
63
+ encode_single(object, options)
64
+ end
57
65
  end
58
66
 
59
67
  private
@@ -64,6 +72,21 @@ module Pump
64
72
 
65
73
  def compile_string;end
66
74
 
75
+ def compile_field_map
76
+ instance_eval("@fields_map = { #{compile_string_fields_map} }")
77
+ end
78
+
79
+ def compile_string_fields_map
80
+ encoder_config.map do |config|
81
+ config.keys.first
82
+ end.inject([]) do |array, name|
83
+ underscores = name.to_s.underscore
84
+ dashes = name.to_s.dasherize
85
+ array << "'#{dashes}' => true" if dashes != underscores
86
+ array << "'#{underscores}' => true"
87
+ end.join(',')
88
+ end
89
+
67
90
  def merge_base
68
91
  return unless @encoder_options[:base]
69
92
  @base = @encoder_options.delete(:base)
@@ -89,5 +112,13 @@ module Pump
89
112
  def merge_base_options
90
113
  encoder_options.merge!(base.encoder_options) { |key, v1, v2| v1 }
91
114
  end
115
+
116
+ def fields_to_hash(options)
117
+ return unless options[:fields]
118
+ options[:fields] = options[:fields].inject({}) do |hash, name|
119
+ hash[name.to_s.underscore.to_sym] = true if @fields_map[name.to_s]
120
+ hash
121
+ end
122
+ end
92
123
  end
93
124
  end
data/lib/pump/json.rb CHANGED
@@ -8,13 +8,15 @@ module Pump
8
8
  private
9
9
 
10
10
  OJ_OPTIONS = {
11
- :mode => :compat,
12
- :time_format => :xmlschema,
11
+ :mode => :compat,
12
+ :time_format => :xmlschema,
13
13
  :second_precision => 0
14
14
  }
15
15
 
16
16
  def compile_string
17
17
  main = build_main
18
+ partial_main = build_main(:partial => true)
19
+
18
20
  <<-EOV
19
21
  def encode_single(object, options)
20
22
  #{main}
@@ -24,6 +26,14 @@ module Pump
24
26
  Oj.dump(json, OJ_OPTIONS)
25
27
  end
26
28
 
29
+ def encode_partial_single(object, options)
30
+ #{partial_main}
31
+ unless options[:exclude_root_in_json]
32
+ json = { :'#{format_name(root_name)}' => json }
33
+ end
34
+ Oj.dump(json, OJ_OPTIONS)
35
+ end
36
+
27
37
  def encode_array(objects, options)
28
38
  Oj.dump(if options[:exclude_root_in_json]
29
39
  objects.map do |object|
@@ -40,52 +50,54 @@ module Pump
40
50
  EOV
41
51
  end
42
52
 
43
- def build_main
53
+ def build_main(options={})
44
54
  <<-EOV
55
+ field_hash = options[:fields]
45
56
  json = {}
46
- #{build_part(encoder_config)}
57
+ #{build_part(encoder_config, 'json', options)}
47
58
  EOV
48
59
  end
49
60
 
50
- def build_part(config, variable='json')
61
+ def build_part(config, variable, options, path=[])
51
62
  config.inject("") do |str, config|
52
- build_key_value_pair(str, config, variable)
63
+ build_key_value_pair(str, config, variable, options, path)
53
64
  str
54
65
  end
55
66
  end
56
67
 
57
- def build_key_value_pair(str, config, variable='json')
68
+ def build_key_value_pair(str, config, variable, options, path)
58
69
  name, method_name = config.keys.first, config.values.first
70
+ condition = build_condition(name, config, options, path)
59
71
  if method_name.is_a?(Array) && !config.has_key?(:static_value)
60
- build_hash(str, name, method_name, config, variable)
72
+ build_hash(str, name, method_name, config, variable, options, path, condition)
61
73
  elsif config[:array]
62
- build_array(str, name, method_name, config, variable)
74
+ build_array(str, name, method_name, config, variable, options, path, condition)
63
75
  else
64
- build_simple(str, name, method_name, config, variable)
76
+ build_simple(str, name, method_name, config, variable, options, path, condition)
65
77
  end
66
78
  end
67
79
 
68
- def build_hash(str, name, method_name, config, variable)
69
- str << "#{build_condition(config)}\n"
80
+ def build_hash(str, name, method_name, config, variable, options, path, condition)
81
+ str << "#{condition}\n" if condition
70
82
  str << "#{variable}[:'#{format_name(name)}'] = {}\n"
71
- str << build_part(method_name, "#{variable}[:'#{format_name(name)}']")
72
- str << "end\n" if build_condition(config)
83
+ str << build_part(method_name, "#{variable}[:'#{format_name(name)}']", options, path.dup << name)
84
+ str << "end\n" if condition
73
85
  end
74
86
 
75
- def build_array(str, name, method_name, config, variable)
76
- str << "#{build_condition(config)}\n"
87
+ def build_array(str, name, method_name, config, variable, options, path, condition)
88
+ str << "#{condition}\n" if condition
77
89
  str << "#{variable}[:'#{format_name(name)}'] = []\n"
78
90
  unless config.has_key?(:static_value)
79
91
  str << "object.#{method_name}.each do |object| "
80
92
  str << "#{variable}[:'#{format_name(name)}'] << {}\n"
81
- str << build_part(config[:array], "#{variable}[:'#{format_name(name)}'][-1]")
93
+ str << build_part(config[:array], "#{variable}[:'#{format_name(name)}'][-1]", options, path.dup << name)
82
94
  str << "end\n"
83
95
  end
84
- str << "end\n" if build_condition(config)
96
+ str << "end\n" if condition
85
97
  end
86
98
 
87
- def build_simple(str, name, method_name, config, variable)
88
- str << "#{variable}[:'#{format_name(name)}']=#{build_value(method_name, config)}#{build_condition(config)}\n"
99
+ def build_simple(str, name, method_name, config, variable, options, path, condition)
100
+ str << "#{variable}[:'#{format_name(name)}']=#{build_value(method_name, config)}#{condition}\n"
89
101
  end
90
102
 
91
103
  def build_value(method_name, config)
@@ -93,11 +105,24 @@ module Pump
93
105
  "object.#{method_name}"
94
106
  end
95
107
 
96
- def build_condition(config)
108
+ def build_condition(name, config, options, path)
109
+ conditions = []
110
+ conditions << build_partial_condition(name, path) if options[:partial]
111
+
97
112
  if config[:if]
98
- " if object.#{config[:if]}"
113
+ conditions << "object.#{config[:if]}"
99
114
  elsif config[:unless]
100
- " unless object.#{config[:unless]}"
115
+ conditions << "!object.#{config[:unless]}"
116
+ end
117
+
118
+ conditions.any? ? " if #{conditions.join(" && ")} " : nil
119
+ end
120
+
121
+ def build_partial_condition(name, path)
122
+ if path.any?
123
+ "field_hash[:'#{path.join('.')}']"
124
+ else
125
+ "field_hash[:#{name.to_s.underscore}]"
101
126
  end
102
127
  end
103
128
 
data/lib/pump/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pump
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
data/lib/pump/xml.rb CHANGED
@@ -15,25 +15,30 @@ module Pump
15
15
  def encode_array(objects, options)
16
16
  "#{TagArray.new(root_name, {}, sub_tags, tag_options)}"
17
17
  end
18
+
19
+ def encode_partial_single(object, options)
20
+ field_hash = options[:fields]
21
+ "#{Tag.new(root_name, {}, sub_tags(true), tag_options)}"
22
+ end
18
23
  EOV
19
24
  end
20
25
 
21
- def sub_tags
26
+ def sub_tags(partial=false)
22
27
  encoder_config.map do |config|
23
- build_tag(config)
28
+ build_tag(config, partial)
24
29
  end
25
30
  end
26
31
 
27
- def build_tag(config)
32
+ def build_tag(config, partial, path=[])
28
33
  tag_name, method_name = config.keys.first, config.values.first
29
34
  attrs = config[:attributes]
30
- options = config.merge({:xml_key_style => encoder_options[:xml_key_style]})
35
+ options = config.merge({:xml_key_style => encoder_options[:xml_key_style], :partial => partial, :path => path})
31
36
  if method_name.is_a?(Array)
32
- Tag.new(tag_name, attrs, method_name.map{|conf| build_tag(conf) }, options)
37
+ Tag.new(tag_name, attrs, method_name.map{|conf| build_tag(conf, partial, path.dup << tag_name) }, options)
33
38
  elsif config[:array]
34
39
  options.merge!(:array_method => method_name, :array_root => tag_name)
35
40
  child_root = config[:child_root] || tag_name.to_s.singularize
36
- tags = config[:array].map{|conf| build_tag(conf) }
41
+ tags = config[:array].map{|conf| build_tag(conf, partial, path.dup << tag_name) }
37
42
  TagArray.new(child_root, attrs, tags, options)
38
43
  else
39
44
  Tag.new(tag_name, attrs, Value.new(method_name), options)
data/lib/pump/xml/tag.rb CHANGED
@@ -68,15 +68,27 @@ module Pump
68
68
  def condition_end
69
69
  return unless conditional?
70
70
 
71
+ conditions = []
72
+
73
+ if options[:partial]
74
+ conditions << if options[:path] && options[:path].any?
75
+ "field_hash[:'#{options[:path].join('.')}']"
76
+ else
77
+ "field_hash[:#{name.to_s.underscore}]"
78
+ end
79
+ end
80
+
71
81
  if options[:if]
72
- "\" if object.#{options[:if]} }"
82
+ conditions << "object.#{options[:if]}"
73
83
  elsif options[:unless]
74
- "\" unless object.#{options[:unless]} }"
84
+ conditions << "!object.#{options[:unless]}"
75
85
  end
86
+
87
+ "\" if #{conditions.join(" && ")} }" if conditions.any?
76
88
  end
77
89
 
78
90
  def conditional?
79
- !!(options[:if] || options[:unless])
91
+ !!(options[:if] || options[:unless] || options[:partial])
80
92
  end
81
93
  end
82
94
  end
@@ -156,14 +156,14 @@ describe Pump::Json do
156
156
  end
157
157
 
158
158
  context "deep hash-like nesting" do
159
- let(:json) { Pump::Json.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}]}], :instruct => false) }
159
+ let(:json) { Pump::Json.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}]}]) }
160
160
 
161
161
  it "returns static string" do
162
162
  json.encode(person).should eql("{\"person\":{\"name\":\"Benny\",\"parent\":{\"name\":\"Benny\",\"age\":9}}}")
163
163
  end
164
164
 
165
165
  context "with static_value = nil" do
166
- let(:json) { Pump::Json.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}], :static_value => nil}], :instruct => false) }
166
+ let(:json) { Pump::Json.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}], :static_value => nil}]) }
167
167
 
168
168
  it "uses static value" do
169
169
  json.encode(person).should eql("{\"person\":{\"name\":\"Benny\",\"parent\":null}}")
@@ -171,7 +171,7 @@ describe Pump::Json do
171
171
  end
172
172
 
173
173
  context "with static_value = {}" do
174
- let(:json) { Pump::Json.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}], :static_value => {}}], :instruct => false) }
174
+ let(:json) { Pump::Json.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}], :static_value => {}}]) }
175
175
 
176
176
  it "uses static value" do
177
177
  json.encode(person).should eql("{\"person\":{\"name\":\"Benny\",\"parent\":{}}}")
@@ -187,7 +187,7 @@ describe Pump::Json do
187
187
  ]) }
188
188
 
189
189
  let(:json) { Pump::Json.new('person', [{:name => :name}, {:children => :children,
190
- :array => [{:name => :name}]}], :instruct => false) }
190
+ :array => [{:name => :name}]}]) }
191
191
 
192
192
  it "returns json string" do
193
193
  json.encode(person).should eql("{\"person\":{\"name\":\"Gustav\",\"children\":[{\"name\":\"Lilly\"},{\"name\":\"Lena\"}]}}")
@@ -195,7 +195,7 @@ describe Pump::Json do
195
195
 
196
196
  context "with static_value = nil" do
197
197
  let(:json) { Pump::Json.new('person', [{:name => :name}, {:children => :children,
198
- :array => [{:name => :name}], :static_value => nil}], :instruct => false) }
198
+ :array => [{:name => :name}], :static_value => nil}]) }
199
199
  it "uses static value" do
200
200
  json.encode(person).should eql("{\"person\":{\"name\":\"Gustav\",\"children\":[]}}")
201
201
  end
@@ -203,7 +203,7 @@ describe Pump::Json do
203
203
 
204
204
  context "with static_value = []" do
205
205
  let(:json) { Pump::Json.new('person', [{:name => :name}, {:children => :children,
206
- :array => [{:name => :name}], :static_value => []}], :instruct => false) }
206
+ :array => [{:name => :name}], :static_value => []}]) }
207
207
  it "uses static value" do
208
208
  json.encode(person).should eql("{\"person\":{\"name\":\"Gustav\",\"children\":[]}}")
209
209
  end
@@ -240,6 +240,57 @@ describe Pump::Json do
240
240
  it "returns json string without root" do
241
241
  json.encode(person, :exclude_root_in_json => true).should eql("{\"name\":\"Benny\"}")
242
242
  end
243
+
244
+ it "returns json string without root on array" do
245
+ json.encode([person], :exclude_root_in_json => true).should eql("[{\"name\":\"Benny\"}]")
246
+ end
247
+ end
248
+
249
+ context "with :fields option" do
250
+ let(:json) { Pump::Json.new('person', [
251
+ {:name => :name}, {:age => :age}, {:last_name => :last_name},
252
+ {:parent => [{:name => :name}, {:age => :age}]}
253
+ ])}
254
+
255
+ it "returns only specified fields" do
256
+ json.encode(person, :fields => ['name']).should eql("{\"person\":{\"name\":\"Benny\"}}")
257
+ json.encode(person, :fields => ['age']).should eql("{\"person\":{\"age\":9}}")
258
+ end
259
+
260
+ it "ignores unknown fields" do
261
+ json.encode(person, :fields => ['name', 'unknown']).should eql("{\"person\":{\"name\":\"Benny\"}}")
262
+ json.encode(person, :fields => ['unknown']).should eql("{\"person\":{}}")
263
+ end
264
+
265
+ it "accepts dasherized and underscored field names" do
266
+ json.encode(person, :fields => ['name', 'last-name']).should eql("{\"person\":{\"name\":\"Benny\",\"last_name\":\"Hellman\"}}")
267
+ json.encode(person, :fields => ['name', 'last_name']).should eql("{\"person\":{\"name\":\"Benny\",\"last_name\":\"Hellman\"}}")
268
+ end
269
+
270
+ context "deep hash-like nesting" do
271
+ it "adds all keys if fields contains parent" do
272
+ json.encode(person, :fields => ['name', 'parent']).should eql(
273
+ "{\"person\":{\"name\":\"Benny\",\"parent\":{\"name\":\"Benny\",\"age\":9}}}"
274
+ )
275
+ end
276
+ end
277
+
278
+ context "deep array-like nesting" do
279
+ let(:person) {
280
+ Struct.new(:name, :age, :children).new('Gustav', 1, [
281
+ Struct.new(:name, :age).new('Lilly', 2),
282
+ Struct.new(:name, :age).new('Lena', 3)
283
+ ]) }
284
+
285
+ let(:json) { Pump::Json.new('person', [{:name => :name}, {:age => :age}, {:children => :children,
286
+ :array => [{:name => :name}, {:age => :age}]}]) }
287
+
288
+ it "adds all keys if fields contains children" do
289
+ json.encode(person, :fields => ['name', 'children']).should eql(
290
+ "{\"person\":{\"name\":\"Gustav\",\"children\":[{\"name\":\"Lilly\",\"age\":2},{\"name\":\"Lena\",\"age\":3}]}}"
291
+ )
292
+ end
293
+ end
243
294
  end
244
295
  end
245
296
  end
@@ -332,5 +332,52 @@ describe Pump::Xml do
332
332
  end
333
333
  end
334
334
  end
335
+
336
+ context "with :fields option" do
337
+ let(:xml) { Pump::Xml.new('person', [
338
+ {:name => :name}, {:age => :age}, {:last_name => :last_name},
339
+ {:parent => [{:name => :name}, {:age => :age}]}
340
+ ])}
341
+
342
+ it "returns only specified fields" do
343
+ xml.encode(person, :fields => ['name']).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name>Benny</name>\n</person>\n")
344
+ xml.encode(person, :fields => ['age']).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <age>9</age>\n</person>\n")
345
+ end
346
+
347
+ it "ignores unknown fields" do
348
+ xml.encode(person, :fields => ['name', 'unknown']).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name>Benny</name>\n</person>\n")
349
+ xml.encode(person, :fields => ['unknown']).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n</person>\n")
350
+ end
351
+
352
+ it "accepts dasherized and underscored field names" do
353
+ xml.encode(person, :fields => ['name', 'last-name']).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name>Benny</name>\n <last-name>Hellman</last-name>\n</person>\n")
354
+ xml.encode(person, :fields => ['name', 'last_name']).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name>Benny</name>\n <last-name>Hellman</last-name>\n</person>\n")
355
+ end
356
+
357
+ context "deep hash-like nesting" do
358
+ it "adds all keys if fields contains parent" do
359
+ xml.encode(person, :fields => ['name', 'parent']).should eql(
360
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name>Benny</name>\n <parent>\n <name>Benny</name>\n <age>9</age>\n </parent>\n</person>\n"
361
+ )
362
+ end
363
+ end
364
+
365
+ context "deep array-like nesting" do
366
+ let(:person) {
367
+ Struct.new(:name, :age, :children).new('Gustav', 1, [
368
+ Struct.new(:name, :age).new('Lilly', 2),
369
+ Struct.new(:name, :age).new('Lena', 3)
370
+ ]) }
371
+
372
+ let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age}, {:children => :children,
373
+ :array => [{:name => :name}, {:age => :age}]}]) }
374
+
375
+ it "adds all keys if fields contains children" do
376
+ xml.encode(person, :fields => ['name', 'children']).should eql(
377
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n <name>Gustav</name>\n <children type=\"array\">\n <child>\n <name>Lilly</name>\n <age>2</age>\n </child>\n <child>\n <name>Lena</name>\n <age>3</age>\n </child>\n </children>\n</person>\n"
378
+ )
379
+ end
380
+ end
381
+ end
335
382
  end
336
383
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Munz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-26 00:00:00.000000000 Z
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport