yahm 0.0.11 → 0.0.12

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: 266c6fca9bf3474f6fb9766a9030a6bb5ebf700d
4
- data.tar.gz: 8f38ca8b5fff309b1606d4d5fd42e8c90958d0a8
3
+ metadata.gz: d8e9c9372841f53ea0466a68d75609ca2c44fcb9
4
+ data.tar.gz: e6161948ad5bcc0772ef79b03df84de245afcde7
5
5
  SHA512:
6
- metadata.gz: e8e2f2e2a39f7e59a874838342b735b5b187f6e4b5f9309963d53ffa392f09ebd68cf686b18d4f7d264f5db42dbbe0735619ddb2ac2205a936c245f6bb14367c
7
- data.tar.gz: 76ead024eb29b194fde92eae0bdc8c9315b523e511eb2483b10653d730db17251a2c14ddcc0bca152b193cd23cbb051fdd44a94cedf56848ffbbc31d22f41704
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 processed_by
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
 
@@ -14,6 +14,10 @@ module Yahm::HashMapper
14
14
  translated_hash
15
15
  end
16
16
 
17
+ if options[:private]
18
+ private mapper_method_name.to_sym
19
+ end
20
+
17
21
  self
18
22
  end
19
23
  end
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 source_value.is_a?(Array)
50
- source_value.map(&symbol_or_callable)
49
+ source_value = if symbol_or_callable.is_a?(Symbol)
50
+ source_value.send(symbol_or_callable)
51
51
  else
52
- if symbol_or_callable.is_a?(Symbol)
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
@@ -1,3 +1,3 @@
1
1
  class Yahm
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -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
- class ClassExtendedWithHashMapper
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
- class ClassExtendedWithHashMapper
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
@@ -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.11
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-03-14 00:00:00.000000000 Z
11
+ date: 2014-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler