attribute_cartographer 0.0.1 → 0.0.2
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/.rvmrc +1 -1
- data/README +12 -7
- data/lib/attribute_cartographer.rb +32 -19
- data/lib/attribute_cartographer/VERSION.rb +1 -1
- data/spec/attribute_cartographer_spec.rb +70 -27
- metadata +40 -33
data/.rvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
rvm 1.9.2-p136@
|
|
1
|
+
rvm 1.9.2-p136@attribute_cartographer --create
|
data/README
CHANGED
|
@@ -8,7 +8,7 @@ Attribute Cartographer allows you to map an attributes hash into similarly or di
|
|
|
8
8
|
|
|
9
9
|
== INSTALL
|
|
10
10
|
|
|
11
|
-
Add attribute-
|
|
11
|
+
Add attribute-cartographer to your Gemfile
|
|
12
12
|
|
|
13
13
|
gem 'attribute-cartographer'
|
|
14
14
|
|
|
@@ -20,14 +20,19 @@ Then run:
|
|
|
20
20
|
class Mapper
|
|
21
21
|
include AttributeCartographer
|
|
22
22
|
|
|
23
|
-
map :a
|
|
24
|
-
map :
|
|
23
|
+
map :a
|
|
24
|
+
map :b, ->(v) { v.downcase }
|
|
25
|
+
|
|
26
|
+
map :c, :d
|
|
27
|
+
map :e, :f, ->(v) { v.downcase }
|
|
28
|
+
map :g, :h, ->(v) { v.downcase }, ->(v) { v.upcase }
|
|
29
|
+
|
|
30
|
+
map [:i, :j, :k]
|
|
31
|
+
map [:l, :m, :n], ->(v) { v.upcase }
|
|
25
32
|
end
|
|
26
33
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
account.d # => "Mapper"
|
|
30
|
-
account.original_attributes # => { a: 123, c: "Mapper" }
|
|
34
|
+
mapper = Mapper.new attributes
|
|
35
|
+
mapper.original_attributes # => {:a=>123, :c=>"Mapper", :e=>"Eee!", :f=>"F.", :g=>"Gee!"}
|
|
31
36
|
|
|
32
37
|
== REQUIREMENTS
|
|
33
38
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
module AttributeCartographer
|
|
2
|
-
class InvalidBlockArityError < StandardError; end
|
|
3
2
|
class InvalidArgumentError < StandardError; end
|
|
4
3
|
|
|
5
4
|
class << self
|
|
@@ -12,19 +11,19 @@ module AttributeCartographer
|
|
|
12
11
|
module ClassMethods
|
|
13
12
|
def map *args
|
|
14
13
|
@mapper ||= {}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@mapper.merge!
|
|
25
|
-
|
|
26
|
-
from
|
|
27
|
-
@mapper.merge!
|
|
14
|
+
|
|
15
|
+
(from, to), (f1, f2) = args.partition { |a| !(Proc === a) }
|
|
16
|
+
|
|
17
|
+
raise AttributeCartographer::InvalidArgumentError if [f1,f2].compact.any? { |f| f.arity > 1 }
|
|
18
|
+
|
|
19
|
+
f1 ||= ->(v) { v }
|
|
20
|
+
to ||= from
|
|
21
|
+
|
|
22
|
+
if Array === from
|
|
23
|
+
from.each { |key| @mapper.merge! key => [key, f1] }
|
|
24
|
+
else
|
|
25
|
+
@mapper.merge! from => [to, f1]
|
|
26
|
+
@mapper.merge! to => [from, f2] if f2
|
|
28
27
|
end
|
|
29
28
|
end
|
|
30
29
|
end
|
|
@@ -32,12 +31,9 @@ module AttributeCartographer
|
|
|
32
31
|
module InstanceMethods
|
|
33
32
|
def initialize attributes
|
|
34
33
|
@_original_attributes = attributes
|
|
35
|
-
|
|
34
|
+
@_mapped_attributes = {}
|
|
36
35
|
|
|
37
|
-
|
|
38
|
-
value = attributes.has_key?(from) ? block.call(attributes[from]) : nil
|
|
39
|
-
self.send :define_singleton_method, meth, ->{ value }
|
|
40
|
-
} if mapper
|
|
36
|
+
map_attributes! attributes
|
|
41
37
|
|
|
42
38
|
super
|
|
43
39
|
end
|
|
@@ -45,5 +41,22 @@ module AttributeCartographer
|
|
|
45
41
|
def original_attributes
|
|
46
42
|
@_original_attributes
|
|
47
43
|
end
|
|
44
|
+
|
|
45
|
+
def mapped_attributes
|
|
46
|
+
@_mapped_attributes
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def map_attributes! attributes
|
|
52
|
+
mapper = self.class.instance_variable_get(:@mapper)
|
|
53
|
+
|
|
54
|
+
mapper.each { |original_key, (mapped_key, block)|
|
|
55
|
+
value = attributes.has_key?(original_key) ? block.call(attributes[original_key]) : nil
|
|
56
|
+
self.send :define_singleton_method, mapped_key, ->{ value }
|
|
57
|
+
|
|
58
|
+
@_mapped_attributes.merge! mapped_key => value
|
|
59
|
+
} if mapper
|
|
60
|
+
end
|
|
48
61
|
end
|
|
49
62
|
end
|
|
@@ -33,50 +33,93 @@ describe AttributeCartographer do
|
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
+
describe "#mapped_attributes" do
|
|
37
|
+
before { klass.map :a, :b, ->(v) { v + 1 } }
|
|
38
|
+
|
|
39
|
+
it "returns any attributes mapped by the mapper" do
|
|
40
|
+
klass.new(a: 1).mapped_attributes.should == { b: 2 }
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
36
44
|
describe ".map" do
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
context "with a single argument" do
|
|
46
|
+
context "and no lambda" do
|
|
47
|
+
before { klass.map :a }
|
|
40
48
|
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
it "creates an instance method matching the key name" do
|
|
50
|
+
klass.new(:a => :a_value).a.should == :a_value
|
|
51
|
+
end
|
|
43
52
|
end
|
|
44
|
-
end
|
|
45
53
|
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
context "and a lambda" do
|
|
55
|
+
before { klass.map :a, ->(v) { v.downcase } }
|
|
48
56
|
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
it "creates an instance method matching the key name, mapping the value with the lambda" do
|
|
58
|
+
klass.new(:a => "STRING").a.should == "string"
|
|
59
|
+
end
|
|
51
60
|
end
|
|
52
61
|
end
|
|
53
62
|
|
|
54
|
-
context "with
|
|
55
|
-
|
|
63
|
+
context "with two arguments" do
|
|
64
|
+
context "and no lambda" do
|
|
65
|
+
before { klass.map :a, :b }
|
|
56
66
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
instance.b.should == :b_value
|
|
67
|
+
it "creates an instance method matching the key name" do
|
|
68
|
+
klass.new(:a => :a_value).b.should == :a_value
|
|
69
|
+
end
|
|
61
70
|
end
|
|
62
71
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
context "and a single lambda" do
|
|
73
|
+
before { klass.map :a, :b, ->(v) { v.downcase } }
|
|
74
|
+
|
|
75
|
+
it "creates an instance method matching the key name, mapping the value with the lambda" do
|
|
76
|
+
klass.new(:a => "STRING").b.should == "string"
|
|
77
|
+
end
|
|
66
78
|
end
|
|
67
|
-
end
|
|
68
79
|
|
|
69
|
-
|
|
70
|
-
|
|
80
|
+
context "and two lambdas" do
|
|
81
|
+
before { klass.map :a, :b, ->(v) { v.downcase }, ->(v) { v.upcase } }
|
|
71
82
|
|
|
72
|
-
|
|
73
|
-
|
|
83
|
+
it "creates an instance method matching the second key name, mapping the value with the first lambda" do
|
|
84
|
+
klass.new(a: "STRING").b.should == "string"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "creates an instance method matching the first key name, mapping the value with the second lambda" do
|
|
88
|
+
klass.new(b: "string").a.should == "STRING"
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
context "and a >1-arity lambda" do
|
|
93
|
+
it "raises an error" do
|
|
94
|
+
lambda { klass.map :a, :b, ->(k,v) { v + 1 } }.should raise_error(AttributeCartographer::InvalidArgumentError)
|
|
95
|
+
end
|
|
74
96
|
end
|
|
75
97
|
end
|
|
76
98
|
|
|
77
|
-
context "with
|
|
78
|
-
|
|
79
|
-
|
|
99
|
+
context "with an array" do
|
|
100
|
+
context "with no lambda" do
|
|
101
|
+
before { klass.map [:a, :b] }
|
|
102
|
+
|
|
103
|
+
it "creates a method named for each key" do
|
|
104
|
+
instance = klass.new(a: :a_value, b: :b_value)
|
|
105
|
+
instance.a.should == :a_value
|
|
106
|
+
instance.b.should == :b_value
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "makes nil methods for mapped keys which had no attributes passed in for them" do
|
|
110
|
+
instance = klass.new(a: :a_value)
|
|
111
|
+
instance.b.should == nil
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
context "and a lambda" do
|
|
116
|
+
before { klass.map [:a, :b], ->(v) { v.downcase } }
|
|
117
|
+
|
|
118
|
+
it "creates a method named for each key using the lambda to map the values" do
|
|
119
|
+
instance = klass.new(a: "STRING1", b: "STRING2")
|
|
120
|
+
instance.a.should == "string1"
|
|
121
|
+
instance.b.should == "string2"
|
|
122
|
+
end
|
|
80
123
|
end
|
|
81
124
|
end
|
|
82
125
|
end
|
metadata
CHANGED
|
@@ -1,35 +1,39 @@
|
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: attribute_cartographer
|
|
3
|
-
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.1
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
5
4
|
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
|
-
|
|
12
|
+
|
|
13
|
+
date: 2011-05-10 00:00:00 -04:00
|
|
14
|
+
default_executable:
|
|
15
|
+
dependencies:
|
|
16
|
+
- !ruby/object:Gem::Dependency
|
|
15
17
|
name: rspec
|
|
16
|
-
|
|
18
|
+
prerelease: false
|
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
17
20
|
none: false
|
|
18
|
-
requirements:
|
|
19
|
-
- -
|
|
20
|
-
- !ruby/object:Gem::Version
|
|
21
|
-
version:
|
|
21
|
+
requirements:
|
|
22
|
+
- - ">="
|
|
23
|
+
- !ruby/object:Gem::Version
|
|
24
|
+
version: "2.5"
|
|
22
25
|
type: :development
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
or differently named methods, using an optional block to map the values as well.
|
|
27
|
-
email:
|
|
26
|
+
version_requirements: *id001
|
|
27
|
+
description: AttributeCartographer allows you to map an attributes hash into similarly or differently named methods, using an optional block to map the values as well.
|
|
28
|
+
email:
|
|
28
29
|
- krishicks@gmail.com
|
|
29
30
|
executables: []
|
|
31
|
+
|
|
30
32
|
extensions: []
|
|
33
|
+
|
|
31
34
|
extra_rdoc_files: []
|
|
32
|
-
|
|
35
|
+
|
|
36
|
+
files:
|
|
33
37
|
- .gitignore
|
|
34
38
|
- .rspec
|
|
35
39
|
- .rvmrc
|
|
@@ -42,31 +46,34 @@ files:
|
|
|
42
46
|
- lib/attribute_cartographer/VERSION.rb
|
|
43
47
|
- spec/attribute_cartographer_spec.rb
|
|
44
48
|
- spec/spec_helper.rb
|
|
49
|
+
has_rdoc: true
|
|
45
50
|
homepage: https://github.com/krishicks/attribute-cartographer
|
|
46
51
|
licenses: []
|
|
52
|
+
|
|
47
53
|
post_install_message:
|
|
48
54
|
rdoc_options: []
|
|
49
|
-
|
|
55
|
+
|
|
56
|
+
require_paths:
|
|
50
57
|
- lib
|
|
51
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
59
|
none: false
|
|
53
|
-
requirements:
|
|
54
|
-
- -
|
|
55
|
-
- !ruby/object:Gem::Version
|
|
56
|
-
version:
|
|
57
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: "0"
|
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
65
|
none: false
|
|
59
|
-
requirements:
|
|
60
|
-
- -
|
|
61
|
-
- !ruby/object:Gem::Version
|
|
62
|
-
version:
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: "0"
|
|
63
70
|
requirements: []
|
|
71
|
+
|
|
64
72
|
rubyforge_project:
|
|
65
|
-
rubygems_version: 1.
|
|
73
|
+
rubygems_version: 1.6.2
|
|
66
74
|
signing_key:
|
|
67
75
|
specification_version: 3
|
|
68
|
-
summary: Map an attributes hash to methods on Ruby object while transforming the values
|
|
69
|
-
|
|
70
|
-
test_files:
|
|
76
|
+
summary: Map an attributes hash to methods on Ruby object while transforming the values to suit.
|
|
77
|
+
test_files:
|
|
71
78
|
- spec/attribute_cartographer_spec.rb
|
|
72
79
|
- spec/spec_helper.rb
|