yahm 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -1
- data/lib/yahm/hash_mapper.rb +1 -1
- data/lib/yahm/version.rb +1 -1
- data/spec/yahm/hash_mapper_spec.rb +24 -4
- 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: cde533cef9a9f5696f829b4da55c41b7023eb1e8
|
4
|
+
data.tar.gz: 76f5639f0a1ea53c726170b2458220a82ef84b88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 650c52e78c5bd303015b228c4c7d8b70ccd9720b4a590d0b67def1cc7b42c80c9818625045fde1509a383657a0c8a557c35a10f6eb39f8aefd49575eb2ba0c48
|
7
|
+
data.tar.gz: 8bb0f418163d1db856780e6dca97736321123f9372a177b7149a81b55145433efbe355243eb363c6416bd7e99b3635c0444771e6015f5ac2dad4da80502373c1
|
data/README.md
CHANGED
@@ -40,7 +40,7 @@ Record.new.from_other_record({ ... })
|
|
40
40
|
=> { :id => "..." }
|
41
41
|
```
|
42
42
|
|
43
|
-
### More advanced
|
43
|
+
### More advanced examples
|
44
44
|
|
45
45
|
```ruby
|
46
46
|
require "yahm"
|
@@ -66,6 +66,35 @@ Record.translated_hash
|
|
66
66
|
=> { :id => "...", :count => ..., :subjects => { :most_important_subject => "..."}, ... }
|
67
67
|
```
|
68
68
|
|
69
|
+
#### Examples for using processed_by
|
70
|
+
|
71
|
+
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
|
+
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
require "yahm"
|
76
|
+
|
77
|
+
class Record
|
78
|
+
extend Yahm::HashMapper
|
79
|
+
|
80
|
+
def process_bool_string(string)
|
81
|
+
string == "true" ? true : (string == "false" ? false : nil)
|
82
|
+
end
|
83
|
+
|
84
|
+
define_mapper :from_other_record do
|
85
|
+
def cheap?(price)
|
86
|
+
price.to_i < 20
|
87
|
+
end
|
88
|
+
|
89
|
+
map "/record/id", to: "id", processed_by: :to_i
|
90
|
+
map "/record/count", to: "count", processed_by: proc { |v| v.to_i }
|
91
|
+
map "/record/subject", to: "subjects", processed_by: :downcase
|
92
|
+
map "/record/price", to: "is_cheap", processed_by: lambda { |v| cheap?(v) }
|
93
|
+
map "/record/is_available", to: "available", processed_by: lambda { |v| _self.process_bool_string(v) }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
69
98
|
## Related work
|
70
99
|
|
71
100
|
* hash_mapper (https://github.com/ismasan/hash_mapper)
|
data/lib/yahm/hash_mapper.rb
CHANGED
@@ -3,7 +3,7 @@ module Yahm::HashMapper
|
|
3
3
|
mapping = Yahm::Mapping.new
|
4
4
|
|
5
5
|
# evaluate the given block in mappings context
|
6
|
-
mapping
|
6
|
+
mapping.instance_eval(&block)
|
7
7
|
|
8
8
|
define_method mapper_method_name do |hash|
|
9
9
|
translated_hash = mapping.translate_hash(hash, _self: self)
|
data/lib/yahm/version.rb
CHANGED
@@ -18,12 +18,22 @@ describe Yahm::HashMapper do
|
|
18
18
|
class ClassExtendedWithHashMapper
|
19
19
|
extend Yahm::HashMapper
|
20
20
|
|
21
|
-
def
|
21
|
+
def method_of_extended_object(value)
|
22
22
|
true
|
23
23
|
end
|
24
24
|
|
25
25
|
define_mapper :my_mapper do
|
26
|
-
|
26
|
+
def mapper_local_method(value)
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
map "/source[0]", to: "target_1", processed_by: proc { |v| mapper_local_method(v) }
|
31
|
+
map "/source[1]", to: "target_2", processed_by: proc { |v| _self.method_of_extended_object(v) }
|
32
|
+
map "/source[2]", to: "target_3", processed_by: proc { |v| another_local_method(v) }
|
33
|
+
|
34
|
+
def another_local_method(value)
|
35
|
+
true
|
36
|
+
end
|
27
37
|
end
|
28
38
|
end
|
29
39
|
end
|
@@ -32,9 +42,19 @@ describe Yahm::HashMapper do
|
|
32
42
|
expect(class_extended_with_hash_mapper.new).to respond_to(:my_mapper)
|
33
43
|
end
|
34
44
|
|
45
|
+
it "provides the context of the defined mapper as \"self\" for \"processed_by\" callables" do
|
46
|
+
expect(class_extended_with_hash_mapper.new.my_mapper({source: ["foo", "bar"]})).to eq({
|
47
|
+
target_1: true,
|
48
|
+
target_2: true,
|
49
|
+
target_3: true
|
50
|
+
})
|
51
|
+
end
|
52
|
+
|
35
53
|
it "provides the context of the extended object as \"_self\" for \"processed_by\" callables" do
|
36
|
-
expect(class_extended_with_hash_mapper.new.my_mapper({source: "foo"})).to eq({
|
37
|
-
|
54
|
+
expect(class_extended_with_hash_mapper.new.my_mapper({source: ["foo", "bar"]})).to eq({
|
55
|
+
target_1: true,
|
56
|
+
target_2: true,
|
57
|
+
target_3: true
|
38
58
|
})
|
39
59
|
end
|
40
60
|
|
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.9
|
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-03-
|
11
|
+
date: 2014-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|