minimapper 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -61,7 +61,8 @@ require "minimapper"
61
61
  require "minimapper/entity"
62
62
  require "minimapper/memory"
63
63
 
64
- class User < Minimapper::Entity
64
+ class User
65
+ include Minimapper::Entity
65
66
  attributes :name, :email
66
67
  validates :name, :presence => true
67
68
  end
@@ -178,7 +179,8 @@ It gets simpler to maintain if you use shared tests to test both implementations
178
179
  # Supported types: Integer, DateTime
179
180
  # TODO: Will probably not support more types, but instead provide a way to add custom conversions.
180
181
 
181
- class User < Minimapper::Entity
182
+ class User
183
+ include Minimapper::Entity
182
184
  attributes [ :profile_id, Integer ]
183
185
  end
184
186
 
@@ -243,7 +245,6 @@ You need mysql and postgres installed (but they do not have to be running) to be
243
245
  * Extract entity and model class lookup code from the ar-mapper and reuse it in the memory mapper.
244
246
  * Change the memory mapper to store entity attributes, not entity instances.
245
247
  - Unless this makes it difficult to handle associated data.
246
- * Make Minimapper::Entity a module so you won't have to inherit from it.
247
248
  * Make using Minimapper::Entity optional by providing shared examples of the behavior required by the mappers. Test the mappers with an object implementing only this behavior.
248
249
 
249
250
  ### Ideas
@@ -1,5 +1,5 @@
1
1
  module Minimapper
2
- class Entity
2
+ module Entity
3
3
  class Convert
4
4
  def initialize(value)
5
5
  @value = value
@@ -1,52 +1,55 @@
1
1
  # Minimapper does not require you to use this entity base class. It requires a
2
2
  # few methods to be present, like valid?, attributes, attributes=.
3
3
  #
4
- # I plan to convert this to a module and add shared examples that cover
5
- # the API which minimapper depends upon.
4
+ # I plan to add shared examples that cover the API which minimapper depends upon.
6
5
  #
7
6
  # This class also does some things needed for it to work well with rails.
8
7
  require 'informal'
9
8
  require 'minimapper/entity/convert'
10
9
 
11
10
  module Minimapper
12
- class Entity
13
- include Informal::Model
14
-
15
- def self.attributes(*list)
16
- list.each do |attribute|
17
- type = nil
18
-
19
- if attribute.is_a?(Array)
20
- attribute, type = attribute
21
- end
22
-
23
- define_method(attribute) do
24
- instance_variable_get("@#{attribute}")
25
- end
26
-
27
- define_method("#{attribute}=") do |value|
28
- value = Convert.new(value).to(type)
29
- instance_variable_set("@#{attribute}", value)
30
- @attributes[attribute] = value
11
+ module Entity
12
+ module ClassMethods
13
+ def attributes(*list)
14
+ list.each do |attribute|
15
+ type = nil
16
+
17
+ if attribute.is_a?(Array)
18
+ attribute, type = attribute
19
+ end
20
+
21
+ define_method(attribute) do
22
+ instance_variable_get("@#{attribute}")
23
+ end
24
+
25
+ define_method("#{attribute}=") do |value|
26
+ value = Convert.new(value).to(type)
27
+ instance_variable_set("@#{attribute}", value)
28
+ attributes[attribute] = value
29
+ end
31
30
  end
32
31
  end
33
32
  end
34
33
 
35
- def initialize(*opts)
36
- @attributes = {}
37
- super(*opts)
38
- end
34
+ module InstanceMethods
35
+ def to_param
36
+ id
37
+ end
39
38
 
40
- def to_param
41
- id
42
- end
39
+ def persisted?
40
+ id
41
+ end
43
42
 
44
- def persisted?
45
- id
43
+ def attributes
44
+ @attributes ||= {}
45
+ end
46
46
  end
47
47
 
48
- attributes [ :id, Integer ], [ :created_at, DateTime ], [ :updated_at, DateTime ]
49
-
50
- attr_reader :attributes
48
+ def self.included(klass)
49
+ klass.send(:include, Informal::Model)
50
+ klass.send(:include, InstanceMethods)
51
+ klass.send(:extend, ClassMethods)
52
+ klass.attributes [ :id, :Integer ], [ :created_at, :DateTime ], [ :updated_at, :DateTime ]
53
+ end
51
54
  end
