transformator 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7d12867ec6be626559c324f81d2de7763d6d5276
4
+ data.tar.gz: 6dc48981be819de4302d2432953f6f7183a48b63
5
+ SHA512:
6
+ metadata.gz: b8d5164a191183bf62501b67eff5560d2389b5a4c9a863828e0ff88d5b04d5a1127d3501f6aa831982d45c94094be6ae23488224d2eeb04b2df9865c2b19b8ae
7
+ data.tar.gz: bae53fd9cf275ffb45a5e306792d0b1eea195bcdd4064cd69a0474140810dd631e200e14d30c2792fa8d53e5d48dec315810d4ff16efa29601fea6791e35cf2f
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in xml_transformer.gemspec
4
+ gemspec
5
+
6
+ gem "pry", "~> 0.9.12.6"
7
+ gem "pry-byebug", "<= 1.3.2"
8
+ gem "pry-stack_explorer", "~> 0.4.9.1"
9
+ gem "pry-syntax-hacks", "~> 0.0.6"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Michael Sievers
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
+ # Transformator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'transformator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install transformator
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/transformator/fork )
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 a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,41 @@
1
+ require "ox"
2
+
3
+ class Transformator::Transformation
4
+ def initialize(options = {}, &block)
5
+ @context = options[:context]
6
+ @rules = []
7
+ self.instance_eval(&block) if block
8
+ end
9
+
10
+ def apply_to(source, options = {})
11
+ source = Ox.parse(source) if source.is_a?(String)
12
+ result = Ox::Document.new(version: "1.0", encoding: "UTF-8").tap do |target|
13
+ @rules.each do |rule|
14
+ apply_rule(rule, source, target, options)
15
+ end
16
+ end
17
+
18
+ Ox.dump(result, with_xml: true)
19
+ end
20
+
21
+ private
22
+
23
+ def process(path, options = {}, &block)
24
+ @rules.push({ callable: block, path: path }.merge(options))
25
+ self
26
+ end
27
+
28
+ def apply_rule(rule, source, target, options = {})
29
+ value =
30
+ if rule[:path].is_a?(Array)
31
+ rule[:path].inject({}) do |hash, path|
32
+ hash[path] = source.locate(path)
33
+ memo
34
+ end
35
+ else
36
+ source.locate(rule[:path])
37
+ end
38
+
39
+ (options[:context] || @context || self).instance_exec(value, target, &rule[:callable])
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Transformator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "transformator/version"
2
+
3
+ module Transformator
4
+ require_relative "./transformator/transformation"
5
+ end
@@ -0,0 +1,20 @@
1
+ require "active_support/core_ext"
2
+ require "transformator"
3
+
4
+ # require pry if possible
5
+ begin
6
+ require "pry"
7
+ rescue LoadError
8
+ end
9
+
10
+ RSpec.configure do |config|
11
+ # begin --- rspec 3.1 generator
12
+ config.expect_with :rspec do |expectations|
13
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
14
+ end
15
+
16
+ config.mock_with :rspec do |mocks|
17
+ mocks.verify_partial_doubles = true
18
+ end
19
+ # end --- rspec 3.1 generator
20
+ end
@@ -0,0 +1,189 @@
1
+ describe Transformator::Transformation do
2
+ let(:source_xml) do
3
+ <<-xml.strip_heredoc
4
+ <?xml version="1.0" encoding="UTF-8"?>
5
+ <foo>
6
+ <bar>muff</bar>
7
+ </foo>
8
+ xml
9
+ end
10
+
11
+ let(:transformed_xml) do
12
+ <<-xml.strip_heredoc
13
+ <?xml version="1.0" encoding="UTF-8"?>
14
+ <muff>muff</muff>
15
+ xml
16
+ end
17
+
18
+ describe "#new" do
19
+ it "can be called with a block, containing the mapping described by the DSL" do
20
+ transformation = described_class.new do
21
+ process "foo/bar" do |elements, target|
22
+ target_elements = elements.map do |element|
23
+ Ox::Element.new("muff").tap do |new_element|
24
+ element.nodes.each do |node|
25
+ new_element << node
26
+ end
27
+ end
28
+ end
29
+
30
+ target << target_elements.first
31
+ binding.pry
32
+ end
33
+ end
34
+
35
+ expect(transformation.apply_to(source_xml)).to eq(transformed_xml)
36
+ end
37
+ =begin
38
+ context "when called with a \"context\" option" do
39
+ it "passes methods from withing the mapping (e.g. procs/lambdas) to the context" do
40
+ some_class = Class.new do
41
+ require "yahm"
42
+
43
+ def some_method(value)
44
+ value.to_s + "bar"
45
+ end
46
+
47
+ def mapping
48
+ @mapping ||= Yahm::Mapping.new(context: self) do
49
+ map "/foo", to: "/bar", processed_by: lambda { |v| some_method(v) }
50
+ end
51
+ end
52
+
53
+ def apply_mapping_to(hash)
54
+ mapping.apply_to(hash)
55
+ end
56
+ end
57
+
58
+ expect(some_class.new.apply_mapping_to({foo: "foo"})).to eq({:bar=>"foobar"})
59
+ end
60
+ end
61
+ =end
62
+ end
63
+ =begin
64
+ describe ".apply_to" do
65
+ let(:mapping) do
66
+ Yahm::Mapping.new do
67
+ map "/record_id", to: "/id"
68
+ map "/record/title", to: "/title"
69
+ map "/record/isbns", to: "/my_data/isbns"
70
+ map "/record/isbns[1]", to: "/main_isbn" # when an array, one can specifiy which element to choose
71
+ map "/record/count", to: "/count", processed_by: lambda { |v| v.to_i }
72
+ map "/record/languages", to: "/languages", force_array: true
73
+ map "/record/authors", to: "/authors", split_by: ";"
74
+ map "/record/version", to: "/version", default: 1
75
+ map "/record/creators", to: "/creators", force_array: true # when source value is nil, there should be an empty array in the target hash
76
+ map "/record/publishers", to: "/publishers", split_by: ";" # if there are is no source value, this should result to nil in the target hash
77
+ end
78
+ end
79
+
80
+ let(:input_hash) do
81
+ {
82
+ record_id: "some_id123",
83
+ record: {
84
+ title: "some title",
85
+ isbns: [
86
+ "3-86680-192-0",
87
+ "3-680-08783-7"
88
+ ],
89
+ count: "3",
90
+ languages: "ger",
91
+ authors: "John Doe; Jane Doe"
92
+ }
93
+ }
94
+ end
95
+
96
+ let(:expected_result) do
97
+ {
98
+ :id=>"some_id123",
99
+ :title=>"some title",
100
+ :my_data=>{:isbns=>["3-86680-192-0", "3-680-08783-7"]},
101
+ :main_isbn=>"3-680-08783-7",
102
+ :count=>3,
103
+ :languages=>["ger"],
104
+ :authors=>["John Doe", "Jane Doe"],
105
+ :version=>1,
106
+ :creators=>[],
107
+ :publishers=>nil
108
+ }
109
+ end
110
+
111
+ it "translates a given hash according to @rules" do
112
+ expect(mapping.apply_to(input_hash)).to eq(expected_result)
113
+ end
114
+
115
+ context "when threading is enabled" do
116
+ it "translates a given hash according to @rules" do
117
+ mapping.use_threads = true
118
+ expect(mapping.apply_to(input_hash)).to eq(expected_result)
119
+ end
120
+ end
121
+
122
+ it "does not apply \"split_by\" if source value is an array" do
123
+ expect(Yahm::Mapping.new { map "/source/value", to: "/target", split_by: ";" }.apply_to(source: { value: ["foo", "foo;bar"] }))
124
+ .to eq({
125
+ :target => ["foo", "foo;bar"]
126
+ })
127
+ end
128
+
129
+ it "processes the source value by the lambda/proc given by \"processed_by\"" do
130
+ expect(Yahm::Mapping.new { map "/source/value", to: "/target", processed_by: lambda { |v| v.downcase } }.apply_to(source: { value: "A STRING" }))
131
+ .to eq({
132
+ :target => "a string"
133
+ })
134
+
135
+ expect(Yahm::Mapping.new { map "/source/value", to: "/target", processed_by: proc { |v| v.downcase } }.apply_to(source: { value: "A STRING" }))
136
+ .to eq({
137
+ :target => "a string"
138
+ })
139
+ end
140
+
141
+ it "returns nil if the source path does not exist" do
142
+ expect(Yahm::Mapping.new { map "/source/value/min", to: "/target" }.apply_to(foo: "bar")).to eq({
143
+ :target => nil
144
+ })
145
+ end
146
+
147
+ it "accepts a lambda/proc as :to option" do
148
+ expect(Yahm::Mapping.new { map "/source", to: lambda { |v, result| result.merge!(target: true) } }.apply_to(foo: "bar"))
149
+ .to eq({
150
+ :target => true
151
+ })
152
+ end
153
+
154
+ it "provides multiple values as an array of values to lambdas/procs" do
155
+ expect(Yahm::Mapping.new { map "/source", to: "/array_length", processed_by: lambda { |v| v.length } }.apply_to(source: ["bar", "moep"]))
156
+ .to eq({
157
+ :array_length => 2
158
+ })
159
+ end
160
+
161
+ it "can map multiple input fields to one output field" do
162
+ mapping = Yahm::Mapping.new do
163
+ map ["/hello", "/world", "/foo"], to: "/combined", processed_by: lambda { |v| v.values.compact.join(" ") }
164
+ end
165
+
166
+ expect(mapping.apply_to({
167
+ hello: "hello",
168
+ world: "world"
169
+ })).to eq({
170
+ combined: "hello world"
171
+ })
172
+ end
173
+
174
+ it "can be called with a \"context\" option to set/overwrite the mapping context" do
175
+ mapping = Yahm::Mapping.new do
176
+ map "/source", to: "/target", processed_by: lambda { |v| process_value(v) }
177
+ end
178
+
179
+ context = Module.new do
180
+ def self.process_value(v)
181
+ v.to_i
182
+ end
183
+ end
184
+
185
+ expect(mapping.apply_to({source: "1"}, context: context)).to eq({target: 1})
186
+ end
187
+ end
188
+ =end
189
+ end
@@ -0,0 +1,2 @@
1
+ describe Transformator do
2
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "transformator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "transformator"
8
+ spec.version = Transformator::VERSION
9
+ spec.authors = ["Michael Sievers"]
10
+ spec.summary = %q{A ruby dsl for transforming hashes or json/xml documents}
11
+ spec.homepage = "https://github.com/ubpb/transformator"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "ox", ">= 2.0.0", "< 3.0.0"
20
+
21
+ spec.add_development_dependency "activesupport", ">= 3.0.0", "< 5.0.0"
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", ">= 3.0.0", "< 4.0.0"
25
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transformator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Sievers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ox
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 3.0.0
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: 5.0.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 3.0.0
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: 5.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.6'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.6'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 3.0.0
88
+ - - "<"
89
+ - !ruby/object:Gem::Version
90
+ version: 4.0.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 3.0.0
98
+ - - "<"
99
+ - !ruby/object:Gem::Version
100
+ version: 4.0.0
101
+ description:
102
+ email:
103
+ executables: []
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".gitignore"
108
+ - ".rspec"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - lib/transformator.rb
114
+ - lib/transformator/transformation.rb
115
+ - lib/transformator/version.rb
116
+ - spec/spec_helper.rb
117
+ - spec/transformator/transformation_spec.rb
118
+ - spec/transformator_spec.rb
119
+ - transformator.gemspec
120
+ homepage: https://github.com/ubpb/transformator
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.2
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: A ruby dsl for transforming hashes or json/xml documents
144
+ test_files:
145
+ - spec/spec_helper.rb
146
+ - spec/transformator/transformation_spec.rb
147
+ - spec/transformator_spec.rb