morfo 0.5.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9745685e231af883b84df5fff36681de804fa51
4
- data.tar.gz: 2907a87a4dc0903ce1144e0b274950d8cdf02056
3
+ metadata.gz: 3dacdb6d0f61b40610a10e7c98846c1a9cf80112
4
+ data.tar.gz: 02281f2f9f9c33335c12acb90e511e3351724f3e
5
5
  SHA512:
6
- metadata.gz: 28fdf38411af5ddf9ab0d413c591b3a71e232cb38745a6abbe471e8dffcd0f142547acd7114ed3ad1704a80aa5b9357b16acbae1c93347c5ee453161c342caf8
7
- data.tar.gz: 29ce48337dbd64783e7f00cadfbe44113b7c807a3adda00956308140748f45e4dd761bf810513f052d2917d22318a79b72a1eca1e2fedca2b518ccc2f35aa48e
6
+ metadata.gz: 4d25e176a5594b68a1f6d9a0813da091963da55ff3f77e5dd5bfca0f12e7219cfb7a6778c92ccb389bf3d11f1fac0e45da4c65ac65e3d55e5c0781393c744274
7
+ data.tar.gz: 01153864d73f8c816c56a389af3f45fa6da76dc68d6e1b4780f21895b0d80b8e835cbc67ab9364c78ee97e5ce3a87f3ec4ced4e894156147595cfc7bb05aa161
data/.travis.yml CHANGED
@@ -1,13 +1,16 @@
1
+ sudo: false
1
2
  language: ruby
2
3
  cache: bundler
3
4
  env: JRUBY_OPTS=--2.0
4
5
  rvm:
5
6
  - 2.0.0
6
- - 2.1.3
7
- - jruby-1.7.16
7
+ - 2.1.7
8
+ - 2.2.3
9
+ - jruby-1.7.22
10
+ - jruby-9.0.3.0
8
11
  - ruby-head
9
12
  - jruby-head
10
- - rbx-2.2.3
13
+ - rbx-2.2.9
11
14
  matrix:
12
15
  fast_finish: true
13
16
  allow_failures:
data/lib/morfo/builder.rb CHANGED
@@ -17,7 +17,8 @@ module Morfo
17
17
  end
18
18
 
19
19
  if definition[:calculated]
20
- f = f.calculated { |r| definition[:calculated] % Morfo::Tools::FlattenHashKeys.new(r).flatten }
20
+ base_hash = Morfo::Tools::BaseKeys.new(definition[:calculated]).build
21
+ f = f.calculated { |r| definition[:calculated] % base_hash.merge(Morfo::Tools::FlattenHashKeys.new(r).flatten) }
21
22
  end
22
23
 
23
24
  if definition[:transformed]
data/lib/morfo/tools.rb CHANGED
@@ -30,5 +30,21 @@ module Morfo
30
30
  end
31
31
  end
32
32
  end
33
+
34
+ class BaseKeys
35
+ attr_reader :input_string
36
+
37
+ def initialize(input_string)
38
+ @input_string = input_string.nil? ? "" : input_string.dup.freeze
39
+ end
40
+
41
+ def build
42
+ keys = input_string.scan(/\%\{([^\}]+)\}/).flatten
43
+
44
+ keys.inject({}) do |hash, key|
45
+ hash.merge!(key.to_sym => nil)
46
+ end
47
+ end
48
+ end
33
49
  end
34
50
  end
data/lib/morfo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Morfo
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -32,6 +32,19 @@ module Morfo
32
32
  end
33
33
  end
34
34
 
35
+ context "already nested" do
36
+ let(:input_hash) { { :"a.nested" => :hash } }
37
+ let(:expected_output) { { :"a.nested" => :hash } }
38
+
39
+ it "returns a new hash" do
40
+ expect(subject.flatten).not_to be(input_hash)
41
+ end
42
+
43
+ it "returns the correct hash" do
44
+ expect(subject.flatten).to eq(expected_output)
45
+ end
46
+ end
47
+
35
48
  context "multiple nested hash" do
