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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7184233b84f3abc2d69fc8e96c4a4832a8571eb
4
- data.tar.gz: ce29e872ade8e3d0c73a82b1ec938f4f6c6034f1
3
+ metadata.gz: cde533cef9a9f5696f829b4da55c41b7023eb1e8
4
+ data.tar.gz: 76f5639f0a1ea53c726170b2458220a82ef84b88
5
5
  SHA512:
6
- metadata.gz: 1580dd0447512dd5d9604dcefa59702885ced26b49be40b0bf0ce85e2faaf17e57d0a62073ee29af89607c821c8e81c63930888ddee4959f43b23febd2087e70
7
- data.tar.gz: 48c173585006d03c01baa1c929492cce12d6c45a7ff9878d9ef6233528e397a0a4107b3711e3377a46ac3fadafb8ecb46d87fc8f91f42af022b31a56ebd3d54b
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 example
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)
@@ -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 = mapping.instance_eval(&block) || 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
@@ -1,3 +1,3 @@
1
1
  class Yahm
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -18,12 +18,22 @@ describe Yahm::HashMapper do
18
18
  class ClassExtendedWithHashMapper
19
19
  extend Yahm::HashMapper
20
20
 
21
- def sample_method(value)
21
+ def method_of_extended_object(value)
22
22
  true
23
23
  end
24
24
 
25
25
  define_mapper :my_mapper do
26
- map "/source", to: "target", processed_by: proc { |v| _self.sample_method(v) }
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
- target: true
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.8
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-13 00:00:00.000000000 Z
11
+ date: 2014-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler