yahm 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/yahm/mapping.rb +15 -9
- data/lib/yahm/version.rb +1 -1
- data/spec/yahm/mapping_spec.rb +7 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 266c6fca9bf3474f6fb9766a9030a6bb5ebf700d
|
4
|
+
data.tar.gz: 8f38ca8b5fff309b1606d4d5fd42e8c90958d0a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
@
|
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
|
-
@
|
17
|
+
@result = {}
|
18
18
|
|
19
19
|
@rules.each do |rule|
|
20
20
|
process_rule(input_hash, rule)
|
21
21
|
end
|
22
22
|
|
23
|
-
@
|
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
|
-
|
42
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
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
data/spec/yahm/mapping_spec.rb
CHANGED
@@ -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
|