36
49
  let(:input_hash) { { a: { deeper: { nested: :hash } } } }
37
50
  let(:expected_output) { { :"a.deeper.nested" => :hash } }
@@ -87,5 +100,44 @@ module Morfo
87
100
  end
88
101
  end
89
102
  end
103
+
104
+ describe BaseKeys do
105
+ subject { described_class.new(input_string) }
106
+
107
+ describe "#build" do
108
+ context "empty string" do
109
+ let(:input_string) { "" }
110
+ let(:expected_output) { { } }
111
+
112
+ context "empty string" do
113
+ it "returns empty hash" do
114
+ expect(subject.build).to eq(expected_output)
115
+ end
116
+ end
117
+ end
118
+
119
+ context "nil input" do
120
+ let(:input_string) { nil }
121
+ let(:expected_output) { { } }
122
+
123
+ context "empty string" do
124
+ it "returns empty hash" do
125
+ expect(subject.build).to eq(expected_output)
126
+ end
127
+ end
128
+ end
129
+
130
+ context "non nested values" do
131
+ let(:input_string) { "The rating is: %{rating}" }
132
+ let(:expected_output) { { rating: nil } }
133
+
134
+ describe "build" do
135
+ it "returns expected values" do
136
+ expect(subject.build).to eq(expected_output)
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
90
142
  end
91
143
  end
@@ -73,7 +73,7 @@ describe Morfo::Base do
73
73
 
74
74
  subject(:valid_path_with_calculation) do
75
75
  class ImdbRatingMorferWithCalculation < Morfo::Base
76
- field(:ratings).calculated {|r| "IMDB: #{r[:ratings][:imdb]}, Trakt: #{r[:ratings][:trakt]}, Rotten Tommatoes: #{r[:ratings][:rotten_tomatoes]}" }
76
+ field(:ratings).calculated {|r| "IMDB: #{r[:ratings] && r[:ratings][:imdb]}, Trakt: #{r[:ratings] && r[:ratings][:trakt]}, Rotten Tommatoes: #{r[:ratings] && r[:ratings][:rotten_tomatoes]}" }
77
77
  end
78
78
  ImdbRatingMorferWithCalculation
79
79
  end
@@ -175,15 +175,37 @@ shared_examples "a morfer with nested source" do
175
175
  end
176
176
 
177
177
  context "valid path with calculation" do
178
- let(:expected_output) do
179
- [
180
- { ratings: "IMDB: 8.7, Trakt: 89, Rotten Tommatoes: 93" },
181
- { ratings: "IMDB: 9.5, Trakt: 95, Rotten Tommatoes: 100" },
182
- ]
178
+ context "all inputs matched" do
179
+ let(:expected_output) do
180
+ [
181
+ { ratings: "IMDB: 8.7, Trakt: 89, Rotten Tommatoes: 93" },
182
+ { ratings: "IMDB: 9.5, Trakt: 95, Rotten Tommatoes: 100" },
183
+ ]
184
+ end
185
+
186
+ it "maps nested attributes with transformation" do
187
+ expect(valid_path_with_calculation.morf(input)).to eq(expected_output)
188
+ end
183
189
  end
184
190
 
185
- it "maps nested attributes with transformation" do
186
- expect(valid_path_with_calculation.morf(input)).to eq(expected_output)
191
+ context "inputs not matched" do
192
+ let(:expected_output) do
193
+ [
194
+ { ratings: "IMDB: , Trakt: , Rotten Tommatoes: " },
195
+ { ratings: "IMDB: , Trakt: , Rotten Tommatoes: " },
196
+ ]
197
+ end
198
+
199
+ let(:modified_input) do
200
+ input.map do |row|
201
+ row.delete(:ratings)
202
+ row
203
+ end
204
+ end
205
+
206
+ it "maps nested attributes with transformation" do
207
+ expect(valid_path_with_calculation.morf(modified_input)).to eq(expected_output)
208
+ end
187
209
  end
188
210
  end
189
211
 
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.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leif Gensert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-11 00:00:00.000000000 Z
11
+ date: 2015-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake