yahm 0.0.10 → 0.0.11

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: 8ae6af9e954b14e990a059ff3672a72f0f1c9b8f
4
- data.tar.gz: bb64407d073dff1464f31b677499854ebdf23455
3
+ metadata.gz: 266c6fca9bf3474f6fb9766a9030a6bb5ebf700d
4
+ data.tar.gz: 8f38ca8b5fff309b1606d4d5fd42e8c90958d0a8
5
5
  SHA512:
6
- metadata.gz: 589c022e88492cd65dccf9a239be3ebefe63c24d1f6feaed9dc850fddd29f790bf9093a16df9428919222830a078342054c14566115d7de1d931505b6c902004
7
- data.tar.gz: 2295f96ffca8eae8a8c751a31079c70eb204304fa1a693ca547e6fa3e5ef3da6a350ef39780a76f9c4be6638d0b43dbd2076d843aad6a6934d006770c50f6f6f
6
+ metadata.gz: e8e2f2e2a39f7e59a874838342b735b5b187f6e4b5f9309963d53ffa392f09ebd68cf686b18d4f7d264f5db42dbbe0735619ddb2ac2205a936c245f6bb14367c
7
+ data.tar.gz: 76ead024eb29b194fde92eae0bdc8c9315b523e511eb2483b10653d730db17251a2c14ddcc0bca152b193cd23cbb051fdd44a94cedf56848ffbbc31d22f41704
data/lib/yahm/mapping.rb CHANGED
@@ -4,7 +4,7 @@ class Yahm::Mapping
4
4
 
5
5
  def initialize(options = {})
6
6
  @rules = []
7
- @translated_hash = nil
7
+ @result = nil
8
8
  end
9
9
 
10
10
  def map(str, options = {})
@@ -14,13 +14,13 @@ class Yahm::Mapping
14
14
 
15
15
  def translate_hash(input_hash, options = {})
16
16
  @_self = options[:_self]
17
- @translated_hash = {}
17
+ @result = {}
18
18
 
19
19
  @rules.each do |rule|
20
20
  process_rule(input_hash, rule)
21
21
  end
22
22
 
23
- @translated_hash
23
+ @result
24
24
  end
25
25
 
26
26
  private
@@ -38,8 +38,10 @@ class Yahm::Mapping
38
38
  end
39
39
  end
40
40
 
41
- sanitized_target_path = rule.last[:to].sub(/^\//, "").split("/").map(&:to_sym)
42
- target_parent_path = sanitized_target_path.slice(0, sanitized_target_path.length - 1)
41
+ if (to_option = rule.last[:to]).is_a?(String)
42
+ sanitized_target_path = rule.last[:to].sub(/^\//, "").split("/").map(&:to_sym)
43
+ target_parent_path = sanitized_target_path.slice(0, sanitized_target_path.length - 1)
44
+ end
43
45
 
44
46
  source_value = sanitized_source_path.inject(input_hash) { |hash, key| hash.nil? ? nil : hash[key]}
45
47
 
@@ -63,9 +65,13 @@ class Yahm::Mapping
63
65
  source_value = (source_value.is_a?(Array) ? source_value : [source_value]).compact
64
66
  end
65
67
 
66
- target_hash_parent_element = target_parent_path.inject(@translated_hash) { |hash, key| hash[key.to_sym] ||= {} }
67
- target_hash_parent_element.merge!({
68
- sanitized_target_path.last => ((source_value.nil? && !(default_value = rule.last[:default]).nil?) ? default_value : source_value)
69
- })
68
+ if to_option.is_a?(String)
69
+ target_hash_parent_element = target_parent_path.inject(@result) { |hash, key| hash[key.to_sym] ||= {} }
70
+ target_hash_parent_element.merge!({
71
+ sanitized_target_path.last => ((source_value.nil? && !(default_value = rule.last[:default]).nil?) ? default_value : source_value)
72
+ })
73
+ elsif to_option.is_a?(Proc)
74
+ instance_exec(source_value, &to_option)
75
+ end
70
76
  end
71
77
  end
data/lib/yahm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Yahm
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
@@ -85,6 +85,13 @@ describe Yahm::Mapping do
85
85
  :target => nil
86
86
  })
87
87
  end
88
+
89
+ it "accepts a lambda/proc as :to option" do
90
+ expect(Yahm::Mapping.new.map("/source", to: proc { |v| @result = { target: true } }).translate_hash(foo: "bar"))
91
+ .to eq({
92
+ :target => true
93
+ })
94
+ end
88
95
  end
89
96
 
90
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yahm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Sievers