pump 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b9056ee092ab29fe87298579710bd70071d6a6d
4
+ data.tar.gz: ef775b68abb7303644d320ff315f23e643817576
5
+ SHA512:
6
+ metadata.gz: 29d00d8d8999018fa340ec2c6e77569dbfabb60419cce2437a89290fdd93f46bfdd7e5ed431724498305f8e97ce32d84bf4d99cca13abf004321d04b1d6b4d8c
7
+ data.tar.gz: 633002231a25894b1512e764e0513c5dc054b46be7e4fae8ff19a5d4cdd1c091f934778519fd125a1bfa059cc2dbe01c2ac3fac9f2461abe8ed64137c55bd6c6
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p247
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 2.0.0
3
+ - 1.9.3
4
+ gemfile:
5
+ - gemfiles/activesupport-4
6
+ - gemfiles/activesupport-3-2
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activemodel", "3.2.13"
4
+
5
+ gem 'rspec', '>= 2.4.0'
6
+ gem 'rake'
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activemodel", "4.0"
4
+
5
+ gem 'rspec', '>= 2.4.0'
6
+ gem 'rake'
data/lib/pump/array.rb ADDED
@@ -0,0 +1,20 @@
1
+ module Pump
2
+ module Array
3
+ def pump_to_xml(options={})
4
+ encoder = get_pump_encoder(options[:set], :xml)
5
+ return to_xml(options) unless encoder
6
+ encoder.encode(self)
7
+ end
8
+
9
+ private
10
+
11
+ def get_pump_encoder(set, format)
12
+ return if empty? || !first.class.respond_to?(:pumps)
13
+ first.class.pumps.get(set, format)
14
+ end
15
+ end
16
+ end
17
+
18
+ class ::Array
19
+ include Pump::Array
20
+ end
@@ -0,0 +1,21 @@
1
+ module Pump
2
+ class Collection
3
+ def initialize
4
+ @pumps = {}
5
+ end
6
+
7
+ def add(set, format, value)
8
+ @pumps[format] ||= {}
9
+ @pumps[format][set || :default] = value
10
+ end
11
+
12
+ def get(set, format)
13
+ pumps = @pumps[format]
14
+ pumps && (pumps[set] || pumps[:default])
15
+ end
16
+
17
+ def size
18
+ @pumps.values.map(&:size).inject(0) {|sum, it| sum += it; it}
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ require 'pump/collection'
2
+ require 'pump/xml'
3
+ require 'active_support/concern'
4
+
5
+ module Pump
6
+ module Object
7
+ extend ActiveSupport::Concern
8
+
9
+ def pump_to_xml(options={})
10
+ encoder = self.class.pumps.get(options[:set], :xml)
11
+ if encoder
12
+ encoder.encode(self)
13
+ else
14
+ self.to_xml(options)
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ def pumps
20
+ @pumps ||= Pump::Collection.new
21
+ end
22
+
23
+ def add_pump(name, set=nil, options={}, &block)
24
+ pumps.add(set, :xml, Pump::Xml.new(name, options, &block))
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/pump/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pump
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/pump.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "pump/version"
2
2
  require "pump/xml"
3
+ require "pump/object"
4
+ require "pump/array"
3
5
 
4
6
  # Fast but inflexible XML encoding for ruby objects.
