pump 0.6.0 → 0.6.1

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: c4b95096c96dfa59274e8b007a7946f0223e507c
4
- data.tar.gz: 12e6c47f2239231db115c160575870739141ef95
3
+ metadata.gz: 928aa1adcc5c2a7fc5dd530840b2ceb48c60e872
4
+ data.tar.gz: 7b80bb36b960b06a57ce69f3c4e9aea5cefe8862
5
5
  SHA512:
6
- metadata.gz: 9725fb7b0bb3152bae3321623744f15e280bfc4edae242836702b3971bd69039007f7c54bc8b1e7a49f49b0d440f7d66db989642b64ad2b8e09858ff1d32e210
7
- data.tar.gz: 360c20c887a7c50232df40b77805e86a26d8a8c399695fba4e360121eb721841768918f4738ac6fe957b75bac8d0c161b19eebdee27ebdd95707d45d7ccb758b
6
+ metadata.gz: cac714cccc025776bcef431bf8d47707f1ca3e588162807171b478d5583092dc19bdae802ee6bcc3906c430c50baa23c1092a193ce4ada0091e64d60723b3850
7
+ data.tar.gz: c32c95412ebf0b979b212d4596f48b268228f93bf8eb5a45fa0ea8e04a3ddbd26ec876358bb813dbd9d8488ed2166702221362c32f121e66b57e2554ad3adb47
data/CHANGES.md CHANGED
@@ -1,6 +1,14 @@
1
1
  ### dev
2
2
 
3
- [full changelog](http://github.com/yolk/pump/compare/v0.0.1...master)
3
+ [full changelog](http://github.com/yolk/pump/compare/v0.6.1...master)
4
+
5
+ ### 0.6.1 / 2013-07-25
6
+
7
+ [full changelog](http://github.com/yolk/valvat/compare/v0.6.0...v0.6.1)
8
+
9
+ * Fixed issues with time/date objects in JSON
10
+ * XML defaults now to dasherized key names, JSON to underscores
11
+ * Added :xml_key_style & :json_key_style options
4
12
 
5
13
  ### 0.6.0 / 2013-07-24
6
14
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Pump
2
2
 
3
+ [![Build Status](https://travis-ci.org/yolk/pump.png?branch=master)](https://travis-ci.org/yolk/pump)
4
+
3
5
  Fast but inflexible XML and JSON encoding for ruby objects.
4
6
 
5
7
  ## Quick benchmark
@@ -7,7 +9,11 @@ Fast but inflexible XML and JSON encoding for ruby objects.
7
9
  Serializing an array of 100 random entries 1.000 times.
8
10
 
9
11
  user system total real
10
- Pump::Xml#encode 1.450000 0.010000 1.460000 ( 1.466962)
11
- Pump::Xml#encode (optimized) 1.100000 0.010000 1.110000 ( 1.110234)
12
- Ox 1.490000 0.000000 1.490000 ( 1.483275)
13
- ActiveModel#serialize 23.280000 0.060000 23.340000 ( 23.333999)
12
+ Pump::Json#encode 0.600000 0.010000 0.610000 ( 0.603469)
13
+ Pump::Xml#encode 1.140000 0.020000 1.160000 ( 1.162651)
14
+ Pump::Xml#encode (optimized) 0.830000 0.020000 0.850000 ( 0.858750)
15
+ Ox 1.200000 0.010000 1.210000 ( 1.203765)
16
+ Oj 0.560000 0.000000 0.560000 ( 0.566166)
17
+ Yajl 1.490000 0.000000 1.490000 ( 1.493032)
18
+ ActiveModel#to_xml 24.110000 0.050000 24.160000 ( 24.170127)
19
+ ActiveModel#to_json 3.860000 0.010000 3.870000 ( 3.866130)
data/lib/pump/json.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "pump/encoder"
2
+ require 'active_support/json/encoding'
2
3
  require "oj"
3
4
 
4
5
  module Pump
@@ -90,7 +91,7 @@ module Pump
90
91
  end
91
92
 
92
93
  def format_name(name)
93
- return name if encoder_options[:underscore] == false
94
+ return name.to_s.dasherize if encoder_options[:json_key_style] == :dashes
94
95
  name.to_s.underscore
95
96
  end
96
97
  end
data/lib/pump/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pump
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
data/lib/pump/xml/node.rb CHANGED
@@ -24,6 +24,7 @@ module Pump
24
24
  def add_node(node)
25
25
  node.level = level + 1
26
26
  node.options[:extra_indent] = options[:extra_indent]
27
+ node.options[:xml_key_style] = options[:xml_key_style]
27
28
  nodes << node
28
29
  end
29
30
 
@@ -38,6 +39,11 @@ module Pump
38
39
  def extra_indent
39
40
  options[:extra_indent] || 0
40
41
  end
42
+
43
+ def format_name(name)
44
+ return name.to_s.underscore if options[:xml_key_style] == :underscores
45
+ name.to_s.dasherize
46
+ end
41
47
  end
42
48
  end
43
49
  end
data/lib/pump/xml/tag.rb CHANGED
@@ -29,7 +29,7 @@ module Pump
29
29
  end
30
30
 
31
31
  def open_tag
32
- "#{prefix}<#{name}#{attributes_string}"
32
+ "#{prefix}<#{format_name(name)}#{attributes_string}"
33
33
  end
34
34
 
35
35
  def prefix
@@ -42,7 +42,7 @@ module Pump
42
42
 
43
43
  def value_and_close_tag(path=nil)
44
44
  value = value_nodes? ? nodes.first.to_s(path) : ("\n" << nodes.map(&:to_s).join)
45
- ">#{value}#{tabs unless value_nodes?}</#{name}>\n"
45
+ ">#{value}#{tabs unless value_nodes?}</#{format_name(name)}>\n"
46
46
  end
47
47
 
48
48
  def value_and_close_tag_with_nil_check
@@ -51,7 +51,7 @@ module Pump
51
51
 
52
52
  def static_value_and_close_tag
53
53
  return " nil=\\\"true\\\"/>\n" if options[:static_value].nil?
54
- ">#{options[:static_value]}</#{name}>\n"
54
+ ">#{options[:static_value]}</#{format_name(name)}>\n"
55
55
  end
56
56
 
57
57
  def attributes_string
@@ -5,16 +5,19 @@ module Pump
5
5
  class Xml
6
6
  class TagArray < Node
7
7
  def initialize(name, attributes={}, nodes=[], options={})
8
- tag = Tag.new(name, attributes, nodes, {:level => 1, :extra_indent => options[:extra_indent]})
8
+ tag = Tag.new(name, attributes, nodes, {
9
+ :level => 1, :extra_indent => options[:extra_indent],
10
+ :xml_key_style => options[:xml_key_style]
11
+ })
9
12
  array_root = options[:array_root] || name.to_s.pluralize
10
13
  super(array_root, {}, [tag], options)
11
14
  end
12
15
 
13
16
  def to_s
14
17
  if options.has_key?(:static_value)
15
- "#{prefix}<#{name} type=\\\"array\\\"#{static_value_and_close_tag}"
18
+ "#{prefix}<#{format_name name} type=\\\"array\\\"#{static_value_and_close_tag}"
16
19
  else
17
- "#{prefix}<#{name} type=\\\"array\\\"#{loop_and_close_tag}"
20
+ "#{prefix}<#{format_name name} type=\\\"array\\\"#{loop_and_close_tag}"
18
21
  end
19
22
  end
20
23
 
@@ -25,12 +28,12 @@ module Pump
25
28
  end
26
29
 
27
30
  def loop_and_close_tag
28
- "\#{ #{objects_path}.empty? ? \"/>\n\" : \">\n#{tag_loop}#{tabs}</#{name}>\n\" }"
31
+ "\#{ #{objects_path}.empty? ? \"/>\n\" : \">\n#{tag_loop}#{tabs}</#{format_name name}>\n\" }"
29
32
  end
30
33
 
31
34
  def static_value_and_close_tag
32
35
  return "/>\n" if options[:static_value].nil?
33
- ">#{options[:static_value]}</#{name}>\n"
36
+ ">#{options[:static_value]}</#{format_name name}>\n"
34
37
  end
35
38
 
36
39
  def objects_path
data/lib/pump/xml.rb CHANGED
@@ -27,18 +27,25 @@ module Pump
27
27
  def build_tag(config)
28
28
  tag_name, method_name = config.keys.first, config.values.first
29
29
  attrs = config[:attributes]
30
+ options = config.merge({:xml_key_style => encoder_options[:xml_key_style]})
30
31
  if method_name.is_a?(Array)
31
- Tag.new(tag_name, attrs, method_name.map{|conf| build_tag(conf) }, config)
32
+ Tag.new(tag_name, attrs, method_name.map{|conf| build_tag(conf) }, options)
32
33
  elsif config[:array]
33
- config.merge!(:array_method => method_name, :array_root => tag_name)
34
- TagArray.new(config[:child_root] || tag_name.to_s.singularize, attrs, config[:array].map{|conf| build_tag(conf) }, config)
34
+ options.merge!(:array_method => method_name, :array_root => tag_name)
35
+ child_root = config[:child_root] || tag_name.to_s.singularize
36
+ tags = config[:array].map{|conf| build_tag(conf) }
37
+ TagArray.new(child_root, attrs, tags, options)
35
38
  else
36
- Tag.new(tag_name, attrs, Value.new(method_name), config)
39
+ Tag.new(tag_name, attrs, Value.new(method_name), options)
37
40
  end
38
41
  end
39
42
 
40
43
  def tag_options
41
- {:instruct => add_instruct?, :extra_indent => encoder_options[:extra_indent], :array_root => encoder_options[:array_root] }
44
+ {
45
+ :instruct => add_instruct?, :extra_indent => encoder_options[:extra_indent],
46
+ :array_root => encoder_options[:array_root],
47
+ :xml_key_style => encoder_options[:xml_key_style]
48
+ }
42
49
  end
43
50
 
44
51
  def add_instruct?
data/pump.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'pump/version'
4
+ require "pump/version"
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "pump"
@@ -11,15 +11,15 @@ Gem::Specification.new do |gem|
11
11
  gem.description = %q{Fast but inflexible XML and JSON encoding for ruby objects.}
12
12
  gem.summary = %q{Fast but inflexible XML and JSON encoding for ruby objects.}
13
13
  gem.homepage = "https://github.com/yolk/pump"
14
- gem.license = 'MIT'
14
+ gem.license = "MIT"
15
15
 
16
16
  gem.files = `git ls-files`.split($/)
17
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_dependency 'activesupport'
22
- gem.add_dependency 'oj'
23
- gem.add_development_dependency 'rspec', '>= 2.12.0'
24
- gem.add_development_dependency 'guard-rspec', '>=2.2.2'
21
+ gem.add_dependency "activesupport", ">=3.2"
22
+ gem.add_dependency "oj", ">=2.0"
23
+ gem.add_development_dependency "rspec", ">= 2.12.0"
24
+ gem.add_development_dependency "guard-rspec", ">=2.2.2"
25
25
  end
@@ -14,6 +14,33 @@ describe Pump::Json do
14
14
  json.encode(person).should eql("{\"person\":{\"name\":\"Benny\"}}")
15
15
  end
16
16
 
17
+ context "with time object" do
18
+ let(:person) { Struct.new(:born).new(Time.new(2007,11,1,15,25,0, "+09:00")) }
19
+ let(:json) { Pump::Json.new('person', [{:born => :born}]) }
20
+
21
+ it "formats time as iso string" do
22
+ json.encode(person).should eql("{\"person\":{\"born\":\"2007-11-01T15:25:00+09:00\"}}")
23
+ end
24
+ end
25
+
26
+ context "with date object" do
27
+ let(:person) { Struct.new(:born).new(Date.new(2007,11,1)) }
28
+ let(:json) { Pump::Json.new('person', [{:born => :born}]) }
29
+
30
+ it "formats time as iso string" do
31
+ json.encode(person).should eql("{\"person\":{\"born\":\"2007-11-01\"}}")
32
+ end
33
+ end
34
+
35
+ context "with datetime object" do
36
+ let(:person) { Struct.new(:born).new(DateTime.new(2007,11,1,15,25,0, "+09:00")) }
37
+ let(:json) { Pump::Json.new('person', [{:born => :born}]) }
38
+
39
+ it "formats time as iso string" do
40
+ json.encode(person).should eql("{\"person\":{\"born\":\"2007-11-01T15:25:00+09:00\"}}")
41
+ end
42
+ end
43
+
17
44
  context "with array" do
18
45
 
19
46
  context "with one entry" do
@@ -183,7 +210,7 @@ describe Pump::Json do
183
210
  end
184
211
  end
185
212
 
186
- context "with underscore option" do
213
+ context "with :json_key_style option" do
187
214
  context "not set" do
188
215
  let(:json) { Pump::Json.new('my-person', [{"first-name" => :name}]) }
189
216
 
@@ -192,16 +219,16 @@ describe Pump::Json do
192
219
  end
193
220
  end
194
221
 
195
- context "set to false" do
196
- let(:json) { Pump::Json.new('my-person', [{"first-name" => :name}], :underscore => false) }
222
+ context "set to :dashes" do
223
+ let(:json) { Pump::Json.new('my-person', [{"first-name" => :name}], :json_key_style => :dashes) }
197
224
 
198
225
  it "returns json string with dashes" do
199
226
  json.encode(person).should eql("{\"my-person\":{\"first-name\":\"Benny\"}}")
200
227
  end
201
228
  end
202
229
 
203
- context "set to true" do
204
- let(:json) { Pump::Json.new('my-person', [{"first-name" => :name}], :underscore => true) }
230
+ context "set to :underscores" do
231
+ let(:json) { Pump::Json.new('my-person', [{"first-name" => :name}], :json_key_style => :underscores) }
205
232
 
206
233
  it "returns json string with underscores" do
207
234
  json.encode(person).should eql("{\"my_person\":{\"first_name\":\"Benny\"}}")
@@ -209,7 +236,7 @@ describe Pump::Json do
209
236
  end
210
237
  end
211
238
 
212
- context "with exclude_root_in_json option" do
239
+ context "with :exclude_root_in_json option" do
213
240
  it "returns json string without root" do
214
241
  json.encode(person, :exclude_root_in_json => true).should eql("{\"name\":\"Benny\"}")
215
242
  end
@@ -111,7 +111,7 @@ describe Pump::Object do
111
111
  end
112
112
 
113
113
  it "should return xml on pump_to_xml" do
114
- subject.new.pump_to_xml.should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my_object>\n <name>MyName</name>\n</my_object>\n")
114
+ subject.new.pump_to_xml.should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my-object>\n <name>MyName</name>\n</my-object>\n")
115
115
  end
116
116
 
117
117
  it "should return json on pump_to_json" do
@@ -131,15 +131,15 @@ describe Pump::Object do
131
131
  end
132
132
 
133
133
  it "should return default xml on pump_to_xml" do
134
- subject.new.pump_to_xml.should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my_object>\n <name>MyName</name>\n</my_object>\n")
134
+ subject.new.pump_to_xml.should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my-object>\n <name>MyName</name>\n</my-object>\n")
135
135
  end
136
136
 
137
137
  it "should return special xml on set option" do
138
- subject.new.pump_to_xml(:set => :sometimes).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my_object>\n <name>MyName</name>\n <age type=\"integer\">72</age>\n</my_object>\n")
138
+ subject.new.pump_to_xml(:set => :sometimes).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my-object>\n <name>MyName</name>\n <age type=\"integer\">72</age>\n</my-object>\n")
139
139
  end
140
140
 
141
141
  it "should return default xml on pump_to_xml with unknown set option" do
142
- subject.new.pump_to_xml(:set => :unknown).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my_object>\n <name>MyName</name>\n</my_object>\n")
142
+ subject.new.pump_to_xml(:set => :unknown).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my-object>\n <name>MyName</name>\n</my-object>\n")
143
143
  end
144
144
 
145
145
  it "should return default json on pump_to_json" do
@@ -163,7 +163,7 @@ describe Pump::Object do
163
163
  end
164
164
 
165
165
  it "should return default xml on pump_to_xml" do
166
- subject.new.pump_to_xml.should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my_object>\n <name>MyName</name>\n <role>my_role</role>\n</my_object>\n")
166
+ subject.new.pump_to_xml.should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my-object>\n <name>MyName</name>\n <role>my_role</role>\n</my-object>\n")
167
167
  end
168
168
 
169
169
  it "should return default json on pump_to_json" do
@@ -171,7 +171,7 @@ describe Pump::Object do
171
171
  end
172
172
 
173
173
  it "should return special inherited xml on pump_to_xml(:set => :restricted)" do
174
- subject.new.pump_to_xml(:set => :restricted).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my_object>\n <name>MyName</name>\n <role>basic_role</role>\n <age type=\"integer\">72</age>\n</my_object>\n")
174
+ subject.new.pump_to_xml(:set => :restricted).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my-object>\n <name>MyName</name>\n <role>basic_role</role>\n <age type=\"integer\">72</age>\n</my-object>\n")
175
175
  end
176
176
 
177
177
  it "should return special inherited json on pump_to_json(:set => :restricted)" do
@@ -283,5 +283,54 @@ describe Pump::Xml do
283
283
  end
284
284
  end
285
285
  end
286
+
287
+ context "with :xml_key_style option" do
288
+ context "not set" do
289
+ let(:xml) { Pump::Xml.new('my_person', [{"first_name" => :name}]) }
290
+
291
+ it "returns xml string with dashes" do
292
+ xml.encode(person).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my-person>\n <first-name>Benny</first-name>\n</my-person>\n")
293
+ end
294
+ end
295
+
296
+ context "set to :dashes" do
297
+ let(:xml) { Pump::Xml.new('my_person', [{"first-name" => :name}], :xml_key_style => :dashes) }
298
+
299
+ it "returns xml string with dashes" do
300
+ xml.encode(person).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my-person>\n <first-name>Benny</first-name>\n</my-person>\n")
301
+ end
302
+ end
303
+
304
+ context "set to :underscores" do
305
+ let(:xml) { Pump::Xml.new('my_person', [{"first-name" => :name}], :xml_key_style => :underscores) }
306
+
307
+ it "returns xml string with underscores" do
308
+ xml.encode(person).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my_person>\n <first_name>Benny</first_name>\n</my_person>\n")
309
+ end
310
+
311
+ context "on complex array like" do
312
+ let(:person) {
313
+ Struct.new(:name, :children).new('Gustav', [
314
+ Struct.new(:name).new('Lilly'),
315
+ Struct.new(:name).new('Lena')
316
+ ]) }
317
+
318
+ let(:xml) { Pump::Xml.new('my-person', [{:"my-name" => :name}, {:"the-children" => :children,
319
+ :array => [{:"child-name" => :name}]}], :xml_key_style => :underscores) }
320
+
321
+ it "returns xml string with underscores" do
322
+ xml.encode(person).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<my_person>\n <my_name>Gustav</my_name>\n <the_children type=\"array\">\n <the_child>\n <child_name>Lilly</child_name>\n </the_child>\n <the_child>\n <child_name>Lena</child_name>\n </the_child>\n </the_children>\n</my_person>\n")
323
+ end
324
+ end
325
+
326
+ context "on complex array like" do
327
+ let(:xml) { Pump::Xml.new('a-person', [{:"my-name" => :name}, {:"the-parent" => [{:"a-name" => :name}, {:"la-age" => :age}]}], :xml_key_style => :underscores) }
328
+
329
+ it "returns xml string with underscores" do
330
+ xml.encode(person).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<a_person>\n <my_name>Benny</my_name>\n <the_parent>\n <a_name>Benny</a_name>\n <la_age>9</la_age>\n </the_parent>\n</a_person>\n")
331
+ end
332
+ end
333
+ end
334
+ end
286
335
  end
287
336
  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.0
4
+ version: 0.6.1
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-24 00:00:00.000000000 Z
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '3.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
- version: '0'
26
+ version: '3.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: oj
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement