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 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 block to map the values as well.
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
- mapper = Mapper.new attributes
35
- mapper.original_attributes # => {:a=>123, :c=>"Mapper", :e=>"Eee!", :f=>"F.", :g=>"Gee!"}
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 block to map the values as well.}
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
 
@@ -1,3 +1,3 @@
1
1
  module AttributeCartographer
2
- VERSION = "0.0.2" unless defined?(::AttributeCartographer::VERSION)
2
+ VERSION = "0.0.4" unless defined?(::AttributeCartographer::VERSION)
3
3
  end
@@ -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
- from.each { |key| @mapper.merge! key => [key, f1] }
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
- @mapper.merge! from => [to, f1]
26
- @mapper.merge! to => [from, f2] if f2
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.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 }
59
+ (mapper.keys & attributes.keys).each do |key|
60
+ mapping = mapper[key]
57
61
 
58
- @_mapped_attributes.merge! mapped_key => value
59
- } if mapper
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 "maps attributes to nil when no mappable attribute was passed in" do
25
- klass.new(c: :d).b.should be_nil
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 "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
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
- date: 2011-05-10 00:00:00 -04:00
14
- default_executable:
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
- prerelease: false
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: "2.5"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '2.5'
25
22
  type: :development
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:
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: "0"
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: "0"
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
70
63
  requirements: []
71
-
72
64
  rubyforge_project:
73
- rubygems_version: 1.6.2
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 to suit.
77
- test_files:
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