pump 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rbenv-gemsets ADDED
@@ -0,0 +1 @@
1
+ plugin
data/.rbenv-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3-p286
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/CHANGES.md ADDED
@@ -0,0 +1,3 @@
1
+ ### dev
2
+
3
+ [full changelog](http://github.com/yolk/pump/compare/v0.0.1...master)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pump.gemspec
4
+ gemspec
5
+
6
+ gem 'rb-fsevent' if RUBY_PLATFORM.include?("x86_64-darwin")
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^spec/.+_spec\.rb})
3
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sebastian
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Pump
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pump'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pump
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module Pump
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,34 @@
1
+ module Pump
2
+ class Xml
3
+ class Node
4
+ attr_reader :name, :attributes, :nodes, :options
5
+ attr_writer :level
6
+
7
+ def initialize(name, attributes={}, nodes=[], options={})
8
+ @name = name
9
+ @attributes = attributes || {}
10
+ @options = options || {}
11
+ @nodes = []
12
+ Array(nodes).each{|node| add_node(node) }
13
+ end
14
+
15
+ def to_s
16
+ end
17
+
18
+ private
19
+
20
+ def add_node(node)
21
+ node.level = level + 1
22
+ nodes << node
23
+ end
24
+
25
+ def level
26
+ @level || options[:level] || 0
27
+ end
28
+
29
+ def indent
30
+ (level)*(options[:indent] || 2)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,74 @@
1
+ require 'pump/xml/node'
2
+
3
+ module Pump
4
+ class Xml
5
+ class Tag < Node
6
+ INSTRUCT = "<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\n"
7
+
8
+ def initialize(*args)
9
+ super
10
+ if value_nodes?
11
+ nodes.first.options = options
12
+ end
13
+ end
14
+
15
+ def to_s
16
+ if !value_nodes? || options[:never_blank]
17
+ "#{open_tag}#{value_and_close_tag}"
18
+ else
19
+ "#{open_tag}\#{v = #{nodes.first.plain};''}#{nil_attribute}\#{#{value_and_close_tag_with_blank_check}}"
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def value_nodes?
26
+ Value === nodes.first
27
+ end
28
+
29
+ def open_tag
30
+ "#{prefix}<#{name}#{attributes_string}"
31
+ end
32
+
33
+ def prefix
34
+ if level == 0
35
+ options[:instruct] ? INSTRUCT : ""
36
+ else
37
+ "\n#{" "*indent}"
38
+ end
39
+ end
40
+
41
+ def value_and_close_tag(path=nil)
42
+ value = value_nodes? ? nodes.first.to_s(path) : (nodes.map(&:to_s).join << "\n")
43
+ ">#{value}</#{name}>"
44
+ end
45
+
46
+ def value_and_close_tag_with_blank_check
47
+ "#{blank_check} ? #{close_blank_tag} : \"#{value_and_close_tag('v')}\""
48
+ end
49
+
50
+ def attributes_string
51
+ return "" if !attributes || attributes.size == 0
52
+ attributes.inject('') do |str, (key, value)|
53
+ str << " #{key}=\\\"#{value}\\\""
54
+ end
55
+ end
56
+
57
+ def nil_attribute
58
+ "\#{\" nil=\\\"true\\\"\" if v.nil?}" if options[:nil_check]
59
+ end
60
+
61
+ def blank_check
62
+ if respond_to?(:blank?)
63
+ "v.blank?"
64
+ else
65
+ "v.nil? || v == ''"
66
+ end
67
+ end
68
+
69
+ def close_blank_tag
70
+ "\"/>\""
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,17 @@
1
+ require 'pump/xml/node'
2
+
3
+ module Pump
4
+ class Xml
5
+ class Value < Node
6
+ attr_accessor :options
7
+
8
+ def plain
9
+ "object.#{name}"
10
+ end
11
+
12
+ def to_s(plain_path=nil)
13
+ "\#{#{plain_path || plain}#{'.to_s.encode(:xml => :text)' unless options[:skip_encoding]}}"
14
+ end
15
+ end
16
+ end
17
+ end
data/lib/pump/xml.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "pump/xml/tag"
2
+ require "pump/xml/value"
3
+
4
+ module Pump
5
+ class Xml
6
+ def initialize(root_name, tags)
7
+ @root_name = root_name
8
+ @tags = tags
9
+ build
10
+ end
11
+
12
+ def serialize(object)
13
+ # "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<#{@root_name}>\n <name>#{record.name}</name>\n</#{@root_name}>"
14
+ end
15
+
16
+ private
17
+
18
+ def build
19
+ self.instance_eval build_string
20
+ end
21
+
22
+ def build_string
23
+ root_node = Tag.new(@root_name, {}, tags, {:instruct => true})
24
+ <<-EOV
25
+ def serialize(object)
26
+ "#{root_node}"
27
+ end
28
+ EOV
29
+ end
30
+
31
+ def tags
32
+ @tags.map do |options|
33
+ tag_name, method_name = options.keys.first, options.values.first
34
+ Tag.new(tag_name, options[:attributes], Value.new(method_name), options)
35
+ end
36
+ end
37
+ end
38
+ end
data/lib/pump.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "pump/version"
2
+ require "pump/xml"
3
+
4
+ module Pump
5
+ end
data/pump.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pump/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "pump"
8
+ gem.version = Pump::VERSION
9
+ gem.authors = ["Sebastian Munz"]
10
+ gem.email = ["sebastian@yo.lk"]
11
+ gem.description = %q{Fast but inflexible XML dumping for ruby objects.}
12
+ gem.summary = %q{Fast but inflexible XML dumping for ruby objects.}
13
+ gem.homepage = "https://github.com/yolk/pump"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rspec', '>= 2.12.0'
21
+ gem.add_development_dependency 'guard-rspec', '>=2.2.2'
22
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pump::Xml::Tag do
4
+ describe ".new" do
5
+ it "requires one parameter" do
6
+ lambda{ Pump::Xml::Tag.new }.should raise_error(ArgumentError)
7
+ lambda{ Pump::Xml::Tag.new(0) }.should_not raise_error
8
+ end
9
+ end
10
+
11
+ describe ".to_s" do
12
+ context "with value node(s)" do
13
+ let(:attributes) { {} }
14
+ let(:options) { {} }
15
+ subject{ Pump::Xml::Tag.new("tag", attributes, Pump::Xml::Value.new('method'), options).to_s }
16
+
17
+ it {should eql(
18
+ "<tag\#{v = object.method;''}\#{v.nil? || v == '' ? \"/>\" : \">\#{v.to_s.encode(:xml => :text)}</tag>\"}"
19
+ )}
20
+
21
+ context "with :nil_check => true" do
22
+ let(:options) { {:nil_check => true} }
23
+
24
+ it {should eql(
25
+ "<tag\#{v = object.method;''}\#{\" nil=\\\"true\\\"\" if v.nil?}\#{v.nil? || v == '' ? \"/>\" : \">\#{v.to_s.encode(:xml => :text)}</tag>\"}"
26
+ )}
27
+ end
28
+
29
+ context "with :never_blank => true" do
30
+ let(:options) { {:never_blank => true} }
31
+
32
+ it {should eql(
33
+ "<tag>\#{object.method.to_s.encode(:xml => :text)}</tag>"
34
+ )}
35
+
36
+ context "with attributes" do
37
+ let(:attributes) { {:foo => "bar"} }
38
+
39
+ it {should eql(
40
+ "<tag foo=\\\"bar\\\">\#{object.method.to_s.encode(:xml => :text)}</tag>"
41
+ )}
42
+ end
43
+ end
44
+
45
+ context "with :skip_encoding => true" do
46
+ let(:options) { {:skip_encoding => true} }
47
+
48
+ it {should eql(
49
+ "<tag\#{v = object.method;''}\#{v.nil? || v == '' ? \"/>\" : \">\#{v}</tag>\"}"
50
+ )}
51
+ end
52
+
53
+ context "with attributes" do
54
+ let(:attributes) { {:foo => "bar"} }
55
+
56
+ it {should eql(
57
+ "<tag foo=\\\"bar\\\"\#{v = object.method;''}\#{v.nil? || v == '' ? \"/>\" : \">\#{v.to_s.encode(:xml => :text)}</tag>\"}"
58
+ )}
59
+ end
60
+ end
61
+
62
+ context "with other tag child nodes" do
63
+ let(:attributes1) { {} }
64
+ let(:options1) { {} }
65
+ let(:attributes2) { {} }
66
+ let(:options2) { {:never_blank => true, :skip_encoding => true} }
67
+ let(:tag2) { Pump::Xml::Tag.new("child", attributes2, Pump::Xml::Value.new('method'), options2) }
68
+ let(:tag1) { Pump::Xml::Tag.new("root", attributes1, [tag2], options1) }
69
+ subject{ tag1.to_s }
70
+
71
+ it {should eql(
72
+ "<root>\n <child>\#{object.method}</child>\n</root>"
73
+ )}
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pump::Xml::Value do
4
+ subject { Pump::Xml::Value.new("method_name") }
5
+
6
+ describe ".new" do
7
+ it "requires one parameter" do
8
+ lambda{ Pump::Xml::Value.new }.should raise_error(ArgumentError)
9
+ lambda{ subject }.should_not raise_error
10
+ end
11
+ end
12
+
13
+ describe "#plain" do
14
+ its(:plain) { should eql("object.method_name") }
15
+ end
16
+
17
+ describe "#to_s" do
18
+ its(:to_s) { should eql("\#{object.method_name.to_s.encode(:xml => :text)}") }
19
+
20
+ context "with option :skip_encoding => true" do
21
+ subject { Pump::Xml::Value.new("method_name", {}, [], :skip_encoding => true) }
22
+
23
+ its(:to_s) { should eql("\#{object.method_name}") }
24
+ end
25
+
26
+ context "with path name" do
27
+ it do
28
+ subject.to_s('custom_path').should eql("\#{custom_path.to_s.encode(:xml => :text)}")
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pump::Xml do
4
+ describe ".new" do
5
+ it "requires two parameters" 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
+ end
10
+ end
11
+
12
+ describe "#serialize" do
13
+ let(:person) { Struct.new(:name, :age).new('Benny', 9) }
14
+ let(:xml) { Pump::Xml.new('person', [{:name => :name}]) }
15
+
16
+ it "requires one object" do
17
+ lambda{ xml.serialize }.should raise_error(ArgumentError)
18
+ lambda{ xml.serialize(person) }.should_not raise_error
19
+ end
20
+
21
+ it "returns xml string" do
22
+ xml.serialize(person).should eql("#{XML_INSTRUCT}<person>\n <name>Benny</name>\n</person>")
23
+ end
24
+
25
+ context "with attribute" do
26
+ let(:xml) do
27
+ Pump::Xml.new('person', [
28
+ {:name => :name},
29
+ {:age => :age, :attributes => {:type => :integer}}
30
+ ])
31
+ end
32
+
33
+ it do
34
+ xml.serialize(person).should eql("#{XML_INSTRUCT}<person>\n <name>Benny</name>\n <age type=\"integer\">9</age>\n</person>")
35
+ end
36
+
37
+ end
38
+
39
+ context "with blank name" do
40
+ let(:person) { Struct.new(:name, :age).new('', 9) }
41
+
42
+ it do
43
+ xml.serialize(person).should eql("#{XML_INSTRUCT}<person>\n <name/>\n</person>")
44
+ end
45
+ end
46
+
47
+ context "with nil name" do
48
+ let(:person) { Struct.new(:name, :age).new(nil, 9) }
49
+
50
+ it do
51
+ xml.serialize(person).should eql("#{XML_INSTRUCT}<person>\n <name/>\n</person>")
52
+ end
53
+
54
+ context "and with :nil_check => true" do
55
+ let(:xml) { Pump::Xml.new('person', [{:name => :name, :nil_check => true}]) }
56
+
57
+ it do
58
+ xml.serialize(person).should eql("#{XML_INSTRUCT}<person>\n <name nil=\"true\"/>\n</person>")
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,10 @@
1
+ require File.dirname(__FILE__) + '/../lib/pump.rb'
2
+
3
+ XML_INSTRUCT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.run_all_when_everything_filtered = true
8
+ config.filter_run :focus
9
+ config.order = 'random'
10
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pump
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sebastian Munz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.12.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.12.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: guard-rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.2.2
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.2.2
46
+ description: Fast but inflexible XML dumping for ruby objects.
47
+ email:
48
+ - sebastian@yo.lk
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rbenv-gemsets
55
+ - .rbenv-version
56
+ - .rspec
57
+ - CHANGES.md
58
+ - Gemfile
59
+ - Guardfile
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - lib/pump.rb
64
+ - lib/pump/version.rb
65
+ - lib/pump/xml.rb
66
+ - lib/pump/xml/node.rb
67
+ - lib/pump/xml/tag.rb
68
+ - lib/pump/xml/value.rb
69
+ - pump.gemspec
70
+ - spec/pump/xml/tag_spec.rb
71
+ - spec/pump/xml/value_spec.rb
72
+ - spec/pump/xml_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: https://github.com/yolk/pump
75
+ licenses: []
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.23
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: Fast but inflexible XML dumping for ruby objects.
98
+ test_files:
99
+ - spec/pump/xml/tag_spec.rb
100
+ - spec/pump/xml/value_spec.rb
101
+ - spec/pump/xml_spec.rb
102
+ - spec/spec_helper.rb