pump 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- class ObjectWithoutInclude
4
-
5
- end
3
+ class ObjectWithoutInclude;end
6
4
 
7
5
  class ObjectWithInclude
8
6
  include Pump::Object
@@ -10,6 +8,10 @@ class ObjectWithInclude
10
8
  def to_xml(options={})
11
9
  "<to_xml />"
12
10
  end
11
+
12
+ def to_json(options={})
13
+ "{to_json}"
14
+ end
13
15
  end
14
16
 
15
17
  class ObjectWithIncludeAndPumps
@@ -45,6 +47,32 @@ class ObjectWithIncludeAndMultiplePumps
45
47
  end
46
48
  end
47
49
 
50
+ class ObjectWithIncludeAndMultiplePumpsWithInheritance
51
+ include Pump::Object
52
+
53
+ add_pump :my_object do
54
+ string "name"
55
+ string "role"
56
+ end
57
+
58
+ add_pump :my_object, :restricted, :base => :default do
59
+ integer "age"
60
+ string "role", :static_value => "basic_role"
61
+ end
62
+
63
+ def name
64
+ "MyName"
65
+ end
66
+
67
+ def role
68
+ "my_role"
69
+ end
70
+
71
+ def age
72
+ 72
73
+ end
74
+ end
75
+
48
76
  describe Pump::Object do
49
77
  it "should not extend all objects by default" do
50
78
  ObjectWithoutInclude.respond_to?(:pumps).should eql(false)
@@ -62,9 +90,17 @@ describe Pump::Object do
62
90
  subject.new.respond_to?(:pump_to_xml).should eql(true)
63
91
  end
64
92
 
93
+ it "should add pump_to_json instance method" do
94
+ subject.new.respond_to?(:pump_to_json).should eql(true)
95
+ end
96
+
65
97
  it "should fall back to original to_xml on pump_to_xml" do
66
98
  subject.new.pump_to_xml.should eql("<to_xml />")
67
99
  end
100
+
101
+ it "should fall back to original to_json on pump_to_xml" do
102
+ subject.new.pump_to_json.should eql("{to_json}")
103
+ end
68
104
  end
69
105
 
70
106
  context "when included with one encoder added" do
@@ -77,6 +113,14 @@ describe Pump::Object do
77
113
  it "should return xml on pump_to_xml" do
78
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")
79
115
  end
116
+
117
+ it "should return json on pump_to_json" do
118
+ subject.new.pump_to_json.should eql("{\"my_object\":{\"name\":\"MyName\"}}")
119
+ end
120
+
121
+ it "should pass down options to encoder" do
122
+ subject.new.pump_to_json(:exclude_root_in_json => true).should eql("{\"name\":\"MyName\"}")
123
+ end
80
124
  end
81
125
 
82
126
  context "when included with multiple encoders added" do
@@ -98,5 +142,40 @@ describe Pump::Object do
98
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")
99
143
  end
100
144
 
145
+ it "should return default json on pump_to_json" do
146
+ subject.new.pump_to_json.should eql("{\"my_object\":{\"name\":\"MyName\"}}")
147
+ end
148
+
149
+ it "should return special json on set option" do
150
+ subject.new.pump_to_json(:set => :sometimes).should eql("{\"my_object\":{\"name\":\"MyName\",\"age\":72}}")
151
+ end
152
+
153
+ it "should return default json on pump_to_json with unknown set option" do
154
+ subject.new.pump_to_json(:set => :unknown).should eql("{\"my_object\":{\"name\":\"MyName\"}}")
155
+ end
156
+ end
157
+
158
+ context "when included with multiple encoders added with inheritance" do
159
+ subject { ObjectWithIncludeAndMultiplePumpsWithInheritance }
160
+
161
+ it "should add pumps" do
162
+ subject.pumps.size.should eql(2)
163
+ end
164
+
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")
167
+ end
168
+
169
+ it "should return default json on pump_to_json" do
170
+ subject.new.pump_to_json.should eql("{\"my_object\":{\"name\":\"MyName\",\"role\":\"my_role\"}}")
171
+ end
172
+
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")
175
+ end
176
+
177
+ it "should return special inherited json on pump_to_json(:set => :restricted)" do
178
+ subject.new.pump_to_json(:set => :restricted).should eql("{\"my_object\":{\"name\":\"MyName\",\"role\":\"basic_role\",\"age\":72}}")
179
+ end
101
180
  end
102
181
  end
@@ -1,25 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Pump::Xml do
4
- describe ".new" do
5
- it "requires two parameters or one and a block" do
6
- lambda{ Pump::Xml.new }.should raise_error(ArgumentError)
7
- lambda{ Pump::Xml.new('record') }.should raise_error(ArgumentError)
8
- lambda{ Pump::Xml.new('record', []) }.should_not raise_error
9
- lambda{ Pump::Xml.new('record') {} }.should_not raise_error
10
- end
11
-
12
- describe "with block given" do
13
- subject do
14
- Pump::Xml.new('human', :instruct => false) do
15
- tag :name
16
- end.encode(Struct.new(:name).new('Artur'))
17
- end
18
-
19
- its(:encode) { should eql("<human>\n <name>Artur</name>\n</human>\n")}
20
- end
21
- end
22
-
23
4
  describe "#encode" do
