yahm 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -1
- data/lib/yahm/hash_mapper.rb +4 -0
- data/lib/yahm/mapping.rb +3 -7
- data/lib/yahm/version.rb +1 -1
- data/spec/yahm/hash_mapper_spec.rb +11 -2
- data/spec/yahm/mapping_spec.rb +7 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8e9c9372841f53ea0466a68d75609ca2c44fcb9
|
4
|
+
data.tar.gz: e6161948ad5bcc0772ef79b03df84de245afcde7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 291151329654b3abb919dd6bccdb47a7210a337d6c41f5369f44243d389ffa6635d69a17e0b988abf835479a7cf56012a40d1999a720f728cdc69eae7587b56b
|
7
|
+
data.tar.gz: 6953490ce87c26b07e911273ab4cdb40362d626603ccb54eacb1954801d520d5908c7cb1eed025b0fc96dbded3ee7d4b9c9f7315e4f4db1b8a3eff05bf99b10d
|
data/README.md
CHANGED
@@ -66,7 +66,28 @@ Record.translated_hash
|
|
66
66
|
=> { :id => "...", :count => ..., :subjects => { :most_important_subject => "..."}, ... }
|
67
67
|
```
|
68
68
|
|
69
|
-
#### Examples for using
|
69
|
+
#### Examples for using `to`
|
70
|
+
|
71
|
+
The `to` option of a mapping rule can either be a string, naming the path in the resultung hash or a proc, which is executed in the context of the created mapper. The result hash can be accessed by `@result`.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
require "yahm"
|
75
|
+
|
76
|
+
class Record
|
77
|
+
extend Yahm::HashMapper
|
78
|
+
|
79
|
+
define_mapper :from_other_record do
|
80
|
+
def funky_method(value)
|
81
|
+
@result[:is_funky] = true if funky?(value)
|
82
|
+
end
|
83
|
+
|
84
|
+
map "/record/count", to: "count"
|
85
|
+
map "/record/name", to: lambda { |v| funky_method(v) }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
#### Examples for using `processed_by`
|
70
91
|
|
71
92
|
You can pass symbols, lambdas or procs as `processed_by` option. Inside of lambdas or procs, `self` corresponds to the context of the defined mapper. To access the context of to extended object, use `_self`.
|
72
93
|
|
data/lib/yahm/hash_mapper.rb
CHANGED
data/lib/yahm/mapping.rb
CHANGED
@@ -46,14 +46,10 @@ class Yahm::Mapping
|
|
46
46
|
source_value = sanitized_source_path.inject(input_hash) { |hash, key| hash.nil? ? nil : hash[key]}
|
47
47
|
|
48
48
|
unless (symbol_or_callable = rule.last[:processed_by]).nil?
|
49
|
-
source_value = if
|
50
|
-
source_value.
|
49
|
+
source_value = if symbol_or_callable.is_a?(Symbol)
|
50
|
+
source_value.send(symbol_or_callable)
|
51
51
|
else
|
52
|
-
|
53
|
-
source_value.send(symbol_or_callable)
|
54
|
-
else
|
55
|
-
symbol_or_callable.call(source_value)
|
56
|
-
end
|
52
|
+
symbol_or_callable.call(source_value)
|
57
53
|
end
|
58
54
|
end
|
59
55
|
|
data/lib/yahm/version.rb
CHANGED
@@ -15,7 +15,7 @@ describe Yahm::HashMapper do
|
|
15
15
|
|
16
16
|
describe "#define_mapper" do
|
17
17
|
let(:class_extended_with_hash_mapper) do
|
18
|
-
|
18
|
+
Class.new do
|
19
19
|
extend Yahm::HashMapper
|
20
20
|
|
21
21
|
def method_of_extended_object(value)
|
@@ -35,6 +35,9 @@ describe Yahm::HashMapper do
|
|
35
35
|
true
|
36
36
|
end
|
37
37
|
end
|
38
|
+
|
39
|
+
define_mapper :my_private_mapper, private: true do
|
40
|
+
end
|
38
41
|
end
|
39
42
|
end
|
40
43
|
|
@@ -60,7 +63,7 @@ describe Yahm::HashMapper do
|
|
60
63
|
|
61
64
|
context "when called with an option called 'call_setter'" do
|
62
65
|
let(:class_extended_with_hash_mapper) do
|
63
|
-
|
66
|
+
Class.new do
|
64
67
|
extend Yahm::HashMapper
|
65
68
|
|
66
69
|
attr_accessor :my_result
|
@@ -75,5 +78,11 @@ describe Yahm::HashMapper do
|
|
75
78
|
expect(instance.my_result).not_to be_nil
|
76
79
|
end
|
77
80
|
end
|
81
|
+
|
82
|
+
context "when called with private: true" do
|
83
|
+
it "adds a private named mapper method to the instances of the extended class" do
|
84
|
+
expect(class_extended_with_hash_mapper.new.private_methods).to include(:my_private_mapper)
|
85
|
+
end
|
86
|
+
end
|
78
87
|
end
|
79
88
|
end
|
data/spec/yahm/mapping_spec.rb
CHANGED
@@ -92,6 +92,13 @@ describe Yahm::Mapping do
|
|
92
92
|
:target => true
|
93
93
|
})
|
94
94
|
end
|
95
|
+
|
96
|
+
it "provides multiple values as an array of values to lambdas/procs" do
|
97
|
+
expect(Yahm::Mapping.new.map("/source", to: "/array_length", processed_by: lambda { |v| v.length }).translate_hash(source: ["bar", "moep"]))
|
98
|
+
.to eq({
|
99
|
+
:array_length => 2
|
100
|
+
})
|
101
|
+
end
|
95
102
|
end
|
96
103
|
|
97
104
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yahm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Sievers
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|