5
7
  module Pump
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ class Array
4
+ def to_xml(options={})
5
+ "<Array#to_xml />"
6
+ end
7
+ end
8
+
9
+ class ArrayObjectWithoutInclude
10
+ end
11
+
12
+ class ArrayObjectWithInclude
13
+ include Pump::Object
14
+ end
15
+
16
+ class ArrayObject
17
+ include Pump::Object
18
+
19
+ add_pump 'array-object' do
20
+ string :name
21
+ end
22
+
23
+ add_pump 'array-object', :with_age do
24
+ string :name
25
+ integer :age
26
+ end
27
+
28
+ def name
29
+ "Tintin"
30
+ end
31
+
32
+ def age
33
+ 27
34
+ end
35
+ end
36
+
37
+ describe Pump::Array do
38
+ it "should extend ::Array by default with pump_to_xml" do
39
+ [].respond_to?(:pump_to_xml).should eql(true)
40
+ end
41
+
42
+ context "with objects without include" do
43
+ subject{ [ArrayObjectWithoutInclude.new] }
44
+
45
+ it "should return default to_xml" do
46
+ subject.pump_to_xml.should eql("<Array#to_xml />")
47
+ end
48
+ end
49
+
50
+ context "with objects with include but without encoders" do
51
+ subject{ [ArrayObjectWithInclude.new] }
52
+
53
+ it "should return default to_xml" do
54
+ subject.pump_to_xml.should eql("<Array#to_xml />")
55
+ end
56
+ end
57
+
58
+ context "with objects with include and encoders" do
59
+ subject{ [ArrayObject.new] }
60
+
61
+ it "should encode with default encoder" do
62
+ subject.pump_to_xml.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")
63
+ end
64
+
65
+ it "should encode with specified encoder" do
66
+ subject.pump_to_xml(:set => :with_age).should eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<array-objects type=\"array\">\n <array-object>\n <name>Tintin</name>\n <age type=\"integer\">27</age>\n </array-object>\n</array-objects>\n")
67
+ end
68
+
69
+ it "should encode with default encoder on unknown set" do
70
+ 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
+ end
72
+ end
73
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pump::Collection do
4
+ subject { Pump::Collection.new }
5
+
6
+ describe ".new" do
7
+ it "should not accept any arguments" do
8
+ subject
9
+ lambda{ Pump::Collection.new("") }.should raise_error(ArgumentError)
10
+ end
11
+
12
+ its(:size) { should eql(0) }
13
+ end
14
+
15
+ describe "#add" do
16
+ it "should add entry" do
17
+ subject.add(:set_name, :xml, :value)
18
+ subject.size.should eql(1)
19
+ end
20
+
21
+ it "should allow different formats" do
22
+ subject.add(:set_name, :json, :value)
23
+ subject.size.should eql(1)
24
+ end
25
+
26
+ it "should default to :default set" do
27
+ subject.add(nil, :xml, :value)
28
+ subject.get(:default, :xml).should eql(:value)
29
+ end
30
+ end
31
+
32
+ describe "#get" do
33
+ it "should return nil on unknown entry" do
34
+ subject.get(:set_name, :xml).should eql(nil)
35
+ subject.get(:default, :xml).should eql(nil)
36
+ subject.get(nil, :xml).should eql(nil)
37
+ subject.get(:default, :json).should eql(nil)
38
+ end
39
+
40
+ context "when entries are added" do
41
+ before{ subject.add(:set_name, :xml, :value); subject.add(:default, :xml, :value2) }
42
+
43
+ it "should return given value" do
44
+ subject.get(:set_name, :xml).should eql(:value)
45
+ subject.get(:default, :xml).should eql(:value2)
46
+ end
47
+
48
+ it "should return nil with wrong format" do
49
+ subject.get(:set_name, :json).should eql(nil)
50
+ subject.get(:default, :json).should eql(nil)
51
+ end
52
+
53
+ it "should default to :default on unknwon set" do
54
+ subject.get(:unknown, :xml).should eql(:value2)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ class ObjectWithoutInclude
4
+
5
+ end
6
+
7
+ class ObjectWithInclude
8
+ include Pump::Object
9
+
10
+ def to_xml(options={})
11
+ "<to_xml />"
12
+ end
13
+ end
14
+
15
+ class ObjectWithIncludeAndPumps
16
+ include Pump::Object
17
+
18
+ add_pump :my_object do
19
+ string "name"
20
+ end
21
+
22
+ def name
23
+ "MyName"
24
+ end
25
+ end
26
+
27
+ class ObjectWithIncludeAndMultiplePumps
28
+ include Pump::Object
29
+
30
+ add_pump :my_object do
31
+ string "name"
32
+ end
33
+
34
+ add_pump :my_object, :sometimes do
35
+ string "name"
36
+ integer "age"
37
+ end
38
+
39
+ def name
40
+ "MyName"
41
+ end
42
+
43
+ def age
44
+ 72
45
+ end
46
+ end
47
+
48
+ describe Pump::Object do
49
+ it "should not extend all objects by default" do
50
+ ObjectWithoutInclude.respond_to?(:pumps).should eql(false)
51
+ end
52
+
53
+ context "when included" do
54
+ subject { ObjectWithInclude }
55
+
56
+ it "should add pumps class method" do
57
+ subject.respond_to?(:pumps).should eql(true)
58
+ subject.pumps.size.should eql(0)
59
+ end
60
+
61
+ it "should add pump_to_xml instance method" do
62
+ subject.new.respond_to?(:pump_to_xml).should eql(true)
63
+ end
64
+
65
+ it "should fall back to original to_xml on pump_to_xml" do
66
+ subject.new.pump_to_xml.should eql("<to_xml />")
67
+ end
68
+ end
69
+
70
+ context "when included with one encoder added" do
71
+ subject { ObjectWithIncludeAndPumps }
72
+
73
+ it "should add pump" do
74
+ subject.pumps.size.should eql(1)
75
+ end
76
+
77
+ it "should return xml on pump_to_xml" do
78
+ 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
+ end
80
+ end
81
+
82
+ context "when included with multiple encoders added" do
83
+ subject { ObjectWithIncludeAndMultiplePumps }
84
+
85
+ it "should add pumps" do
86
+ subject.pumps.size.should eql(2)
87
+ end
88
+
89
+ it "should return default xml on pump_to_xml" do
90
+ 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")
91
+ end
92
+
93
+ it "should return special xml on set option" do
94
+ 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")
95
+ end
96
+
97
+ it "should return default xml on pump_to_xml with unknown set option" do
98
+ 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
+ end
100
+
101
+ end
102
+ end
@@ -158,11 +158,11 @@ describe Pump::Xml do
158
158
  end