24
5
  let(:person) { Struct.new(:name, :age, :last_name).new('Benny', 9, 'Hellman') }
25
6
  let(:xml) { Pump::Xml.new('person', [{:name => :name}]) }
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.5.1
4
+ version: 0.6.0
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-23 00:00:00.000000000 Z
11
+ date: 2013-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,7 +66,7 @@ dependencies:
52
66
  - - '>='
53
67
  - !ruby/object:Gem::Version
54
68
  version: 2.2.2
55
- description: Fast but inflexible XML encoding for ruby objects.
69
+ description: Fast but inflexible XML and JSON encoding for ruby objects.
56
70
  email:
57
71
  - sebastian@yo.lk
58
72
  executables: []
@@ -76,10 +90,12 @@ files:
76
90
  - lib/pump.rb
77
91
  - lib/pump/array.rb
78
92
  - lib/pump/collection.rb
93
+ - lib/pump/dsl.rb
94
+ - lib/pump/encoder.rb
95
+ - lib/pump/json.rb
79
96
  - lib/pump/object.rb
80
97
  - lib/pump/version.rb
81
98
  - lib/pump/xml.rb
82
- - lib/pump/xml/dsl.rb
83
99
  - lib/pump/xml/node.rb
84
100
  - lib/pump/xml/tag.rb
85
101
  - lib/pump/xml/tag_array.rb
@@ -87,8 +103,10 @@ files:
87
103
  - pump.gemspec
88
104
  - spec/pump/array_spec.rb
89
105
  - spec/pump/collection_spec.rb
106
+ - spec/pump/dsl_spec.rb
107
+ - spec/pump/encoder_spec.rb
108
+ - spec/pump/json_spec.rb
90
109
  - spec/pump/object_spec.rb
91
- - spec/pump/xml/dsl_spec.rb
92
110
  - spec/pump/xml/tag_array_spec.rb
93
111
  - spec/pump/xml/tag_spec.rb
94
112
  - spec/pump/xml/value_spec.rb
@@ -117,12 +135,14 @@ rubyforge_project:
117
135
  rubygems_version: 2.0.3
118
136
  signing_key:
119
137
  specification_version: 4
120
- summary: Fast but inflexible XML encoding for ruby objects.
138
+ summary: Fast but inflexible XML and JSON encoding for ruby objects.
121
139
  test_files:
122
140
  - spec/pump/array_spec.rb
123
141
  - spec/pump/collection_spec.rb
142
+ - spec/pump/dsl_spec.rb
143
+ - spec/pump/encoder_spec.rb
144
+ - spec/pump/json_spec.rb
124
145
  - spec/pump/object_spec.rb
125
- - spec/pump/xml/dsl_spec.rb
126
146
  - spec/pump/xml/tag_array_spec.rb
127
147
  - spec/pump/xml/tag_spec.rb
128
148
  - spec/pump/xml/value_spec.rb
data/lib/pump/xml/dsl.rb DELETED
@@ -1,59 +0,0 @@
1
- module Pump
2
- class Xml
3
- class Dsl
4
- def initialize(&blk)
5
- raise ArgumentError unless block_given?
6
- instance_eval(&blk)
7
- end
8
-
9
- def config
10
- @config ||= []
11
- end
12
-
13
- private
14
-
15
- def tag(name, options={}, &blk)
16
- method = if block_given?
17
- self.class.new(&blk).config
18
- else
19
- options.delete(:from) || (name.to_s =~ /-/ ? name.to_s.gsub('-', '_').to_sym : name)
20
- end
21
- config << ({ name => method }).merge(options)
22
- end
23
- alias_method :string, :tag
24
-
25
- def integer(name, options={})
26
- with_type('integer', name, options)
27
- end
28
-
29
- def float(name, options={})
30
- with_type('float', name, options)
31
- end
32
-
33
- def boolean(name, options={})
34
- with_type('boolean', name, options)
35
- end
36
-
37
- def date(name, options={})
38
- with_type('date', name, options)
39
- end
40
-
41
- def datetime(name, options={})
42
- options[:typecast] = :xmlschema unless options.has_key?(:typecast)
43
- with_type('datetime', name, options)
44
- end
45
- alias_method :time, :datetime
46
-
47
- def with_type(type, name, options)
48
- (options[:attributes] ||= {}).merge!({:type => type})
49
- options[:xmlsafe] = true unless options.has_key?(:xmlsafe)
50
- tag(name, options)
51
- end
52
-
53
- def array(name, options={}, &blk)
54
- options[:array] = self.class.new(&blk).config
55
- tag(name, options)
56
- end
57
- end
58
- end
59
- end