minimapper 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -246,6 +246,25 @@ User.new(:reminder_on => "2012-01-01").reminder # => #<Date: 2012-01-01 ...>
246
246
 
247
247
  Minimapper only calls #convert on non-empty strings. When the value is blank or nil, the attribute is set to nil.
248
248
 
249
+ ### Overriding attribute accessors
250
+
251
+ Attribute readers and writers are implemented so that you can override them with inheritance:
252
+
253
+ ``` ruby
254
+ class User
255
+ include Minimapper::Entity
256
+ attribute :name
257
+
258
+ def name
259
+ super.upcase
260
+ end
261
+
262
+ def name=(value)
263
+ super(value.strip)
264
+ end
265
+ end
266
+ ```
267
+
249
268
  ### Associations
250
269
 
251
270
  There is no core support for associations, but we're implementing them in [minimapper-extras](https://github.com/barsoom/minimapper-extras) as we need them.
@@ -5,10 +5,17 @@ module Minimapper
5
5
  module Attributes
6
6
  def attributes(*list)
7
7
  columns = add_columns(list)
8
+
9
+ # By adding instance methods via an included module,
10
+ # they become overridable with "super".
11
+ instance_method_container = Module.new
12
+
8
13
  columns.each do |column|
9
- define_reader(column)
10
- define_writer(column)
14
+ define_reader(column, instance_method_container)
15
+ define_writer(column, instance_method_container)
11
16
  end
17
+
18
+ include instance_method_container
12
19
  end
13
20
 
14
21
  def attribute(*opts)
@@ -27,15 +34,19 @@ module Minimapper
27
34
  @entity_columns |= list.map { |data| Column.new(data) }
28
35
  end
29
36
 
30
- def define_reader(column)
31
- define_method(column.name) do
32
- attributes[column.name]
37
+ def define_reader(column, instance_method_container)
38
+ instance_method_container.module_eval do
39
+ define_method(column.name) do
40
+ attributes[column.name]
41
+ end
33
42
  end
34
43
  end
35
44
 
36
- def define_writer(column)
37
- define_method("#{column.name}=") do |value|
38
- attributes[column.name] = Convert.new(value).to(column.type)
45
+ def define_writer(column, instance_method_container)
46
+ instance_method_container.module_eval do
47
+ define_method("#{column.name}=") do |value|
48
+ attributes[column.name] = Convert.new(value).to(column.type)
49
+ end
39
50
  end
40
51
  end
41
52
 
@@ -27,7 +27,7 @@ module Minimapper
27
27
 
28
28
  def converter_for(type)
29
29
  if type && !@@converters[type]
30
- raise "Unknown attribute type: #{type}. Perhaps you've spelled it wrong or not registered the converter for it."
30
+ raise "Unknown attribute type: #{type}. Perhaps you've spelled it wrong or haven't registered the converter for it."
31
31
  end
32
32
 
33
33
  @@converters.fetch(type, NoOpConverter.new)
@@ -1,3 +1,3 @@
1
1
  module Minimapper
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
@@ -29,4 +29,8 @@ describe Minimapper::Entity::Convert do
29
29
  it "raises when the type isn't known" do
30
30
  lambda { described_class.new('foobar').to(:unknown) }.should raise_error(/Unknown attribute type/)
31
31
  end
32
+
33
+ it "does not make false nil" do
34
+ described_class.new(false).to(:unknown).should eq(false)
35
+ end
32
36
  end
data/unit/entity_spec.rb CHANGED
@@ -20,9 +20,23 @@ class TestTask
20
20
  attribute :due_at, :date_time
21
21
  end
22
22
 
23
+ class OverridingTestUser
24
+ include Minimapper::Entity
25
+ attributes :name
26
+
27
+ def name
28
+ super.upcase
29
+ end
30
+
31
+ def name=(value)
32
+ super(value.strip)
33
+ end
34
+ end
35
+
23
36
  describe Minimapper::Entity do
24
37
  it "handles base attributes" do
25
38
  entity = TestEntity.new
39
+
26
40
  entity.id = 5
27
41
  entity.id.should == 5
28
42
 
@@ -40,7 +54,7 @@ describe Minimapper::Entity do
40
54
  entity.attributes[:id].should == 5
41
55
  end
42
56
 
43
- it "can access attributes set though a hash" do
57
+ it "can access attributes set through a hash" do
44
58
  entity = TestUser.new
45
59
  entity.attributes = { :id => 5 }
46
60
  entity.id.should == 5
@@ -72,6 +86,18 @@ describe Minimapper::Entity do
72
86
  entity.attributes = { "id" => "15" }
73
87
  entity.attributes[:id].should == 15
74
88
  end
89
+
90
+ it "is possible to override attribute readers with inheritance" do
91
+ user = OverridingTestUser.new
92
+ user.name = "pelle"
93
+ user.name.should == "PELLE"
94
+ end
95
+
96
+ it "is possible to override attribute writers with inheritance" do
97
+ user = OverridingTestUser.new
98
+ user.name = " 123 "
99
+ user.name.should == "123"
100
+ end
75
101
  end
76
102
 
77
103
  describe Minimapper::Entity, "attributes without type" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
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: 2013-05-17 00:00:00.000000000 Z
12
+ date: 2013-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70151297215480 !ruby/object:Gem::Requirement
16
+ requirement: &70347747664260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70151297215480
24
+ version_requirements: *70347747664260
25
25
  description: A minimalistic way of separating your models from ORMs like ActiveRecord.
26
26
  email:
27
27
  - joakim.kolsjo@gmail.com
@@ -75,18 +75,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
- segments:
79
- - 0
80
- hash: -4467739529293173565
81
78
  required_rubygems_version: !ruby/object:Gem::Requirement
82
79
  none: false
83
80
  requirements:
84
81
  - - ! '>='
85
82
  - !ruby/object:Gem::Version
86
83
  version: '0'
87
- segments:
88
- - 0
89
- hash: -4467739529293173565
90
84
  requirements: []
91
85
  rubyforge_project:
92
86
  rubygems_version: 1.8.5