minimapper 0.1.1 → 0.1.2
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.md +5 -1
- data/lib/minimapper/entity/attributes.rb +2 -4
- data/lib/minimapper/entity/core.rb +7 -1
- data/lib/minimapper/entity/rails.rb +21 -0
- data/lib/minimapper/entity.rb +9 -2
- data/lib/minimapper/version.rb +1 -1
- data/minimapper.gemspec +1 -2
- data/unit/entity/core_spec.rb +8 -0
- data/unit/entity/rails_spec.rb +53 -0
- data/unit/entity_spec.rb +16 -23
- metadata +5 -15
data/README.md
CHANGED
@@ -63,6 +63,7 @@ require "minimapper/memory"
|
|
63
63
|
|
64
64
|
class User
|
65
65
|
include Minimapper::Entity
|
66
|
+
|
66
67
|
attributes :name, :email
|
67
68
|
validates :name, :presence => true
|
68
69
|
end
|
@@ -190,6 +191,10 @@ User.new(:profile_id => " ").profile_id # => nil
|
|
190
191
|
User.new(:profile_id => "foobar").profile_id # => nil
|
191
192
|
```
|
192
193
|
|
194
|
+
### Custom entity class
|
195
|
+
|
196
|
+
You don't need to use Minimapper::Entity. The core API is implemented in [Minimapper::Entity::Core](https://github.com/joakimk/minimapper/blob/master/lib/minimapper/entity/core.rb). Either include that module or implement your own version.
|
197
|
+
|
193
198
|
### Adding a new mapper
|
194
199
|
|
195
200
|
If you where to add a [Mongoid](http://mongoid.org/en/mongoid/index.html) mapper:
|
@@ -241,7 +246,6 @@ You need mysql and postgres installed (but they do not have to be running) to be
|
|
241
246
|
|
242
247
|
### Next
|
243
248
|
|
244
|
-
* Make "informal" optional. If you only use minimapper/entity/core there is no need for it as a dependency.
|
245
249
|
* Add some way to extend type conversions to keep that part of minimapper small.
|
246
250
|
* Extract entity and model class lookup code from the ar-mapper and reuse it in the memory mapper.
|
247
251
|
* Change the memory mapper to store entity attributes, not entity instances.
|
@@ -12,13 +12,11 @@ module Minimapper
|
|
12
12
|
end
|
13
13
|
|
14
14
|
define_method(attribute) do
|
15
|
-
|
15
|
+
attributes[attribute]
|
16
16
|
end
|
17
17
|
|
18
18
|
define_method("#{attribute}=") do |value|
|
19
|
-
|
20
|
-
instance_variable_set("@#{attribute}", value)
|
21
|
-
attributes[attribute] = value
|
19
|
+
attributes[attribute] = Convert.new(value).to(type)
|
22
20
|
end
|
23
21
|
end
|
24
22
|
end
|
@@ -11,12 +11,18 @@ module Minimapper
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def attributes=(new_attributes)
|
14
|
-
@attributes = attributes.merge(new_attributes)
|
14
|
+
@attributes = attributes.merge(symbolize_keys(new_attributes))
|
15
15
|
end
|
16
16
|
|
17
17
|
def valid?
|
18
18
|
true
|
19
19
|
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def symbolize_keys(hash)
|
24
|
+
hash.inject({}) { |h, (k, v)| h.merge!(k.to_sym => v) }
|
25
|
+
end
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
@@ -1,6 +1,15 @@
|
|
1
|
+
require "active_model"
|
2
|
+
|
1
3
|
module Minimapper
|
2
4
|
module Entity
|
3
5
|
module Rails
|
6
|
+
def self.included(klass)
|
7
|
+
klass.class_eval do
|
8
|
+
extend ActiveModel::Naming
|
9
|
+
include ActiveModel::Validations
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
4
13
|
def to_param
|
5
14
|
id
|
6
15
|
end
|
@@ -8,6 +17,18 @@ module Minimapper
|
|
8
17
|
def persisted?
|
9
18
|
id
|
10
19
|
end
|
20
|
+
|
21
|
+
def new_record?
|
22
|
+
!id
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_model
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_key
|
30
|
+
persisted? ? [ id ] : nil
|
31
|
+
end
|
11
32
|
end
|
12
33
|
end
|
13
34
|
end
|
data/lib/minimapper/entity.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
# Look at minimapper/entity/core for the required API.
|
2
|
-
require 'informal'
|
3
2
|
require 'minimapper/entity/core'
|
4
3
|
require 'minimapper/entity/attributes'
|
5
4
|
require 'minimapper/entity/rails'
|
@@ -8,8 +7,16 @@ module Minimapper
|
|
8
7
|
module Entity
|
9
8
|
include Minimapper::Entity::Core
|
10
9
|
|
10
|
+
def initialize(attributes = {})
|
11
|
+
self.attributes = attributes
|
12
|
+
end
|
13
|
+
|
14
|
+
def attributes=(new_attributes)
|
15
|
+
super(new_attributes)
|
16
|
+
new_attributes.each_pair { |name, value| self.send("#{name}=", value) }
|
17
|
+
end
|
18
|
+
|
11
19
|
def self.included(klass)
|
12
|
-
klass.send(:include, Informal::Model)
|
13
20
|
klass.send(:include, Minimapper::Entity::Rails)
|
14
21
|
klass.send(:extend, Minimapper::Entity::Attributes)
|
15
22
|
klass.attributes([ :id, :Integer ], [ :created_at, :DateTime ], [ :updated_at, :DateTime ])
|
data/lib/minimapper/version.rb
CHANGED
data/minimapper.gemspec
CHANGED
data/unit/entity/core_spec.rb
CHANGED
@@ -2,6 +2,7 @@ require 'minimapper/entity/core'
|
|
2
2
|
|
3
3
|
class BasicEntity
|
4
4
|
include Minimapper::Entity::Core
|
5
|
+
attr_accessor :one, :two
|
5
6
|
end
|
6
7
|
|
7
8
|
describe Minimapper::Entity::Core do
|
@@ -19,6 +20,13 @@ describe Minimapper::Entity::Core do
|
|
19
20
|
entity.attributes.should == { :one => 1, :two => 2 }
|
20
21
|
end
|
21
22
|
|
23
|
+
it "converts all keys to symbols" do
|
24
|
+
entity = BasicEntity.new
|
25
|
+
entity.attributes = { :one => 1 }
|
26
|
+
entity.attributes = { "one" => 11 }
|
27
|
+
entity.attributes.should == { :one => 11 }
|
28
|
+
end
|
29
|
+
|
22
30
|
it "returns true for valid?" do
|
23
31
|
entity = BasicEntity.new
|
24
32
|
entity.should be_valid
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'minimapper/entity/rails'
|
2
|
+
require 'minimapper/entity/core'
|
3
|
+
|
4
|
+
class RailsEntity
|
5
|
+
include Minimapper::Entity::Core
|
6
|
+
include Minimapper::Entity::Rails
|
7
|
+
|
8
|
+
attr_accessor :name
|
9
|
+
validates :name, :presence => true
|
10
|
+
end
|
11
|
+
|
12
|
+
describe Minimapper::Entity::Rails do
|
13
|
+
it "responds to new_record?" do
|
14
|
+
entity = RailsEntity.new
|
15
|
+
entity.should be_new_record
|
16
|
+
entity.id = 5
|
17
|
+
entity.should_not be_new_record
|
18
|
+
end
|
19
|
+
|
20
|
+
it "resonds to to_model" do
|
21
|
+
entity = RailsEntity.new
|
22
|
+
entity.to_model.should == entity
|
23
|
+
end
|
24
|
+
|
25
|
+
it "responds to to_key" do
|
26
|
+
entity = RailsEntity.new
|
27
|
+
entity.to_key.should be_nil
|
28
|
+
entity.id = 5
|
29
|
+
entity.to_key.should == [ 5 ]
|
30
|
+
end
|
31
|
+
|
32
|
+
# for rails link helpers
|
33
|
+
it "responds to to_param" do
|
34
|
+
entity = RailsEntity.new
|
35
|
+
entity.id = 5
|
36
|
+
entity.to_param.should == 5
|
37
|
+
end
|
38
|
+
|
39
|
+
# for rails form helpers
|
40
|
+
it "responds to persisted?" do
|
41
|
+
entity = RailsEntity.new
|
42
|
+
entity.should_not be_persisted
|
43
|
+
entity.id = 5
|
44
|
+
entity.should be_persisted
|
45
|
+
end
|
46
|
+
|
47
|
+
it "includes active model validations" do
|
48
|
+
entity = RailsEntity.new
|
49
|
+
entity.should_not be_valid
|
50
|
+
entity.name = "Joe"
|
51
|
+
entity.should be_valid
|
52
|
+
end
|
53
|
+
end
|
data/unit/entity_spec.rb
CHANGED
@@ -7,7 +7,6 @@ end
|
|
7
7
|
class TestUser
|
8
8
|
include Minimapper::Entity
|
9
9
|
attributes :name
|
10
|
-
validates :name, :presence => true
|
11
10
|
end
|
12
11
|
|
13
12
|
describe Minimapper::Entity do
|
@@ -25,21 +24,31 @@ describe Minimapper::Entity do
|
|
25
24
|
end
|
26
25
|
|
27
26
|
it "can access attributes set at construction time" do
|
28
|
-
entity =
|
27
|
+
entity = TestUser.new(:id => 5)
|
28
|
+
entity.id.should == 5
|
29
|
+
entity.attributes[:id].should == 5
|
30
|
+
end
|
31
|
+
|
32
|
+
it "can access attributes set though a hash" do
|
33
|
+
entity = TestUser.new
|
34
|
+
entity.attributes = { :id => 5 }
|
29
35
|
entity.id.should == 5
|
36
|
+
entity.attributes = { "id" => 8 }
|
37
|
+
entity.id.should == 8
|
30
38
|
end
|
31
39
|
|
32
40
|
it "converts typed attributes" do
|
33
41
|
entity = TestEntity.new
|
34
42
|
entity.id = "10"
|
35
43
|
entity.id.should == 10
|
44
|
+
entity.attributes = { :id => "15" }
|
45
|
+
entity.id.should == 15
|
36
46
|
end
|
37
47
|
|
38
|
-
it "
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
user.should be_valid
|
48
|
+
it "symbolizes keys" do
|
49
|
+
entity = TestEntity.new
|
50
|
+
entity.attributes = { "id" => "15" }
|
51
|
+
entity.attributes[:id].should == 15
|
43
52
|
end
|
44
53
|
end
|
45
54
|
|
@@ -61,19 +70,3 @@ describe Minimapper::Entity, "attributes" do
|
|
61
70
|
entity.attributes.should == { :id => 5, :created_at => time }
|
62
71
|
end
|
63
72
|
end
|
64
|
-
|
65
|
-
describe Minimapper::Entity, "to_param" do
|
66
|
-
it "responds with the id to be compatible with rails link helpers" do
|
67
|
-
entity = TestEntity.new(:id => 5)
|
68
|
-
entity.to_param.should == 5
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
describe Minimapper::Entity, "persisted?" do
|
73
|
-
it "responds true when there is an id (to be compatible with rails form helpers)" do
|
74
|
-
entity = TestEntity.new
|
75
|
-
entity.should_not be_persisted
|
76
|
-
entity.id = 5
|
77
|
-
entity.should be_persisted
|
78
|
-
end
|
79
|
-
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.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,28 +11,17 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-11-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: informal
|
16
|
-
requirement: &70315666068220 !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: *70315666068220
|
25
14
|
- !ruby/object:Gem::Dependency
|
26
15
|
name: rake
|
27
|
-
requirement: &
|
16
|
+
requirement: &70233951931080 !ruby/object:Gem::Requirement
|
28
17
|
none: false
|
29
18
|
requirements:
|
30
19
|
- - ! '>='
|
31
20
|
- !ruby/object:Gem::Version
|
32
21
|
version: '0'
|
33
|
-
type: :
|
22
|
+
type: :development
|
34
23
|
prerelease: false
|
35
|
-
version_requirements: *
|
24
|
+
version_requirements: *70233951931080
|
36
25
|
description: A minimalistic way of separating your models from ORMs like ActiveRecord
|
37
26
|
(by implementing the repository pattern)
|
38
27
|
email:
|
@@ -68,6 +57,7 @@ files:
|
|
68
57
|
- spec/support/shared_examples/mapper.rb
|
69
58
|
- unit/entity/convert_spec.rb
|
70
59
|
- unit/entity/core_spec.rb
|
60
|
+
- unit/entity/rails_spec.rb
|
71
61
|
- unit/entity_spec.rb
|
72
62
|
- unit/memory_spec.rb
|
73
63
|
- unit/repository_spec.rb
|