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 +4 -3
- data/lib/minimapper/entity/convert.rb +1 -1
- data/lib/minimapper/entity.rb +36 -33
- data/lib/minimapper/version.rb +1 -1
- data/spec/ar_spec.rb +2 -1
- data/unit/entity_spec.rb +30 -20
- data/unit/memory_spec.rb +2 -1
- metadata +6 -6
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
|
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
|
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
|
data/lib/minimapper/entity.rb
CHANGED
@@ -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
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
module InstanceMethods
|
35
|
+
def to_param
|
36
|
+
id
|
37
|
+
end
|
39
38
|
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
def persisted?
|
40
|
+
id
|
41
|
+
end
|
43
42
|
|
44
|
-
|
45
|
-
|
43
|
+
def attributes
|
44
|
+
@attributes ||= {}
|
45
|
+
end
|
46
46
|
end
|
47
47
|
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
data/lib/minimapper/version.rb
CHANGED
data/spec/ar_spec.rb
CHANGED
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
|
-
|
6
|
-
|
7
|
-
|
9
|
+
entity = TestEntity.new
|
10
|
+
entity.id = 5
|
11
|
+
entity.id.should == 5
|
8
12
|
|
9
13
|
time = Time.now
|
10
|
-
|
11
|
-
|
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
|
-
|
14
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
27
|
+
entity = TestEntity.new
|
28
|
+
entity.id = "10"
|
29
|
+
entity.id.should == 10
|
21
30
|
end
|
22
31
|
end
|
23
32
|
|
24
|
-
class TestUser
|
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
|
-
|
50
|
+
entity = TestEntity.new(:id => 5)
|
41
51
|
time = Time.now
|
42
|
-
|
43
|
-
|
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
|
-
|
50
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
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
|
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-
|
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: &
|
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: *
|
24
|
+
version_requirements: *70274943136540
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
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: *
|
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:
|