hash19 0.1.0 → 0.1.1

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: bd7aefc6e059fa2ba78256565a3e4c013aefa45b
4
- data.tar.gz: acef49c8de9299b6e93823dbea8bca8f00b481fb
3
+ metadata.gz: 209c3fa297b355615a5ad54aae279fa620d4731e
4
+ data.tar.gz: 592946e41fb02e0f786a8bb16736d32d94cf26a9
5
5
  SHA512:
6
- metadata.gz: 4c40e4807096a11ad6f38c87f854b0298acda2ea8c9bb342ceb55a2f462fe562150ed637a2eee125c33427abd5790c79803740a84f717c847be80fab2743cc15
7
- data.tar.gz: ae8faf7d7f16f6eac88a5a8ddfc70ca1010e6673d6d3da23fc112c88a1e8fda1d84cc09cc75994c8f231962105378b7dd6b72570bdfb5d7d0532ff36005e34db
6
+ metadata.gz: 477f432e12b4c9a1874fee7bea50300fe27482b363edd188501e351a6f31a7cd28f1884b45281ae6373d5475391728182c552b2d1c1d8ecc9502d0f1ea6fffeb
7
+ data.tar.gz: 36481cbdd34c529faeb9c0516931713b8b6fa404380934d46e3a9e98425e2784f87aced2f7e5eec30eb411379fa26528c5b7e0af716529256acb27f28d644d30
@@ -20,6 +20,7 @@ module Hash19
20
20
  hash = payload.with_indifferent_access
21
21
  @hash19 = hash.slice(*self.class.keys)
22
22
  resolve_aliases if self.class.aliases.present?
23
+ resolve_transformers if self.class.transformers.present?
23
24
  resolve_has_one(hash) if self.class.one_assocs.present?
24
25
  resolve_has_many(hash) if self.class.many_assocs.present?
25
26
  end
@@ -3,6 +3,7 @@ module Hash19
3
3
 
4
4
  attr_accessor :keys
5
5
  attr_accessor :aliases
6
+ attr_accessor :transformers
6
7
  attr_accessor :one_assocs
7
8
  attr_accessor :many_assocs
8
9
  attr_accessor :injections
@@ -19,6 +20,10 @@ module Hash19
19
20
  @aliases[opts[:key]] = name
20
21
  add_attributes(opts[:key])
21
22
  end
23
+ if opts.has_key?(:transform)
24
+ @transformers ||= {}
25
+ @transformers[name] = opts[:transform]
26
+ end
22
27
  end
23
28
 
24
29
  def has_one(name, opts = {})
@@ -40,6 +40,16 @@ module Hash19
40
40
  end
41
41
  end
42
42
 
43
+ def resolve_transformers
44
+ self.class.transformers.each do |attribute, proc|
45
+ if proc.is_a?(Proc)
46
+ @hash19[attribute] = proc.call(@hash19[attribute])
47
+ else
48
+ raise "[Attribute: #{attribute}] The transform function should be a lambda!"
49
+ end
50
+ end
51
+ end
52
+
43
53
  def resolve_injections(hash)
44
54
  together do
45
55
  async_injections.each do |opts|
@@ -1,3 +1,3 @@
1
1
  module Hash19
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Transformers' do
4
+
5
+ class Testable
6
+ include Hash19
7
+ end
8
+
9
+ it 'should be able to call the transform function if present' do
10
+ class Transformer < Testable
11
+ attribute :name, transform: lambda { |x| x.upcase }
12
+ end
13
+
14
+ tt = Transformer.new(name: 'rc')
15
+ expect(tt.to_h).to eq('name' => 'RC')
16
+ end
17
+
18
+ it 'should whine if transform function is not a proc' do
19
+ class BadTransformer < Testable
20
+ attribute :name, transform: {}
21
+ end
22
+
23
+ expect do
24
+ tt = BadTransformer.new(name: 'rc')
25
+ tt.to_h
26
+ end.to raise_error
27
+ end
28
+
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash19
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - RC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-25 00:00:00.000000000 Z
11
+ date: 2015-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -151,6 +151,7 @@ files:
151
151
  - spec/hash19/core_spec.rb
152
152
  - spec/hash19/future_spec.rb
153
153
  - spec/hash19/injection_spec.rb
154
+ - spec/hash19/transform_spec.rb
154
155
  - spec/spec_helper.rb
155
156
  homepage: https://github.com/rcdexta/hash19
156
157
  licenses:
@@ -184,4 +185,5 @@ test_files:
184
185
  - spec/hash19/core_spec.rb
185
186
  - spec/hash19/future_spec.rb
186
187
  - spec/hash19/injection_spec.rb
188
+ - spec/hash19/transform_spec.rb
187
189
  - spec/spec_helper.rb