159
159
 
160
160
  context "with datetime attribute" do
161
- let(:person) { Struct.new(:at).new(Time.new(2013, 2, 7, 0, 0, 0)) }
161
+ let(:person) { Struct.new(:at).new(Time.utc(2013, 2, 7, 0, 0, 0)) }
162
162
  let(:xml) { Pump::Xml.new('person', [{:at => :at, :typecast => :xmlschema, :attributes => {:type => 'datetime'}}]) }
163
163
 
164
164
  it "returns xml string" do
165
- xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <at type=\"datetime\">2013-02-07T00:00:00+01:00</at>\n</person>\n")
165
+ xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <at type=\"datetime\">2013-02-07T00:00:00Z</at>\n</person>\n")
166
166
  end
167
167
 
168
168
  context "but nil" do
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,5 @@ RSpec.configure do |config|
7
7
  config.run_all_when_everything_filtered = true
8
8
  config.filter_run :focus
9
9
  config.order = 'random'
10
-
11
- config.backtrace_clean_patterns = [/rspec\/(core|expectations)/]
10
+ config.backtrace_exclusion_patterns = [/rspec\/(core|expectations)/]
12
11
  end
metadata CHANGED
@@ -1,62 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
5
- prerelease:
4
+ version: 0.5.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sebastian Munz
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-13 00:00:00.000000000 Z
11
+ date: 2013-07-23 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: 2.12.0
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: 2.12.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: guard-rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: 2.2.2
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: 2.2.2
62
55
  description: Fast but inflexible XML encoding for ruby objects.
@@ -67,9 +60,9 @@ extensions: []
67
60
  extra_rdoc_files: []
68
61
  files:
69
62
  - .gitignore
70
- - .rbenv-gemsets
71
- - .rbenv-version
72
63
  - .rspec
64
+ - .ruby-version
65
+ - .travis.yml
73
66
  - .yardopts
74
67
  - CHANGES.md
75
68
  - Gemfile
@@ -78,7 +71,12 @@ files:
78
71
  - README.md
79
72
  - Rakefile
80
73
  - benchmarks/encode.rb
74
+ - gemfiles/activesupport-3-2
75
+ - gemfiles/activesupport-4
81
76
  - lib/pump.rb
77
+ - lib/pump/array.rb
78
+ - lib/pump/collection.rb
79
+ - lib/pump/object.rb
82
80
  - lib/pump/version.rb
83
81
  - lib/pump/xml.rb
84
82
  - lib/pump/xml/dsl.rb
@@ -87,6 +85,9 @@ files:
87
85
  - lib/pump/xml/tag_array.rb
88
86
  - lib/pump/xml/value.rb
89
87
  - pump.gemspec
88
+ - spec/pump/array_spec.rb
89
+ - spec/pump/collection_spec.rb
90
+ - spec/pump/object_spec.rb
90
91
  - spec/pump/xml/dsl_spec.rb
91
92
  - spec/pump/xml/tag_array_spec.rb
92
93
  - spec/pump/xml/tag_spec.rb
@@ -96,33 +97,34 @@ files:
96
97
  homepage: https://github.com/yolk/pump
97
98
  licenses:
98
99
  - MIT
100
+ metadata: {}
99
101
  post_install_message:
100
102
  rdoc_options: []
101
103
  require_paths:
102
104
  - lib
103
105
  required_ruby_version: !ruby/object:Gem::Requirement
104
- none: false
105
106
  requirements:
106
- - - ! '>='
107
+ - - '>='
107
108
  - !ruby/object:Gem::Version
108
109
  version: '0'
109
110
  required_rubygems_version: !ruby/object:Gem::Requirement
110
- none: false
111
111
  requirements:
112
- - - ! '>='
112
+ - - '>='
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  requirements: []
116
116
  rubyforge_project:
117
- rubygems_version: 1.8.23
117
+ rubygems_version: 2.0.3
118
118
  signing_key:
119
- specification_version: 3
119
+ specification_version: 4
120
120
  summary: Fast but inflexible XML encoding for ruby objects.
121
121
  test_files:
122
+ - spec/pump/array_spec.rb
123
+ - spec/pump/collection_spec.rb
124
+ - spec/pump/object_spec.rb
122
125
  - spec/pump/xml/dsl_spec.rb
123
126
  - spec/pump/xml/tag_array_spec.rb
124
127
  - spec/pump/xml/tag_spec.rb
125
128
  - spec/pump/xml/value_spec.rb
126
129
  - spec/pump/xml_spec.rb
127
130
  - spec/spec_helper.rb
128
- has_rdoc:
data/.rbenv-gemsets DELETED
@@ -1 +0,0 @@
1
- plugin
data/.rbenv-version DELETED
@@ -1 +0,0 @@
1
- 1.9.3-p286