morfo 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87b740fa3940052dc425842ec2f8a8c1b39a8955
4
- data.tar.gz: ce6d9032e4b76325233bcad0e4ed2a29dba17c15
3
+ metadata.gz: e9745685e231af883b84df5fff36681de804fa51
4
+ data.tar.gz: 2907a87a4dc0903ce1144e0b274950d8cdf02056
5
5
  SHA512:
6
- metadata.gz: f0baa0e37cf6809e502bed94eb4da771d3572b51970918d9666d1ea4bdcc2c9872b046f78df3f2035727da35a33affbe3daa3c51c9d050864c61cd6361580aa8
7
- data.tar.gz: e1e50f08391db40f95d7ecb81908c19e9e3774d996ca0fa0da757e1f7cd4a1f3c582bbc34cdb0b86ca2ab3dcc840f9b715f24aafb37c05a7bc3f6dd4a395196e
6
+ metadata.gz: 28fdf38411af5ddf9ab0d413c591b3a71e232cb38745a6abbe471e8dffcd0f142547acd7114ed3ad1704a80aa5b9357b16acbae1c93347c5ee453161c342caf8
7
+ data.tar.gz: 29ce48337dbd64783e7f00cadfbe44113b7c807a3adda00956308140748f45e4dd761bf810513f052d2917d22318a79b72a1eca1e2fedca2b518ccc2f35aa48e
data/README.md CHANGED
@@ -28,13 +28,28 @@ In order to morf the hashes you have to provide a class that extends `Morf::Base
28
28
 
29
29
  Use the `field` method to specify what fields exist and where they will get their data from:
30
30
 
31
+ ### Options
32
+
33
+ The following options can be passed to the morfer
34
+
35
+ #### include_nil_values
36
+
37
+ When passed in, all values that can not be found in the source hash will be set to nil (defaults to `false`). By default those keys will just appear in the result:
38
+
39
+ ```ruby
40
+ Title.morf([
41
+ { title: "The Walking Dead" },
42
+ { title: "Breaking Bad" },
43
+ ], include_nil_values: true)
44
+ ```
45
+
31
46
  ### Simple Mapping
32
47
 
33
48
  The most basic form is copying the value from another field.
34
49
 
35
50
  ```ruby
36
51
  class Title < Morfo::Base
37
- field(:tv_show_title)from(:title)
52
+ field(:tv_show_title).from(:title)
38
53
  end
39
54
  ```
40
55
 
@@ -12,14 +12,14 @@ module Morfo
12
12
  act
13
13
  end
14
14
 
15
- def self.morf input
16
- input.map { |row| morf_single(row) }
15
+ def self.morf input, options = {}
16
+ input.map { |row| morf_single(row, options) }
17
17
  end
18
18
 
19
- def self.morf_single input
19
+ def self.morf_single input, options = {}
20
20
  output = {}
21
21
  mapping_actions.each do |field_path, action|
22
- output.deep_merge!(store_value(action.execute(input), field_path))
22
+ output.deep_merge!(store_value(action.execute(input), field_path, options))
23
23
  end
24
24
  output
25
25
  end
@@ -29,11 +29,11 @@ module Morfo
29
29
  @actions ||= {}
30
30
  end
31
31
 
32
- def self.store_value value, to
33
- return {} if value.nil?
32
+ def self.store_value value, to, options
33
+ return {} if value.nil? && !options[:include_nil_values]
34
34
 
35
35
  to.reverse.inject({}) do |hash, key|
36
- if hash.keys.first.nil?
36
+ if hash.empty?
37
37
  hash.merge!(key => value)
38
38
  else
39
39
  { key => hash }
@@ -1,3 +1,3 @@
1
1
  module Morfo
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features|benchmarks)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency "rake"
@@ -24,10 +24,20 @@ shared_examples "a 1 to 1 morfer" do
24
24
  expect(subject.morf(input)).to eq(expected_output)
25
25
  end
26
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)
27
+ context "include nils" do
28
+ it "includes nil values in result" do
29
+ expected_output = [{tv_show_title: nil},{tv_show_title: nil}]
30
+ modified_input = input.map{|h| h.reject{|k, v| k == :title}}
31
+ expect(subject.morf(modified_input, {include_nil_values: true})).to eq(expected_output)
32
+ end
33
+ end
34
+
35
+ context "disable nils" do
36
+ it "leaves out nil values in result" do
37
+ expected_output = [{},{}]
38
+ modified_input = input.map{|h| h.reject{|k, v| k == :title}}
39
+ expect(subject.morf(modified_input)).to eq(expected_output)
40
+ end
31
41
  end
32
42
  end
33
43
 
@@ -40,10 +50,20 @@ shared_examples "a 1 to 1 morfer" do
40
50
  expect(subject.morf_single(single_input)).to eq(expected_output)
41
51
  end
42
52
 
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)
53
+ context "include nils" do
54
+ it "includes nil values in result" do
55
+ expected_output = {tv_show_title: nil}
56
+ modified_input = single_input.reject { |k, v| k == :title }
57
+ expect(subject.morf_single(modified_input, {include_nil_values: true})).to eq(expected_output)
58
+ end
59
+ end
60
+
61
+ context "disable nils" do
62
+ it "leaves out nil values in result" do
63
+ expected_output = {}
64
+ modified_input = single_input.reject { |k, v| k == :title }
65
+ expect(subject.morf_single(modified_input)).to eq(expected_output)
66
+ end
47
67
  end
48
68
  end
49
69
  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.0
4
+ version: 0.5.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: 2015-04-27 00:00:00.000000000 Z
11
+ date: 2015-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -136,11 +136,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
136
  version: '0'
137
137
  requirements: []
138
138
  rubyforge_project:
139
- rubygems_version: 2.4.5
139
+ rubygems_version: 2.4.5.1
140
140
  signing_key:
141
141
  specification_version: 4
142
142
  summary: Inspired by ActiveImporter, this gem generically converts an array of hashes
143
143
  test_files:
144
+ - benchmarks/data.rb
145
+ - benchmarks/run.rb
144
146
  - spec/lib/morfo/builder_spec.rb
145
147
  - spec/lib/morfo/tools_spec.rb
146
148
  - spec/lib/morfo_spec.rb