pump 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +8 -1
- data/README.md +1 -1
- data/benchmarks/encode.rb +40 -3
- data/gemfiles/activesupport-3-2 +1 -0
- data/gemfiles/activesupport-4 +1 -0
- data/lib/pump.rb +1 -0
- data/lib/pump/array.rb +7 -1
- data/lib/pump/dsl.rb +57 -0
- data/lib/pump/encoder.rb +92 -0
- data/lib/pump/json.rb +97 -0
- data/lib/pump/object.rb +19 -3
- data/lib/pump/version.rb +1 -1
- data/lib/pump/xml.rb +14 -69
- data/pump.gemspec +3 -2
- data/spec/pump/array_spec.rb +34 -2
- data/spec/pump/{xml/dsl_spec.rb → dsl_spec.rb} +22 -22
- data/spec/pump/encoder_spec.rb +97 -0
- data/spec/pump/json_spec.rb +218 -0
- data/spec/pump/object_spec.rb +82 -3
- data/spec/pump/xml_spec.rb +0 -19
- metadata +27 -7
- data/lib/pump/xml/dsl.rb +0 -59
data/lib/pump/xml.rb
CHANGED
@@ -1,84 +1,25 @@
|
|
1
|
-
require "pump/
|
2
|
-
require "pump/xml/value"
|
3
|
-
require "pump/xml/tag_array"
|
4
|
-
require "pump/xml/dsl"
|
1
|
+
require "pump/encoder"
|
5
2
|
require 'active_support/core_ext/string/inflections'
|
6
3
|
|
7
4
|
module Pump
|
8
|
-
class Xml
|
9
|
-
|
10
|
-
attr_reader :root_tag_name, :tag_config, :options
|
11
|
-
|
12
|
-
# Creates a new XML-encoder with a root tag named after +root_tag_name+.
|
13
|
-
#
|
14
|
-
# @example Create a simple encoder for a person with a name attribute:
|
15
|
-
# Pump::Xml.new :person do
|
16
|
-
# tag :name
|
17
|
-
# end
|
18
|
-
#
|
19
|
-
# @example Create the same without usage of the DSL:
|
20
|
-
# Pump::Xml.new :person, [{:name => :name}]
|
21
|
-
#
|
22
|
-
# @example Create the same but without the xml instruct
|
23
|
-
# Pump::Xml.new :person, :instruct => false do
|
24
|
-
# tag :name
|
25
|
-
# end
|
26
|
-
#
|
27
|
-
# @example The same again without DSL:
|
28
|
-
#
|
29
|
-
# Pump::Xml.new :person, [{:name => :name}], :instruct => false
|
30
|
-
#
|
31
|
-
# @param [String, Symbol] root_tag_name the name of the used root tag
|
32
|
-
# @param [Array<Hash>] tag_config optional config for all tags
|
33
|
-
# @param [Hash] options optional options for the whole encoder
|
34
|
-
# @yield an optional block to create the encoder with the Pump::Xml::Dsl
|
35
|
-
#
|
36
|
-
# @return [self]
|
37
|
-
def initialize(root_tag_name, tag_config=nil, options={}, &blk)
|
38
|
-
unless Array === tag_config
|
39
|
-
raise ArgumentError unless block_given?
|
40
|
-
@options = tag_config || {}
|
41
|
-
@tag_config = Dsl.new(&blk).config
|
42
|
-
else
|
43
|
-
@tag_config = tag_config
|
44
|
-
@options = options
|
45
|
-
end
|
46
|
-
@root_tag_name = root_tag_name
|
47
|
-
|
48
|
-
compile
|
49
|
-
end
|
50
|
-
|
51
|
-
# Encode a object or an array of objects to an XML-string.
|
52
|
-
#
|
53
|
-
# @param [Object, Array<Object>] object object or an array of objects to
|
54
|
-
# encode to XML. The only requirement: The given objects must respond
|
55
|
-
# to all methods configured during initalization of the Pump::Xml instance.
|
56
|
-
#
|
57
|
-
# @return [String]
|
58
|
-
def encode(object)
|
59
|
-
object.is_a?(Array) ? encode_array(object) : encode_single(object)
|
60
|
-
end
|
5
|
+
class Xml < Pump::Encoder
|
61
6
|
|
62
7
|
private
|
63
8
|
|
64
|
-
def compile
|
65
|
-
instance_eval(compile_string)
|
66
|
-
end
|
67
|
-
|
68
9
|
def compile_string
|
69
10
|
<<-EOV
|
70
|
-
def encode_single(object)
|
71
|
-
"#{Tag.new(
|
11
|
+
def encode_single(object, options)
|
12
|
+
"#{Tag.new(root_name, {}, sub_tags, tag_options)}"
|
72
13
|
end
|
73
14
|
|
74
|
-
def encode_array(objects)
|
75
|
-
"#{TagArray.new(
|
15
|
+
def encode_array(objects, options)
|
16
|
+
"#{TagArray.new(root_name, {}, sub_tags, tag_options)}"
|
76
17
|
end
|
77
18
|
EOV
|
78
19
|
end
|
79
20
|
|
80
21
|
def sub_tags
|
81
|
-
|
22
|
+
encoder_config.map do |config|
|
82
23
|
build_tag(config)
|
83
24
|
end
|
84
25
|
end
|
@@ -97,11 +38,15 @@ module Pump
|
|
97
38
|
end
|
98
39
|
|
99
40
|
def tag_options
|
100
|
-
{:instruct => add_instruct?, :extra_indent =>
|
41
|
+
{:instruct => add_instruct?, :extra_indent => encoder_options[:extra_indent], :array_root => encoder_options[:array_root] }
|
101
42
|
end
|
102
43
|
|
103
44
|
def add_instruct?
|
104
|
-
|
45
|
+
encoder_options.has_key?(:instruct) ? encoder_options[:instruct] : true
|
105
46
|
end
|
106
47
|
end
|
107
|
-
end
|
48
|
+
end
|
49
|
+
|
50
|
+
require "pump/xml/tag"
|
51
|
+
require "pump/xml/value"
|
52
|
+
require "pump/xml/tag_array"
|
data/pump.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = Pump::VERSION
|
9
9
|
gem.authors = ["Sebastian Munz"]
|
10
10
|
gem.email = ["sebastian@yo.lk"]
|
11
|
-
gem.description = %q{Fast but inflexible XML encoding for ruby objects.}
|
12
|
-
gem.summary = %q{Fast but inflexible XML encoding for ruby objects.}
|
11
|
+
gem.description = %q{Fast but inflexible XML and JSON encoding for ruby objects.}
|
12
|
+
gem.summary = %q{Fast but inflexible XML and JSON encoding for ruby objects.}
|
13
13
|
gem.homepage = "https://github.com/yolk/pump"
|
14
14
|
gem.license = 'MIT'
|
15
15
|
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.require_paths = ["lib"]
|
20
20
|
|
21
21
|
gem.add_dependency 'activesupport'
|
22
|
+
gem.add_dependency 'oj'
|
22
23
|
gem.add_development_dependency 'rspec', '>= 2.12.0'
|
23
24
|
gem.add_development_dependency 'guard-rspec', '>=2.2.2'
|
24
25
|
end
|
data/spec/pump/array_spec.rb
CHANGED
@@ -4,6 +4,10 @@ class Array
|
|
4
4
|
def to_xml(options={})
|
5
5
|
"<Array#to_xml />"
|
6
6
|
end
|
7
|
+
|
8
|
+
def to_json(options={})
|
9
|
+
"{Array#to_json}"
|
10
|
+
end
|
7
11
|
end
|
8
12
|
|
9
13
|
class ArrayObjectWithoutInclude
|
@@ -39,11 +43,19 @@ describe Pump::Array do
|
|
39
43
|
[].respond_to?(:pump_to_xml).should eql(true)
|
40
44
|
end
|
41
45
|
|
46
|
+
it "should extend ::Array by default with pump_to_json" do
|
47
|
+
[].respond_to?(:pump_to_json).should eql(true)
|
48
|
+
end
|
49
|
+
|
42
50
|
context "with objects without include" do
|
43
51
|
subject{ [ArrayObjectWithoutInclude.new] }
|
44
52
|
|
45
53
|
it "should return default to_xml" do
|
46
|
-
subject.pump_to_xml.should eql(
|
54
|
+
subject.pump_to_xml.should eql(subject.to_xml)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should return default to_json" do
|
58
|
+
subject.pump_to_json.should eql(subject.to_json)
|
47
59
|
end
|
48
60
|
end
|
49
61
|
|
@@ -51,7 +63,11 @@ describe Pump::Array do
|
|
51
63
|
subject{ [ArrayObjectWithInclude.new] }
|
52
64
|
|
53
65
|
it "should return default to_xml" do
|
54
|
-
subject.pump_to_xml.should eql(
|
66
|
+
subject.pump_to_xml.should eql(subject.to_xml)
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return default to_json" do
|
70
|
+
subject.pump_to_json.should eql(subject.to_json)
|
55
71
|
end
|
56
72
|
end
|
57
73
|
|
@@ -69,5 +85,21 @@ describe Pump::Array do
|
|
69
85
|
it "should encode with default encoder on unknown set" do
|
70
86
|
subject.pump_to_xml(:set => :bla).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<array-objects type=\"array\">\n <array-object>\n <name>Tintin</name>\n </array-object>\n</array-objects>\n")
|
71
87
|
end
|
88
|
+
|
89
|
+
it "should encode json with default encoder" do
|
90
|
+
subject.pump_to_json.should eql("[{\"array_object\":{\"name\":\"Tintin\"}}]")
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should encode json with specified encoder" do
|
94
|
+
subject.pump_to_json(:set => :with_age).should eql("[{\"array_object\":{\"name\":\"Tintin\",\"age\":27}}]")
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should encode json with default encoder on unknown set" do
|
98
|
+
subject.pump_to_json(:set => :bla).should eql("[{\"array_object\":{\"name\":\"Tintin\"}}]")
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should pass down options to encoder" do
|
102
|
+
subject.pump_to_json(:exclude_root_in_json => true).should eql("[{\"name\":\"Tintin\"}]")
|
103
|
+
end
|
72
104
|
end
|
73
105
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Pump::
|
4
|
-
subject { Pump::
|
3
|
+
describe Pump::Dsl do
|
4
|
+
subject { Pump::Dsl.new {} }
|
5
5
|
|
6
6
|
describe ".new" do
|
7
7
|
it "requires one block" do
|
8
|
-
lambda{ Pump::
|
8
|
+
lambda{ Pump::Dsl.new }.should raise_error(ArgumentError)
|
9
9
|
lambda{ subject }.should_not raise_error
|
10
10
|
end
|
11
11
|
end
|
@@ -14,88 +14,88 @@ describe Pump::Xml::Dsl do
|
|
14
14
|
its(:config) { should eql([]) }
|
15
15
|
|
16
16
|
context "with tag" do
|
17
|
-
subject { Pump::
|
17
|
+
subject { Pump::Dsl.new { tag :name } }
|
18
18
|
its(:config) { should eql([{:name => :name}]) }
|
19
19
|
|
20
20
|
context "with attributes and options" do
|
21
|
-
subject { Pump::
|
21
|
+
subject { Pump::Dsl.new { tag :name, :attributes => {:a => 'b'}, :options => false } }
|
22
22
|
its(:config) { should eql([{:name => :name, :attributes => {:a => 'b'}, :options => false}]) }
|
23
23
|
end
|
24
24
|
|
25
25
|
context "with :from option" do
|
26
|
-
subject { Pump::
|
26
|
+
subject { Pump::Dsl.new { tag :name, :from => :method_name } }
|
27
27
|
its(:config) { should eql([{:name => :method_name}]) }
|
28
28
|
end
|
29
29
|
|
30
30
|
context "with dashs in tag name" do
|
31
|
-
subject { Pump::
|
31
|
+
subject { Pump::Dsl.new { tag :"first-name" } }
|
32
32
|
its(:config) { should eql([{:"first-name" => :first_name}]) }
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
context "with string" do
|
37
|
-
subject { Pump::
|
37
|
+
subject { Pump::Dsl.new { string :name } }
|
38
38
|
its(:config) { should eql([{:name => :name}]) }
|
39
39
|
|
40
40
|
context "with attributes and options" do
|
41
|
-
subject { Pump::
|
41
|
+
subject { Pump::Dsl.new { string :name, :attributes => {:a => 'b'}, :options => false } }
|
42
42
|
its(:config) { should eql([{:name => :name, :attributes => {:a => 'b'}, :options => false}]) }
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
46
|
context "with integer" do
|
47
|
-
subject { Pump::
|
47
|
+
subject { Pump::Dsl.new { integer :name } }
|
48
48
|
its(:config) { should eql([{:name => :name, :attributes => {:type => 'integer'}, :xmlsafe => true}]) }
|
49
49
|
|
50
50
|
context "with attributes and options" do
|
51
|
-
subject { Pump::
|
51
|
+
subject { Pump::Dsl.new { integer :name, :attributes => {:a => 'b'}, :options => false } }
|
52
52
|
its(:config) { should eql([{:name => :name, :attributes => {:type => 'integer', :a => 'b'}, :options => false, :xmlsafe => true}]) }
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
context "with float" do
|
57
|
-
subject { Pump::
|
57
|
+
subject { Pump::Dsl.new { float :name } }
|
58
58
|
its(:config) { should eql([{:name => :name, :attributes => {:type => 'float'}, :xmlsafe => true}]) }
|
59
59
|
|
60
60
|
context "with attributes and options" do
|
61
|
-
subject { Pump::
|
61
|
+
subject { Pump::Dsl.new { float :name, :attributes => {:a => 'b'}, :options => false } }
|
62
62
|
its(:config) { should eql([{:name => :name, :attributes => {:type => 'float', :a => 'b'}, :options => false, :xmlsafe => true}]) }
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
66
|
context "with boolean" do
|
67
|
-
subject { Pump::
|
67
|
+
subject { Pump::Dsl.new { boolean :name } }
|
68
68
|
its(:config) { should eql([{:name => :name, :attributes => {:type => 'boolean'}, :xmlsafe => true}]) }
|
69
69
|
|
70
70
|
context "with attributes and options" do
|
71
|
-
subject { Pump::
|
71
|
+
subject { Pump::Dsl.new { boolean :name, :attributes => {:a => 'b'}, :options => false } }
|
72
72
|
its(:config) { should eql([{:name => :name, :attributes => {:type => 'boolean', :a => 'b'}, :options => false, :xmlsafe => true}]) }
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
76
|
context "with date" do
|
77
|
-
subject { Pump::
|
77
|
+
subject { Pump::Dsl.new { date :at } }
|
78
78
|
its(:config) { should eql([{:at => :at, :attributes => {:type => 'date'}, :xmlsafe => true}]) }
|
79
79
|
|
80
80
|
context "with attributes and options" do
|
81
|
-
subject { Pump::
|
81
|
+
subject { Pump::Dsl.new { date :at, :attributes => {:a => 'b'}, :options => false } }
|
82
82
|
its(:config) { should eql([{:at => :at, :attributes => {:type => 'date', :a => 'b'}, :options => false, :xmlsafe => true}]) }
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
86
|
context "with (date)time" do
|
87
|
-
subject { Pump::
|
87
|
+
subject { Pump::Dsl.new { time :at } }
|
88
88
|
its(:config) { should eql([{:at => :at, :typecast => :xmlschema, :attributes => {:type => 'datetime'}, :xmlsafe => true}]) }
|
89
89
|
|
90
90
|
context "with attributes and options" do
|
91
|
-
subject { Pump::
|
91
|
+
subject { Pump::Dsl.new { time :at, :attributes => {:a => 'b'}, :options => false } }
|
92
92
|
its(:config) { should eql([{:at => :at, :attributes => {:a => 'b', :type => 'datetime'}, :options => false, :typecast => :xmlschema, :xmlsafe => true}]) }
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
96
|
context "with nested tags" do
|
97
97
|
subject do
|
98
|
-
Pump::
|
98
|
+
Pump::Dsl.new do
|
99
99
|
tag :person, :option => 'x' do
|
100
100
|
string :name
|
101
101
|
integer :age
|
@@ -115,7 +115,7 @@ describe Pump::Xml::Dsl do
|
|
115
115
|
|
116
116
|
context "with array tag" do
|
117
117
|
subject do
|
118
|
-
Pump::
|
118
|
+
Pump::Dsl.new do
|
119
119
|
array(:children) do
|
120
120
|
tag :name
|
121
121
|
end
|
@@ -128,7 +128,7 @@ describe Pump::Xml::Dsl do
|
|
128
128
|
|
129
129
|
context "with options" do
|
130
130
|
subject do
|
131
|
-
Pump::
|
131
|
+
Pump::Dsl.new do
|
132
132
|
array(:children, :from => :kids, :child_root => :ugly_kid_joe) do
|
133
133
|
tag :name
|
134
134
|
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Pump::Encoder do
|
4
|
+
describe ".new" do
|
5
|
+
it "requires two parameters or one and a block" do
|
6
|
+
lambda{ Pump::Encoder.new }.should raise_error(ArgumentError)
|
7
|
+
lambda{ Pump::Encoder.new('record') }.should raise_error(ArgumentError)
|
8
|
+
lambda{ Pump::Encoder.new('record', []) }.should_not raise_error
|
9
|
+
lambda{ Pump::Encoder.new('record') {} }.should_not raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "with block given" do
|
13
|
+
subject do
|
14
|
+
Pump::Encoder.new('human', :instruct => false) do
|
15
|
+
tag :name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
its(:encoder_config) { should eql([{:name=>:name}])}
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "with base given in options" do
|
23
|
+
let(:base) { Pump::Encoder.new('person', [{:name => :name}, {:role => :role}], {:option => true}) }
|
24
|
+
|
25
|
+
context "adding other param" do
|
26
|
+
subject{ Pump::Encoder.new('person', [{:age => :age}], :base => base) }
|
27
|
+
its(:encoder_config) { should eql([{:name => :name}, {:role => :role}, {:age => :age}])}
|
28
|
+
its(:encoder_options) { should eql({:option => true}) }
|
29
|
+
its(:base) { should eql(base) }
|
30
|
+
|
31
|
+
it "leaves base config untouched" do
|
32
|
+
subject
|
33
|
+
base.encoder_config.should eql([{:name => :name}, {:role => :role}])
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "adding other param with block" do
|
38
|
+
subject{ Pump::Encoder.new('person', :base => base) { tag :age } }
|
39
|
+
its(:encoder_config) { should eql([{:name => :name}, {:role => :role}, {:age => :age}])}
|
40
|
+
|
41
|
+
it "leaves base config untouched" do
|
42
|
+
subject
|
43
|
+
base.encoder_config.should eql([{:name => :name}, {:role => :role}])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "alter exisiting param" do
|
48
|
+
subject{ Pump::Encoder.new('person', [{:name => :full_name}], :base => base) }
|
49
|
+
its(:encoder_config) { should eql([{:name => :full_name}, {:role => :role}])}
|
50
|
+
|
51
|
+
it "leaves base config untouched" do
|
52
|
+
subject
|
53
|
+
base.encoder_config.should eql([{:name => :name}, {:role => :role}])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "alter exisiting param with block" do
|
58
|
+
subject do
|
59
|
+
Pump::Encoder.new('person', :base => base) do
|
60
|
+
string :last_name
|
61
|
+
tag :role, :static_value => nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
its(:encoder_config) { should eql([{:name => :name}, {:role=>:role, :static_value=>nil}, {:last_name => :last_name}])}
|
65
|
+
|
66
|
+
it "leaves base config untouched" do
|
67
|
+
subject
|
68
|
+
base.encoder_config.should eql([{:name => :name}, {:role => :role}])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "adding an option" do
|
73
|
+
context "adding other param" do
|
74
|
+
subject{ Pump::Encoder.new('person', [], {:other_option => false, :base => base}) }
|
75
|
+
its(:encoder_options) { should eql({:other_option => false, :option => true}) }
|
76
|
+
|
77
|
+
it "leaves base options untouched" do
|
78
|
+
subject
|
79
|
+
base.encoder_options.should eql({:option => true})
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "adding and overwriting an option" do
|
85
|
+
context "adding other param" do
|
86
|
+
subject{ Pump::Encoder.new('person', [], {:option => 47, :other_option => false, :base => base}) }
|
87
|
+
its(:encoder_options) { should eql({:other_option => false, :option => 47}) }
|
88
|
+
|
89
|
+
it "leaves base options untouched" do
|
90
|
+
subject
|
91
|
+
base.encoder_options.should eql({:option => true})
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,218 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Pump::Json do
|
4
|
+
describe "#encode" do
|
5
|
+
let(:person) { Struct.new(:name, :age, :last_name).new('Benny', 9, 'Hellman') }
|
6
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}]) }
|
7
|
+
|
8
|
+
it "requires one object" do
|
9
|
+
lambda{ json.encode }.should raise_error(ArgumentError)
|
10
|
+
lambda{ json.encode(person) }.should_not raise_error
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns json string" do
|
14
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Benny\"}}")
|
15
|
+
end
|
16
|
+
|
17
|
+
context "with array" do
|
18
|
+
|
19
|
+
context "with one entry" do
|
20
|
+
let(:people) { [person] }
|
21
|
+
|
22
|
+
it "returns json string" do
|
23
|
+
json.encode(people).should eql("[{\"person\":{\"name\":\"Benny\"}}]")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "with multiple entries" do
|
28
|
+
let(:people) { [person, Struct.new(:name, :age).new('Carlo', 5)] }
|
29
|
+
|
30
|
+
it "returns xml string" do
|
31
|
+
json.encode(people).should eql("[{\"person\":{\"name\":\"Benny\"}},{\"person\":{\"name\":\"Carlo\"}}]")
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with exclude_root_in_json option" do
|
35
|
+
it "returns json string without root" do
|
36
|
+
json.encode(people, :exclude_root_in_json => true).should eql("[{\"name\":\"Benny\"},{\"name\":\"Carlo\"}]")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with empty array" do
|
42
|
+
let(:people) { [] }
|
43
|
+
|
44
|
+
it "returns xml string" do
|
45
|
+
json.encode(people).should eql("[]")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "with blank name" do
|
51
|
+
let(:person) { Struct.new(:name, :age).new('', 9) }
|
52
|
+
|
53
|
+
it do
|
54
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"\"}}")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "with nil name" do
|
59
|
+
let(:person) { Struct.new(:name, :age).new(nil, 9) }
|
60
|
+
|
61
|
+
it do
|
62
|
+
json.encode(person).should eql("{\"person\":{\"name\":null}}")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "with conditionals" do
|
67
|
+
let(:person) { Struct.new(:name, :age, :is_young, :is_old).new('Gorbatschow', 82, false, true) }
|
68
|
+
|
69
|
+
context "simple if" do
|
70
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:age => :age, :if => :is_young}]) }
|
71
|
+
|
72
|
+
it "skips key-value on false" do
|
73
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Gorbatschow\"}}")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "simple unless" do
|
78
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:age => :age, :unless => :is_old}]) }
|
79
|
+
|
80
|
+
it "skips key-value on false" do
|
81
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Gorbatschow\"}}")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "chained" do
|
86
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:age => :age, :unless => 'age.nil?'}]) }
|
87
|
+
let(:people) { [person, Struct.new(:name, :age).new('Schewardnadse', nil)] }
|
88
|
+
|
89
|
+
it "skips key-value on false" do
|
90
|
+
json.encode(people).should eql("[{\"person\":{\"name\":\"Gorbatschow\",\"age\":82}},{\"person\":{\"name\":\"Schewardnadse\"}}]")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "with static_value set" do
|
96
|
+
let(:person) { Struct.new(:name, :age, :is_yount).new('Gorbatschow', 82, false) }
|
97
|
+
|
98
|
+
context "replace with other value" do
|
99
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:age => :age, :static_value => 12}]) }
|
100
|
+
|
101
|
+
it "returns given static_value" do
|
102
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Gorbatschow\",\"age\":12}}")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "replace with nil value" do
|
107
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:age => :age, :static_value => nil}]) }
|
108
|
+
|
109
|
+
it "returns given static_value" do
|
110
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Gorbatschow\",\"age\":null}}")
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "replace with other value but with failed condition" do
|
115
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:age => :age, :static_value => 12, :if => :is_yount}]) }
|
116
|
+
|
117
|
+
it "returns given static_value" do
|
118
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Gorbatschow\"}}")
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "replace with other value but with succssful condition" do
|
123
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:age => :age, :static_value => 12, :unless => :is_yount}]) }
|
124
|
+
|
125
|
+
it "returns given static_value" do
|
126
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Gorbatschow\",\"age\":12}}")
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "deep hash-like nesting" do
|
132
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}]}], :instruct => false) }
|
133
|
+
|
134
|
+
it "returns static string" do
|
135
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Benny\",\"parent\":{\"name\":\"Benny\",\"age\":9}}}")
|
136
|
+
end
|
137
|
+
|
138
|
+
context "with static_value = nil" do
|
139
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}], :static_value => nil}], :instruct => false) }
|
140
|
+
|
141
|
+
it "uses static value" do
|
142
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Benny\",\"parent\":null}}")
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context "with static_value = {}" do
|
147
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}], :static_value => {}}], :instruct => false) }
|
148
|
+
|
149
|
+
it "uses static value" do
|
150
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Benny\",\"parent\":{}}}")
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context "deep array-like nesting" do
|
156
|
+
let(:person) {
|
157
|
+
Struct.new(:name, :children).new('Gustav', [
|
158
|
+
Struct.new(:name).new('Lilly'),
|
159
|
+
Struct.new(:name).new('Lena')
|
160
|
+
]) }
|
161
|
+
|
162
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:children => :children,
|
163
|
+
:array => [{:name => :name}]}], :instruct => false) }
|
164
|
+
|
165
|
+
it "returns json string" do
|
166
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Gustav\",\"children\":[{\"name\":\"Lilly\"},{\"name\":\"Lena\"}]}}")
|
167
|
+
end
|
168
|
+
|
169
|
+
context "with static_value = nil" do
|
170
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:children => :children,
|
171
|
+
:array => [{:name => :name}], :static_value => nil}], :instruct => false) }
|
172
|
+
it "uses static value" do
|
173
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Gustav\",\"children\":[]}}")
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context "with static_value = []" do
|
178
|
+
let(:json) { Pump::Json.new('person', [{:name => :name}, {:children => :children,
|
179
|
+
:array => [{:name => :name}], :static_value => []}], :instruct => false) }
|
180
|
+
it "uses static value" do
|
181
|
+
json.encode(person).should eql("{\"person\":{\"name\":\"Gustav\",\"children\":[]}}")
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context "with underscore option" do
|
187
|
+
context "not set" do
|
188
|
+
let(:json) { Pump::Json.new('my-person', [{"first-name" => :name}]) }
|
189
|
+
|
190
|
+
it "returns json string with underscores" do
|
191
|
+
json.encode(person).should eql("{\"my_person\":{\"first_name\":\"Benny\"}}")
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
context "set to false" do
|
196
|
+
let(:json) { Pump::Json.new('my-person', [{"first-name" => :name}], :underscore => false) }
|
197
|
+
|
198
|
+
it "returns json string with dashes" do
|
199
|
+
json.encode(person).should eql("{\"my-person\":{\"first-name\":\"Benny\"}}")
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context "set to true" do
|
204
|
+
let(:json) { Pump::Json.new('my-person', [{"first-name" => :name}], :underscore => true) }
|
205
|
+
|
206
|
+
it "returns json string with underscores" do
|
207
|
+
json.encode(person).should eql("{\"my_person\":{\"first_name\":\"Benny\"}}")
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context "with exclude_root_in_json option" do
|
213
|
+
it "returns json string without root" do
|
214
|
+
json.encode(person, :exclude_root_in_json => true).should eql("{\"name\":\"Benny\"}")
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|