attribute_cartographer 0.0.2 → 0.0.4
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.
- data/README +13 -3
- data/attribute_cartographer.gemspec +1 -1
- data/lib/attribute_cartographer/VERSION.rb +1 -1
- data/lib/attribute_cartographer.rb +23 -11
- data/spec/attribute_cartographer_spec.rb +28 -7
- metadata +33 -40
data/README
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
== DESCRIPTION
|
6
6
|
|
7
|
-
Attribute Cartographer allows you to map an attributes hash into similarly or differently named methods, using an optional
|
7
|
+
Attribute Cartographer allows you to map an attributes hash into similarly or differently named methods, using an optional lambda to map the values as well.
|
8
8
|
|
9
9
|
== INSTALL
|
10
10
|
|
@@ -22,6 +22,7 @@ Then run:
|
|
22
22
|
|
23
23
|
map :a
|
24
24
|
map :b, ->(v) { v.downcase }
|
25
|
+
map :O, ->(k,v) { [k.downcase, v.downcase] } # You must return a 2-arg array
|
25
26
|
|
26
27
|
map :c, :d
|
27
28
|
map :e, :f, ->(v) { v.downcase }
|
@@ -29,10 +30,19 @@ Then run:
|
|
29
30
|
|
30
31
|
map [:i, :j, :k]
|
31
32
|
map [:l, :m, :n], ->(v) { v.upcase }
|
33
|
+
map [:P, :Q, :R], ->(k,v) { [k.downcase, v.downcase] } # You must return a 2-arg array
|
32
34
|
end
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
+
Mapper.new(a: 2, b: "STRING")
|
37
|
+
|
38
|
+
For each mapping defined, an instance method is created that returns the mapped
|
39
|
+
value. This can be mapped directly, with a lambda to map the value or key and
|
40
|
+
value, with an explicit key mapping and lambda value mapping (with an optional
|
41
|
+
reverse-value map lambda), with a directly-mapped array, a directly-mapped array
|
42
|
+
with value mapping, or with an array with keys and values mapped with lambdas.
|
43
|
+
|
44
|
+
Mapper#original_attributes is the original hash passed in
|
45
|
+
Mapper#mapped_attributes is a hash representing the mapped keys and their mapped values
|
36
46
|
|
37
47
|
== REQUIREMENTS
|
38
48
|
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.email = ["krishicks@gmail.com"]
|
11
11
|
s.homepage = "https://github.com/krishicks/attribute-cartographer"
|
12
12
|
s.summary = %q{Map an attributes hash to methods on Ruby object while transforming the values to suit.}
|
13
|
-
s.description = %q{AttributeCartographer allows you to map an attributes hash into similarly or differently named methods, using an optional
|
13
|
+
s.description = %q{AttributeCartographer allows you to map an attributes hash into similarly or differently named methods, using an optional lambda to map the values as well.}
|
14
14
|
|
15
15
|
s.add_development_dependency('rspec', '>= 2.5')
|
16
16
|
|
@@ -14,16 +14,20 @@ module AttributeCartographer
|
|
14
14
|
|
15
15
|
(from, to), (f1, f2) = args.partition { |a| !(Proc === a) }
|
16
16
|
|
17
|
-
raise AttributeCartographer::InvalidArgumentError if [f1,f2].compact.any? { |f| f.arity > 1 }
|
18
|
-
|
19
17
|
f1 ||= ->(v) { v }
|
20
|
-
to ||= from
|
21
18
|
|
22
19
|
if Array === from
|
23
|
-
|
20
|
+
if f1.arity == 1
|
21
|
+
from.each { |k| @mapper[k] = [k, f1] }
|
22
|
+
else
|
23
|
+
from.each { |k| @mapper[k] = f1 }
|
24
|
+
end
|
24
25
|
else
|
25
|
-
|
26
|
-
|
26
|
+
raise AttributeCartographer::InvalidArgumentError if to && f1.arity == 2
|
27
|
+
|
28
|
+
to ||= from
|
29
|
+
@mapper[from] = (f1.arity == 1 ? [to, f1] : f1)
|
30
|
+
@mapper[to] = [from, f2] if f2
|
27
31
|
end
|
28
32
|
end
|
29
33
|
end
|
@@ -50,13 +54,21 @@ module AttributeCartographer
|
|
50
54
|
|
51
55
|
def map_attributes! attributes
|
52
56
|
mapper = self.class.instance_variable_get(:@mapper)
|
57
|
+
return unless mapper
|
53
58
|
|
54
|
-
mapper.
|
55
|
-
|
56
|
-
self.send :define_singleton_method, mapped_key, ->{ value }
|
59
|
+
(mapper.keys & attributes.keys).each do |key|
|
60
|
+
mapping = mapper[key]
|
57
61
|
|
58
|
-
|
59
|
-
|
62
|
+
if Array === mapping
|
63
|
+
mapped_key, f = mapping
|
64
|
+
value = f.call(attributes[key])
|
65
|
+
else
|
66
|
+
mapped_key, value = mapping.call(key, attributes[key])
|
67
|
+
end
|
68
|
+
|
69
|
+
self.send :define_singleton_method, mapped_key, ->{ value }
|
70
|
+
@_mapped_attributes[mapped_key] = value
|
71
|
+
end
|
60
72
|
end
|
61
73
|
end
|
62
74
|
end
|
@@ -21,8 +21,8 @@ describe AttributeCartographer do
|
|
21
21
|
context "with attributes that don't match mapped values" do
|
22
22
|
before { klass.map :a, :b, ->(v) { v + 1 } }
|
23
23
|
|
24
|
-
it "
|
25
|
-
klass.new(c: :d).b.should
|
24
|
+
it "doesn't map attributes when no mappable attribute was passed in" do
|
25
|
+
lambda { klass.new(c: :d).b }.should raise_error(NoMethodError)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -51,13 +51,25 @@ describe AttributeCartographer do
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
context "and a lambda" do
|
54
|
+
context "and a 1-arity lambda" do
|
55
55
|
before { klass.map :a, ->(v) { v.downcase } }
|
56
56
|
|
57
57
|
it "creates an instance method matching the key name, mapping the value with the lambda" do
|
58
58
|
klass.new(:a => "STRING").a.should == "string"
|
59
59
|
end
|
60
60
|
end
|
61
|
+
|
62
|
+
context "and a 2-arity lambda" do
|
63
|
+
before { klass.map :A, ->(k,v) { [k.downcase, v.downcase] } }
|
64
|
+
|
65
|
+
it "maps the key and value using the lambda and creates an instance method accordingly" do
|
66
|
+
klass.new(A: "STRING").a.should == "string"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "doesn't raise an error when the attributes hash is missing a mapped key" do
|
70
|
+
lambda { klass.new(c: 2) }.should_not raise_error
|
71
|
+
end
|
72
|
+
end
|
61
73
|
end
|
62
74
|
|
63
75
|
context "with two arguments" do
|
@@ -106,13 +118,12 @@ describe AttributeCartographer do
|
|
106
118
|
instance.b.should == :b_value
|
107
119
|
end
|
108
120
|
|
109
|
-
it "
|
110
|
-
|
111
|
-
instance.b.should == nil
|
121
|
+
it "doesn't raise an error when the attributes hash is missing a mapped key" do
|
122
|
+
lambda { klass.new(a: :a_value).b }.should raise_error(NoMethodError)
|
112
123
|
end
|
113
124
|
end
|
114
125
|
|
115
|
-
context "and a lambda" do
|
126
|
+
context "and a 1-arity lambda" do
|
116
127
|
before { klass.map [:a, :b], ->(v) { v.downcase } }
|
117
128
|
|
118
129
|
it "creates a method named for each key using the lambda to map the values" do
|
@@ -121,6 +132,16 @@ describe AttributeCartographer do
|
|
121
132
|
instance.b.should == "string2"
|
122
133
|
end
|
123
134
|
end
|
135
|
+
|
136
|
+
context "and a 2-arity lambda" do
|
137
|
+
before { klass.map [:A, :B], ->(k,v) { [k.downcase, v.downcase] } }
|
138
|
+
|
139
|
+
it "maps the key and value using the lambda and creates an instance method accordingly" do
|
140
|
+
instance = klass.new(A: "ASTRING", B: "BSTRING")
|
141
|
+
instance.a.should == "astring"
|
142
|
+
instance.b.should == "bstring"
|
143
|
+
end
|
144
|
+
end
|
124
145
|
end
|
125
146
|
end
|
126
147
|
end
|
metadata
CHANGED
@@ -1,39 +1,35 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_cartographer
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.2
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Kris Hicks
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-05-13 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: rspec
|
18
|
-
|
19
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2160682200 !ruby/object:Gem::Requirement
|
20
17
|
none: false
|
21
|
-
requirements:
|
22
|
-
- -
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.5'
|
25
22
|
type: :development
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2160682200
|
25
|
+
description: AttributeCartographer allows you to map an attributes hash into similarly
|
26
|
+
or differently named methods, using an optional lambda to map the values as well.
|
27
|
+
email:
|
29
28
|
- krishicks@gmail.com
|
30
29
|
executables: []
|
31
|
-
|
32
30
|
extensions: []
|
33
|
-
|
34
31
|
extra_rdoc_files: []
|
35
|
-
|
36
|
-
files:
|
32
|
+
files:
|
37
33
|
- .gitignore
|
38
34
|
- .rspec
|
39
35
|
- .rvmrc
|
@@ -46,34 +42,31 @@ files:
|
|
46
42
|
- lib/attribute_cartographer/VERSION.rb
|
47
43
|
- spec/attribute_cartographer_spec.rb
|
48
44
|
- spec/spec_helper.rb
|
49
|
-
has_rdoc: true
|
50
45
|
homepage: https://github.com/krishicks/attribute-cartographer
|
51
46
|
licenses: []
|
52
|
-
|
53
47
|
post_install_message:
|
54
48
|
rdoc_options: []
|
55
|
-
|
56
|
-
require_paths:
|
49
|
+
require_paths:
|
57
50
|
- lib
|
58
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
52
|
none: false
|
60
|
-
requirements:
|
61
|
-
- -
|
62
|
-
- !ruby/object:Gem::Version
|
63
|
-
version:
|
64
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
58
|
none: false
|
66
|
-
requirements:
|
67
|
-
- -
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version:
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
70
63
|
requirements: []
|
71
|
-
|
72
64
|
rubyforge_project:
|
73
|
-
rubygems_version: 1.
|
65
|
+
rubygems_version: 1.8.2
|
74
66
|
signing_key:
|
75
67
|
specification_version: 3
|
76
|
-
summary: Map an attributes hash to methods on Ruby object while transforming the values
|
77
|
-
|
68
|
+
summary: Map an attributes hash to methods on Ruby object while transforming the values
|
69
|
+
to suit.
|
70
|
+
test_files:
|
78
71
|
- spec/attribute_cartographer_spec.rb
|
79
72
|
- spec/spec_helper.rb
|