attribute_cartographer 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm 1.9.2-p136@attribute_cartography --create
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-cartography to your Gemfile
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, :b
24
- map :c, :d, ->(v) { v.downcase }
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
- account = Mapper.new a: 123, c: "Mapper"
28
- account.b # => 123
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
- block = (Proc === args.last) ? args.pop : ->(v) { v }
16
-
17
- raise AttributeCartographer::InvalidArgumentError if block.arity > 1
18
-
19
- if Array === args.first
20
- raise AttributeCartographer::InvalidArgumentError if args.first.empty?
21
- args.first.each { |arg| @mapper.merge! arg => [arg, block] }
22
- elsif args.size == 2
23
- from, to = args
24
- @mapper.merge! from => [to, block]
25
- elsif args.size == 1
26
- from = args.pop
27
- @mapper.merge! from => [from, block]
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
- mapper = self.class.instance_variable_get(:@mapper)
34
+ @_mapped_attributes = {}
36
35
 
37
- mapper.each { |from, (meth, block)|
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
@@ -1,3 +1,3 @@
1
1
  module AttributeCartographer
2
- VERSION = "0.0.1" unless defined?(::AttributeCartographer::VERSION)
2
+ VERSION = "0.0.2" unless defined?(::AttributeCartographer::VERSION)
3
3
  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
- # tests about accepting strings or keys as both args
38
- context "with a single argument given" do
39
- before { klass.map :a }
45
+ context "with a single argument" do
46
+ context "and no lambda" do
47
+ before { klass.map :a }
40
48
 
41
- it "creates an instance method matching the key name" do
42
- klass.new(:a => :a_value).a.should == :a_value
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
- context "with an empty array" do
47
- subject { klass.map [] }
54
+ context "and a lambda" do
55
+ before { klass.map :a, ->(v) { v.downcase } }
48
56
 
49
- it "should raise an error" do
50
- lambda { klass.map [] }.should raise_error(AttributeCartographer::InvalidArgumentError)
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 a non-empty array" do
55
- before { klass.map [:a, :b] }
63
+ context "with two arguments" do
64
+ context "and no lambda" do
65
+ before { klass.map :a, :b }
56
66
 
57
- it "creates a method named for each key" do
58
- instance = klass.new(a: :a_value, b: :b_value)
59
- instance.a.should == :a_value
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
- it "makes nil methods for mapped keys which had no attributes passed in for them" do
64
- instance = klass.new(a: :a_value)
65
- instance.b.should == nil
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
- context "with two keys and a 1-arity block given" do
70
- before { klass.map :a, :b, ->(v) { v + 1 } }
80
+ context "and two lambdas" do
81
+ before { klass.map :a, :b, ->(v) { v.downcase }, ->(v) { v.upcase } }
71
82
 
72
- it "creates a method named for the second key with the result of passing the associated value to the block" do
73
- klass.new(:a => 1).b.should == 2
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 two keys and a >1-arity block given" do
78
- it "raises an error" do
79
- lambda { klass.map :a, :b, ->(k,v) { v + 1 } }.should raise_error(AttributeCartographer::InvalidArgumentError)
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
- date: 2011-05-09 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
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
- requirement: &2152785160 !ruby/object:Gem::Requirement
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
17
20
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '2.5'
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "2.5"
22
25
  type: :development
23
- prerelease: false
24
- version_requirements: *2152785160
25
- description: AttributeCartographer allows you to map an attributes hash into similarly
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
- files:
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
- require_paths:
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: '0'
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: '0'
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
63
70
  requirements: []
71
+
64
72
  rubyforge_project:
65
- rubygems_version: 1.7.2
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
- to suit.
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