morfo 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +1 -1
- data/Gemfile +11 -11
- data/Guardfile +2 -2
- data/README.md +180 -64
- data/Rakefile +2 -2
- data/benchmarks/data.rb +10 -10
- data/benchmarks/run.rb +7 -7
- data/lib/morfo.rb +6 -15
- data/lib/morfo/builder.rb +34 -0
- data/lib/morfo/tools.rb +34 -0
- data/lib/morfo/version.rb +1 -1
- data/morfo.gemspec +14 -13
- data/spec/lib/morfo/builder_spec.rb +114 -0
- data/spec/lib/morfo/tools_spec.rb +91 -0
- data/spec/lib/morfo_spec.rb +51 -230
- data/spec/spec_helper.rb +7 -5
- data/spec/support/shared_context.rb +34 -0
- data/spec/support/shared_examples.rb +248 -0
- metadata +27 -3
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "simplecov"
|
2
|
+
require "coveralls"
|
3
3
|
|
4
4
|
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
5
5
|
SimpleCov.start do
|
6
|
-
add_filter
|
6
|
+
add_filter "spec"
|
7
7
|
end
|
8
8
|
|
9
|
-
require
|
10
|
-
require
|
9
|
+
require "rspec"
|
10
|
+
require "morfo"
|
11
|
+
|
12
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
@@ -0,0 +1,34 @@
|
|
1
|
+
shared_context "tv shows" do
|
2
|
+
let(:input) do
|
3
|
+
[
|
4
|
+
{
|
5
|
+
title: "The Walking Dead",
|
6
|
+
channel: "AMC",
|
7
|
+
watchers: 1337,
|
8
|
+
status: "running",
|
9
|
+
cast: ["Lincoln, Andrew", "McBride, Melissa"],
|
10
|
+
ratings: {
|
11
|
+
imdb: 8.7,
|
12
|
+
trakt: 89,
|
13
|
+
rotten_tomatoes: 93,
|
14
|
+
},
|
15
|
+
},
|
16
|
+
{
|
17
|
+
title: "Breaking Bad",
|
18
|
+
channel: "AMC",
|
19
|
+
watchers: 72891,
|
20
|
+
status: "ended",
|
21
|
+
cast: ["Cranston, Bryan", "Gunn, Anna"],
|
22
|
+
ratings: {
|
23
|
+
imdb: 9.5,
|
24
|
+
trakt: 95,
|
25
|
+
rotten_tomatoes: 100,
|
26
|
+
},
|
27
|
+
}
|
28
|
+
]
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:single_input) do
|
32
|
+
input.first
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,248 @@
|
|
1
|
+
shared_examples "an error throwing morfer" do
|
2
|
+
include_context "tv shows"
|
3
|
+
|
4
|
+
describe "#morf" do
|
5
|
+
it "raises error for nil field" do
|
6
|
+
expect{no_from.morf([{my_field: :something}])}.to raise_error(ArgumentError)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#morf_single" do
|
11
|
+
it "raises error for nil field" do
|
12
|
+
expect{no_from.morf_single({my_field: :something})}.to raise_error(ArgumentError)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
shared_examples "a 1 to 1 morfer" do
|
18
|
+
include_context "tv shows"
|
19
|
+
|
20
|
+
describe "#morf" do
|
21
|
+
let(:expected_output) { input.map{|v| {tv_show_title: v[:title]} } }
|
22
|
+
|
23
|
+
it "maps title correctly" do
|
24
|
+
expect(subject.morf(input)).to eq(expected_output)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "leaves out nil values in result" do
|
28
|
+
expected_output = [{},{}]
|
29
|
+
modified_input = input.map{|h| h.reject{|k, v| k == :title}}
|
30
|
+
expect(subject.morf(modified_input)).to eq(expected_output)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#morf_single" do
|
35
|
+
let(:expected_output) do
|
36
|
+
{ tv_show_title: single_input[:title] }
|
37
|
+
end
|
38
|
+
|
39
|
+
it "maps title correctly" do
|
40
|
+
expect(subject.morf_single(single_input)).to eq(expected_output)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "leaves out nil values in result" do
|
44
|
+
expected_output = {}
|
45
|
+
modified_input = single_input.reject { |k, v| k == :title }
|
46
|
+
expect(subject.morf_single(modified_input)).to eq(expected_output)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
shared_examples "a 1 to many morfer" do
|
52
|
+
include_context "tv shows"
|
53
|
+
|
54
|
+
describe "#morf" do
|
55
|
+
let(:expected_output) { input.map{|v| {title: v[:title], also_title: v[:title]} } }
|
56
|
+
|
57
|
+
it "maps title to multiple fields" do
|
58
|
+
expect(subject.morf(input)).to eq(expected_output)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#morf_single" do
|
63
|
+
let(:expected_output) { {title: single_input[:title], also_title: single_input[:title]} }
|
64
|
+
|
65
|
+
it "maps title to multiple fields" do
|
66
|
+
expect(subject.morf_single(single_input)).to eq(expected_output)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
shared_examples "a 1 to 1 morfer with transformation" do
|
72
|
+
include_context "tv shows"
|
73
|
+
|
74
|
+
describe "#morf" do
|
75
|
+
let(:expected_output) { input.map{|v| {title: "#{v[:title]} and Zombies"} } }
|
76
|
+
|
77
|
+
it "calls transformation correctly" do
|
78
|
+
expect(subject.morf(input)).to eq(expected_output)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "#morf_single" do
|
83
|
+
let(:expected_output) { { title: "#{single_input[:title]} and Zombies" } }
|
84
|
+
|
85
|
+
it "calls transformation correctly" do
|
86
|
+
expect(subject.morf_single(single_input)).to eq(expected_output)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
shared_examples "a calculating morfer" do
|
92
|
+
include_context "tv shows"
|
93
|
+
|
94
|
+
describe "#morf" do
|
95
|
+
let(:expected_output) do
|
96
|
+
input.map{|r| { title_with_channel: "#{r[:title]}, (#{r[:channel]})" } }
|
97
|
+
end
|
98
|
+
|
99
|
+
it "maps calculation correctly" do
|
100
|
+
expect(subject.morf(input)).to eq(expected_output)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "#morf_single" do
|
105
|
+
let(:expected_output) do
|
106
|
+
{
|
107
|
+
title_with_channel: "#{single_input[:title]}, (#{single_input[:channel]})"
|
108
|
+
}
|
109
|
+
end
|
110
|
+
|
111
|
+
it "maps calculation correctly" do
|
112
|
+
expect(subject.morf_single(single_input)).to eq(expected_output)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
shared_examples "a static morfer" do
|
118
|
+
include_context "tv shows"
|
119
|
+
|
120
|
+
describe "#morf" do
|
121
|
+
let(:expected_output) { input.map{|r| {new_title: "Static Title"} } }
|
122
|
+
|
123
|
+
it "maps static value correctly" do
|
124
|
+
expect(subject.morf(input)).to eq(expected_output)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "#morf_single" do
|
129
|
+
let(:expected_output) { { new_title: "Static Title" } }
|
130
|
+
|
131
|
+
it "maps static value correctly" do
|
132
|
+
expect(subject.morf_single(single_input)).to eq(expected_output)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
shared_examples "a morfer with nested source" do
|
138
|
+
include_context "tv shows"
|
139
|
+
|
140
|
+
describe "#morf" do
|
141
|
+
context "valid path" do
|
142
|
+
let(:expected_output) { input.map{|v| {rating: v[:ratings][:imdb]} } }
|
143
|
+
|
144
|
+
it "maps nested attributes correctly" do
|
145
|
+
expect(valid_path.morf(input)).to eq(expected_output)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context "valid path with transformation" do
|
150
|
+
let(:expected_output) { input.map{|v| {rating: "Rating: #{v[:ratings][:imdb]}"} } }
|
151
|
+
|
152
|
+
it "maps and transforms nested attributes correctly" do
|
153
|
+
expect(valid_path_with_transformation.morf(input)).to eq(expected_output)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context "valid path with calculation" do
|
158
|
+
let(:expected_output) do
|
159
|
+
[
|
160
|
+
{ ratings: "IMDB: 8.7, Trakt: 89, Rotten Tommatoes: 93" },
|
161
|
+
{ ratings: "IMDB: 9.5, Trakt: 95, Rotten Tommatoes: 100" },
|
162
|
+
]
|
163
|
+
end
|
164
|
+
|
165
|
+
it "maps nested attributes with transformation" do
|
166
|
+
expect(valid_path_with_calculation.morf(input)).to eq(expected_output)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context "invalid path" do
|
171
|
+
let(:expected_output) { [{},{}] }
|
172
|
+
|
173
|
+
it "doesn't raise error for invalid path" do
|
174
|
+
expect(invalid_path.morf(input)).to eq(expected_output)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe "#morf_single" do
|
180
|
+
context "valid path" do
|
181
|
+
let(:expected_output) { {rating: single_input[:ratings][:imdb]} }
|
182
|
+
|
183
|
+
it "maps nested attributes correctly" do
|
184
|
+
expect(valid_path.morf_single(single_input)).to eq(expected_output)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context "valid path with transformation" do
|
189
|
+
let(:expected_output) { {rating: "Rating: #{single_input[:ratings][:imdb]}"} }
|
190
|
+
|
191
|
+
it "maps nested attributes with transformation" do
|
192
|
+
expect(valid_path_with_transformation.morf_single(single_input)).to eq(expected_output)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
context "valid path with calculation" do
|
197
|
+
let(:expected_output) { {ratings: "IMDB: 8.7, Trakt: 89, Rotten Tommatoes: 93"} }
|
198
|
+
|
199
|
+
it "maps nested attributes with transformation" do
|
200
|
+
expect(valid_path_with_calculation.morf_single(single_input)).to eq(expected_output)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context "invalid path" do
|
205
|
+
let(:expected_output) { {} }
|
206
|
+
|
207
|
+
it "doesn't raise error for invalid path" do
|
208
|
+
expect(invalid_path.morf_single(single_input)).to eq(expected_output)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
shared_examples "a morfer with nested destination" do
|
215
|
+
include_context "tv shows"
|
216
|
+
|
217
|
+
describe "#morf" do
|
218
|
+
let(:expected_output) do
|
219
|
+
input.map do |v|
|
220
|
+
{
|
221
|
+
tv_show: {
|
222
|
+
title: v[:title],
|
223
|
+
channel: "Channel: #{v[:channel]}",
|
224
|
+
}
|
225
|
+
}
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
it "maps to nested destination" do
|
230
|
+
expect(subject.morf(input)).to eq(expected_output)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
describe "#morf_single" do
|
235
|
+
let(:expected_output) do
|
236
|
+
{
|
237
|
+
tv_show: {
|
238
|
+
title: single_input[:title],
|
239
|
+
channel: "Channel: #{single_input[:channel]}",
|
240
|
+
}
|
241
|
+
}
|
242
|
+
end
|
243
|
+
|
244
|
+
it "maps to nested destination" do
|
245
|
+
expect(subject.morf_single(single_input)).to eq(expected_output)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: morfo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leif Gensert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.2'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,10 +106,16 @@ files:
|
|
92
106
|
- benchmarks/run.rb
|
93
107
|
- lib/morfo.rb
|
94
108
|
- lib/morfo/actions.rb
|
109
|
+
- lib/morfo/builder.rb
|
110
|
+
- lib/morfo/tools.rb
|
95
111
|
- lib/morfo/version.rb
|
96
112
|
- morfo.gemspec
|
113
|
+
- spec/lib/morfo/builder_spec.rb
|
114
|
+
- spec/lib/morfo/tools_spec.rb
|
97
115
|
- spec/lib/morfo_spec.rb
|
98
116
|
- spec/spec_helper.rb
|
117
|
+
- spec/support/shared_context.rb
|
118
|
+
- spec/support/shared_examples.rb
|
99
119
|
homepage: ''
|
100
120
|
licenses:
|
101
121
|
- MIT
|
@@ -116,10 +136,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
116
136
|
version: '0'
|
117
137
|
requirements: []
|
118
138
|
rubyforge_project:
|
119
|
-
rubygems_version: 2.
|
139
|
+
rubygems_version: 2.4.5
|
120
140
|
signing_key:
|
121
141
|
specification_version: 4
|
122
142
|
summary: Inspired by ActiveImporter, this gem generically converts an array of hashes
|
123
143
|
test_files:
|
144
|
+
- spec/lib/morfo/builder_spec.rb
|
145
|
+
- spec/lib/morfo/tools_spec.rb
|
124
146
|
- spec/lib/morfo_spec.rb
|
125
147
|
- spec/spec_helper.rb
|
148
|
+
- spec/support/shared_context.rb
|
149
|
+
- spec/support/shared_examples.rb
|