attribute_cartographer 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
data/README
CHANGED
@@ -20,17 +20,21 @@ Then run:
|
|
20
20
|
class Mapper
|
21
21
|
include AttributeCartographer
|
22
22
|
|
23
|
+
# one-way mapping
|
23
24
|
map :a
|
24
|
-
map :b,
|
25
|
-
map :O, ->(k,v) { [k.downcase, v.downcase] } # You must return a 2-arg array
|
26
|
-
|
27
|
-
map :c, :d
|
25
|
+
map :b, ->(v) { v.downcase }
|
28
26
|
map :e, :f, ->(v) { v.downcase }
|
29
|
-
|
27
|
+
|
28
|
+
map :O, ->(k,v) { [k.downcase, v.downcase] }
|
30
29
|
|
31
30
|
map [:i, :j, :k]
|
32
31
|
map [:l, :m, :n], ->(v) { v.upcase }
|
33
|
-
|
32
|
+
|
33
|
+
# two-way mapping
|
34
|
+
map :c, :d
|
35
|
+
map :g, :h, ->(v) { v.downcase }, ->(v) { v.upcase }
|
36
|
+
|
37
|
+
map [:P, :Q, :R], ->(k,v) { [k.downcase, v.downcase] }
|
34
38
|
end
|
35
39
|
|
36
40
|
Mapper.new(a: 2, b: "STRING")
|
@@ -14,7 +14,8 @@ module AttributeCartographer
|
|
14
14
|
|
15
15
|
(from, to), (f1, f2) = args.partition { |a| !(Proc === a) }
|
16
16
|
|
17
|
-
|
17
|
+
passthrough = ->(v) { v }
|
18
|
+
f1 ||= passthrough
|
18
19
|
|
19
20
|
if Array === from
|
20
21
|
if f1.arity == 1
|
@@ -27,7 +28,7 @@ module AttributeCartographer
|
|
27
28
|
|
28
29
|
to ||= from
|
29
30
|
@mapper[from] = (f1.arity == 1 ? [to, f1] : f1)
|
30
|
-
@mapper[to] = [from, f2] if
|
31
|
+
@mapper[to] = [from, (f2 ? f2 : passthrough)] if to != from
|
31
32
|
end
|
32
33
|
end
|
33
34
|
end
|
@@ -76,8 +76,12 @@ describe AttributeCartographer do
|
|
76
76
|
context "and no lambda" do
|
77
77
|
before { klass.map :a, :b }
|
78
78
|
|
79
|
-
it "
|
80
|
-
klass.new(:a => :a_value).b.should == :a_value
|
79
|
+
it "maps the from to the to" do
|
80
|
+
klass.new(:a => :a_value).mapped_attributes[:b].should == :a_value
|
81
|
+
end
|
82
|
+
|
83
|
+
it "maps the to to the from" do
|
84
|
+
klass.new(:b => :b_value).mapped_attributes[:a].should == :b_value
|
81
85
|
end
|
82
86
|
end
|
83
87
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_cartographer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-05-
|
12
|
+
date: 2011-05-25 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153280180 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '2.5'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2153280180
|
25
25
|
description: AttributeCartographer allows you to map an attributes hash into similarly
|
26
26
|
or differently named methods, using an optional lambda to map the values as well.
|
27
27
|
email:
|
@@ -39,7 +39,7 @@ files:
|
|
39
39
|
- Rakefile
|
40
40
|
- attribute_cartographer.gemspec
|
41
41
|
- lib/attribute_cartographer.rb
|
42
|
-
- lib/attribute_cartographer/
|
42
|
+
- lib/attribute_cartographer/version.rb
|
43
43
|
- spec/attribute_cartographer_spec.rb
|
44
44
|
- spec/spec_helper.rb
|
45
45
|
homepage: https://github.com/krishicks/attribute-cartographer
|