attribute_cartographer 0.0.5 → 0.0.6
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 +15 -18
- data/lib/attribute_cartographer.rb +4 -5
- data/lib/attribute_cartographer/version.rb +1 -1
- data/spec/attribute_cartographer_spec.rb +72 -59
- metadata +5 -5
data/README
CHANGED
@@ -21,32 +21,29 @@ Then run:
|
|
21
21
|
include AttributeCartographer
|
22
22
|
|
23
23
|
# one-way mapping
|
24
|
-
map
|
25
|
-
map
|
26
|
-
map :e, :f, ->(v) { v.downcase }
|
24
|
+
map "UnchangedKey" # map a single key, value untouched
|
25
|
+
map %w{ UnchangedKey1 UnchangedKey2 } # same as above, but for multiple in a single statement
|
27
26
|
|
28
|
-
map
|
27
|
+
map "SameKeyNewValue", ->(v) { v.downcase } # maps the key, using the lambda for the value
|
28
|
+
map %w{ SameKeyNewValue1 SameKeyNewValue2 }, ->(v) { v.upcase } # same as above, but for multiple in a single statement
|
29
29
|
|
30
|
-
map
|
31
|
-
map
|
30
|
+
map "OldKey", "new_key", ->(v) { v.downcase } # maps the left key to the right key, using the lambda for the value
|
31
|
+
map "DowncasedKeyAndValue", ->(k,v) { [k.downcase, v.downcase] } # maps the key and value using the lambda for both
|
32
|
+
map %w{ NewKeyAndValue1 NewKeyAndValue2 }, ->(k,v) { [k.downcase, v.downcase] } # same as the example above, but for multiple in a single statement
|
32
33
|
|
33
34
|
# two-way mapping
|
34
|
-
map
|
35
|
-
map
|
35
|
+
map "AnotherOldKey", "another_new_key" # map the left key to the right key and vice-versa, value untouched
|
36
|
+
map "ThisKey", "to_this_key", ->(v) { v.downcase }, ->(v) { v.upcase } # map the left key to the right key with the first lambda,
|
37
|
+
# and the right key to the left key with the second lambda
|
36
38
|
|
37
|
-
map [:P, :Q, :R], ->(k,v) { [k.downcase, v.downcase] }
|
38
39
|
end
|
39
40
|
|
40
|
-
Mapper.new(
|
41
|
+
Mapper.new("UnchangedKey" => "UnchangedValue", "OldKey" => "OldValue") # etc.
|
41
42
|
|
42
|
-
For each mapping defined, an
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
with value mapping, or with an array with keys and values mapped with lambdas.
|
47
|
-
|
48
|
-
Mapper#original_attributes is the original hash passed in
|
49
|
-
Mapper#mapped_attributes is a hash representing the mapped keys and their mapped values
|
43
|
+
For each mapping defined, an entry in mapped_attributes will be made using the
|
44
|
+
logic defined in the mapping. You also get another method, original_attributes,
|
45
|
+
that retains the entire hash that was passed to #initialize, whether the keys
|
46
|
+
were mapped or not.
|
50
47
|
|
51
48
|
== REQUIREMENTS
|
52
49
|
|
@@ -16,6 +16,7 @@ module AttributeCartographer
|
|
16
16
|
|
17
17
|
passthrough = ->(v) { v }
|
18
18
|
f1 ||= passthrough
|
19
|
+
f2 ||= passthrough
|
19
20
|
|
20
21
|
if Array === from
|
21
22
|
if f1.arity == 1
|
@@ -26,15 +27,14 @@ module AttributeCartographer
|
|
26
27
|
else
|
27
28
|
raise AttributeCartographer::InvalidArgumentError if to && f1.arity == 2
|
28
29
|
|
29
|
-
to
|
30
|
-
@mapper[
|
31
|
-
@mapper[to] = [from, (f2 ? f2 : passthrough)] if to != from
|
30
|
+
@mapper[from] = (f1.arity == 1 ? [to || from, f1] : f1)
|
31
|
+
@mapper[to] = [from, f2] if to && (f1 == f2 || f2 != passthrough)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
module InstanceMethods
|
37
|
-
def initialize attributes
|
37
|
+
def initialize attributes = {}
|
38
38
|
@_original_attributes = attributes
|
39
39
|
@_mapped_attributes = {}
|
40
40
|
|
@@ -67,7 +67,6 @@ module AttributeCartographer
|
|
67
67
|
mapped_key, value = mapping.call(key, attributes[key])
|
68
68
|
end
|
69
69
|
|
70
|
-
self.send :define_singleton_method, mapped_key, ->{ value }
|
71
70
|
@_mapped_attributes[mapped_key] = value
|
72
71
|
end
|
73
72
|
end
|
@@ -12,96 +12,122 @@ describe AttributeCartographer do
|
|
12
12
|
}
|
13
13
|
|
14
14
|
describe "#initialize" do
|
15
|
-
context "with nothing mapped" do
|
16
|
-
it "does not try to map anything when map was not called" do
|
17
|
-
lambda { klass.new(a: :b) }.should_not raise_error
|
18
|
-
end
|
19
|
-
end
|
20
15
|
|
21
|
-
context "with attributes that don't match mapped values" do
|
22
|
-
before { klass.map :a, :b, ->(v) { v + 1 } }
|
23
16
|
|
24
|
-
|
25
|
-
|
26
|
-
end
|
17
|
+
it "doesn't require an argument" do
|
18
|
+
lambda { klass.new }.should_not raise_error
|
27
19
|
end
|
28
20
|
end
|
29
21
|
|
30
22
|
describe "#original_attributes" do
|
31
|
-
it "
|
23
|
+
it "is the attributes given on initialize" do
|
32
24
|
klass.new(a: :b).original_attributes.should == { a: :b }
|
33
25
|
end
|
34
26
|
end
|
35
27
|
|
36
28
|
describe "#mapped_attributes" do
|
37
|
-
|
29
|
+
subject { klass.new(attributes).mapped_attributes }
|
30
|
+
|
31
|
+
context "with no mapping" do
|
32
|
+
let(:attributes) { { "Attribute" => "Value" } }
|
33
|
+
|
34
|
+
it { should be_empty }
|
35
|
+
end
|
36
|
+
|
37
|
+
context "with a mapping" do
|
38
|
+
before { klass.map "Attribute", "attribute" }
|
39
|
+
|
40
|
+
context "and no attributes to map" do
|
41
|
+
let(:attributes) { {} }
|
42
|
+
|
43
|
+
it { should be_empty }
|
44
|
+
end
|
38
45
|
|
39
|
-
|
40
|
-
|
46
|
+
context "and attributes to map" do
|
47
|
+
let(:attributes) { { "Attribute" => "Value" } }
|
48
|
+
|
49
|
+
it { should == { "attribute" => "Value" } }
|
50
|
+
end
|
41
51
|
end
|
42
52
|
end
|
43
53
|
|
44
54
|
describe ".map" do
|
45
55
|
context "with a single argument" do
|
46
56
|
context "and no lambda" do
|
47
|
-
before { klass.map
|
57
|
+
before { klass.map "Attribute" }
|
48
58
|
|
49
|
-
it "creates an
|
50
|
-
klass.new(
|
59
|
+
it "creates an entry in mapped_attributes matching the key name" do
|
60
|
+
klass.new("Attribute" => "Value").mapped_attributes["Attribute"].should == "Value"
|
51
61
|
end
|
52
62
|
end
|
53
63
|
|
54
64
|
context "and a 1-arity lambda" do
|
55
|
-
before { klass.map
|
65
|
+
before { klass.map "Attribute", ->(v) { v.downcase } }
|
56
66
|
|
57
|
-
it "creates an
|
58
|
-
klass.new(
|
67
|
+
it "creates an entry in mapped_attributes matching the key name, mapping the value with the lambda" do
|
68
|
+
klass.new("Attribute" => "Value").mapped_attributes["Attribute"].should == "value"
|
59
69
|
end
|
60
70
|
end
|
61
71
|
|
62
72
|
context "and a 2-arity lambda" do
|
63
|
-
before { klass.map
|
73
|
+
before { klass.map "Attribute", ->(k,v) { [k.downcase, v.downcase] } }
|
64
74
|
|
65
|
-
it "
|
66
|
-
klass.new(
|
75
|
+
it "creates an entry in mapped_attributes using the lambda for the key and value" do
|
76
|
+
klass.new("Attribute" => "Value").mapped_attributes["attribute"].should == "value"
|
67
77
|
end
|
68
78
|
|
69
79
|
it "doesn't raise an error when the attributes hash is missing a mapped key" do
|
70
|
-
lambda { klass.new(
|
80
|
+
lambda { klass.new("OtherAttribute" => "OtherValue") }.should_not raise_error
|
71
81
|
end
|
72
82
|
end
|
73
83
|
end
|
74
84
|
|
75
85
|
context "with two arguments" do
|
76
86
|
context "and no lambda" do
|
77
|
-
before { klass.map
|
87
|
+
before { klass.map "Attribute", "attribute" }
|
78
88
|
|
79
|
-
|
80
|
-
|
89
|
+
context "with attributes matching the left key, passing the value through" do
|
90
|
+
it "maps the left key to the right key" do
|
91
|
+
klass.new("Attribute" => "Value").mapped_attributes["attribute"].should == "Value"
|
92
|
+
end
|
81
93
|
end
|
82
94
|
|
83
|
-
|
84
|
-
|
95
|
+
context "with attributes matching the right key, passing the value through" do
|
96
|
+
it "maps the right key to the left key" do
|
97
|
+
klass.new("attribute" => "Value").mapped_attributes["Attribute"].should == "Value"
|
98
|
+
end
|
85
99
|
end
|
86
100
|
end
|
87
101
|
|
88
102
|
context "and a single lambda" do
|
89
|
-
before { klass.map
|
103
|
+
before { klass.map "Attribute", "attribute", ->(v) { v.downcase } }
|
90
104
|
|
91
|
-
|
92
|
-
|
105
|
+
context "with attributes matching the left key" do
|
106
|
+
it "maps the left key to the right key using the lambda to alter the value" do
|
107
|
+
klass.new("Attribute" => "Value").mapped_attributes["attribute"].should == "value"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "with attributes matching the right key" do
|
112
|
+
it "doesn't map the right key as it's a one-way mapping" do
|
113
|
+
klass.new("attribute" => "value").mapped_attributes["Attribute"].should be_nil
|
114
|
+
end
|
93
115
|
end
|
94
116
|
end
|
95
117
|
|
96
118
|
context "and two lambdas" do
|
97
|
-
before { klass.map
|
119
|
+
before { klass.map "Attribute", "attribute", ->(v) { v.downcase }, ->(v) { v.upcase } }
|
98
120
|
|
99
|
-
|
100
|
-
|
121
|
+
context "with attributes matching the left key" do
|
122
|
+
it "maps the left key to the right key, using the first lambda to map the value" do
|
123
|
+
klass.new("Attribute" => "Value").mapped_attributes["attribute"].should == "value"
|
124
|
+
end
|
101
125
|
end
|
102
126
|
|
103
|
-
|
104
|
-
|
127
|
+
context "with attributes matching the right key, passing the value through" do
|
128
|
+
it "maps the right key to the left key, using the second lambda to map the value" do
|
129
|
+
klass.new("attribute" => "value").mapped_attributes["Attribute"].should == "VALUE"
|
130
|
+
end
|
105
131
|
end
|
106
132
|
end
|
107
133
|
|
@@ -113,38 +139,25 @@ describe AttributeCartographer do
|
|
113
139
|
end
|
114
140
|
|
115
141
|
context "with an array" do
|
116
|
-
|
117
|
-
|
142
|
+
let(:attributes) { { "Attribute1" => "Value1", "Attribute2" => "Value2" } }
|
143
|
+
subject { klass.new(attributes).mapped_attributes }
|
118
144
|
|
119
|
-
|
120
|
-
|
121
|
-
instance.a.should == :a_value
|
122
|
-
instance.b.should == :b_value
|
123
|
-
end
|
145
|
+
context "and no lambda" do
|
146
|
+
before { klass.map %w{ Attribute1 Attribute2 } }
|
124
147
|
|
125
|
-
it
|
126
|
-
lambda { klass.new(a: :a_value).b }.should raise_error(NoMethodError)
|
127
|
-
end
|
148
|
+
it { should == attributes }
|
128
149
|
end
|
129
150
|
|
130
151
|
context "and a 1-arity lambda" do
|
131
|
-
before { klass.map
|
152
|
+
before { klass.map %w{ Attribute1 Attribute2 }, ->(v) { v.downcase } }
|
132
153
|
|
133
|
-
it
|
134
|
-
instance = klass.new(a: "STRING1", b: "STRING2")
|
135
|
-
instance.a.should == "string1"
|
136
|
-
instance.b.should == "string2"
|
137
|
-
end
|
154
|
+
it { should == { "Attribute1" => "value1", "Attribute2" => "value2" } }
|
138
155
|
end
|
139
156
|
|
140
157
|
context "and a 2-arity lambda" do
|
141
|
-
before { klass.map
|
158
|
+
before { klass.map %w{ Attribute1 Attribute2 }, ->(k,v) { [k.downcase, v.downcase] } }
|
142
159
|
|
143
|
-
it
|
144
|
-
instance = klass.new(A: "ASTRING", B: "BSTRING")
|
145
|
-
instance.a.should == "astring"
|
146
|
-
instance.b.should == "bstring"
|
147
|
-
end
|
160
|
+
it { should == { "attribute1" => "value1", "attribute2" => "value2" } }
|
148
161
|
end
|
149
162
|
end
|
150
163
|
end
|
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.6
|
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-
|
12
|
+
date: 2011-06-16 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2153467360 !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: *2153467360
|
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:
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
version: '0'
|
63
63
|
requirements: []
|
64
64
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.8.
|
65
|
+
rubygems_version: 1.8.4
|
66
66
|
signing_key:
|
67
67
|
specification_version: 3
|
68
68
|
summary: Map an attributes hash to methods on Ruby object while transforming the values
|