52
55
  end
@@ -1,3 +1,3 @@
1
1
  module Minimapper
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
data/spec/ar_spec.rb CHANGED
@@ -2,7 +2,8 @@ require "spec_helper"
2
2
  require "minimapper/entity"
3
3
  require "minimapper/ar"
4
4
 
5
- class TestEntity < Minimapper::Entity
5
+ class TestEntity
6
+ include Minimapper::Entity
6
7
  attributes :name, :github_url
7
8
  validates :name, :presence => true
8
9
  end
data/unit/entity_spec.rb CHANGED
@@ -1,27 +1,37 @@
1
1
  require 'minimapper/entity'
2
2
 
3
+ class TestEntity
4
+ include Minimapper::Entity
5
+ end
6
+
3
7
  describe Minimapper::Entity do
4
8
  it "handles base attributes" do
5
- base = described_class.new
6
- base.id = 5
7
- base.id.should == 5
9
+ entity = TestEntity.new
10
+ entity.id = 5
11
+ entity.id.should == 5
8
12
 
9
13
  time = Time.now
10
- base.created_at = time
11
- base.created_at.should == time
14
+ entity.created_at = time
15
+ entity.created_at.should == time
16
+
17
+ entity.updated_at = time
18
+ entity.updated_at.should == time
19
+ end
12
20
 
13
- base.updated_at = time
14
- base.updated_at.should == time
21
+ it "can access attributes set at construction time" do
22
+ entity = TestEntity.new(:id => 5)
23
+ entity.id.should == 5
15
24
  end
16
25
 
17
26
  it "converts typed attributes" do
18
- base = described_class.new
19
- base.id = "10"
20
- base.id.should == 10
27
+ entity = TestEntity.new
28
+ entity.id = "10"
29
+ entity.id.should == 10
21
30
  end
22
31
  end
23
32
 
24
- class TestUser < Minimapper::Entity
33
+ class TestUser
34
+ include Minimapper::Entity
25
35
  attributes :name
26
36
  end
27
37
 
@@ -37,25 +47,25 @@ end
37
47
 
38
48
  describe Minimapper::Entity, "attributes" do
39
49
  it "returns the attributes" do
40
- base = described_class.new(:id => 5)
50
+ entity = TestEntity.new(:id => 5)
41
51
  time = Time.now
42
- base.created_at = time
43
- base.attributes.should == { :id => 5, :created_at => time }
52
+ entity.created_at = time
53
+ entity.attributes.should == { :id => 5, :created_at => time }
44
54
  end
45
55
  end
46
56
 
47
57
  describe Minimapper::Entity, "to_param" do
48
58
  it "responds with the id to be compatible with rails link helpers" do
49
- base = described_class.new(:id => 5)
50
- base.to_param.should == 5
59
+ entity = TestEntity.new(:id => 5)
60
+ entity.to_param.should == 5
51
61
  end
52
62
  end
53
63
 
54
64
  describe Minimapper::Entity, "persisted?" do
55
65
  it "responds true when there is an id (to be compatible with rails form helpers)" do
56
- base = described_class.new
57
- base.should_not be_persisted
58
- base.id = 5
59
- base.should be_persisted
66
+ entity = TestEntity.new
67
+ entity.should_not be_persisted
68
+ entity.id = 5
69
+ entity.should be_persisted
60
70
  end
61
71
  end
data/unit/memory_spec.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require 'minimapper/memory'
2
2
  require 'minimapper/entity'
3
3
 
4
- class TestEntity < Minimapper::Entity
4
+ class TestEntity
5
+ include Minimapper::Entity
5
6
  attributes :name
6
7
  validates :name, :presence => true
7
8
  end
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.0.5
4
+ version: 0.1.0
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: 2012-11-02 00:00:00.000000000 Z
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: informal
16
- requirement: &70203560076140 !ruby/object:Gem::Requirement
16
+ requirement: &70274943136540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70203560076140
24
+ version_requirements: *70274943136540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70203560075340 !ruby/object:Gem::Requirement
27
+ requirement: &70274943135820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70203560075340
35
+ version_requirements: *70274943135820
36
36
  description: A minimalistic way of separating your models from ORMs like ActiveRecord
37
37
  (by implementing the repository pattern)
38
38
  email: