minimapper-extras 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -0
- data/lib/minimapper/entity/has_many.rb +26 -0
- data/lib/minimapper/extras/version.rb +1 -1
- data/lib/minimapper/mapper/has_many_association.rb +56 -0
- data/minimapper-extras.gemspec +1 -1
- data/spec/minimapper/entity/has_many_spec.rb +26 -0
- data/spec/minimapper/mapper/has_many_association_spec.rb +50 -0
- metadata +10 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ed4608bcd50ac8efa3a679ce16b038d9afa6982
|
4
|
+
data.tar.gz: f51fe75fc87a6ab8227e3a13a7440e9ca2c7bf3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8af1efffe8ef6e20aafcae764e8494d88ced0d47f75e29b1864417f3c8f9489824672ed8575ec7f72dbcd81cc888b049338894b1292ab0eb1c3dc1e427ea0352
|
7
|
+
data.tar.gz: f3f3d09427adcbc1d83479253915ad64f0c3a86e6900404f8ff11d66d47dbfafc8490cd6a067c7c49b2676fa6a7db286684869c8927887c48400cd97bd960996
|
data/README.md
CHANGED
@@ -23,6 +23,26 @@ Or install it yourself as:
|
|
23
23
|
|
24
24
|
For now, see the specs. TODO: Write docs.
|
25
25
|
|
26
|
+
## Convertions
|
27
|
+
|
28
|
+
Additional attribute conversions. See [lib/minimapper/entity/conversions.rb](https://github.com/barsoom/minimapper-extras/blob/master/lib/minimapper/entity/conversions.rb) for a full list.
|
29
|
+
|
30
|
+
require "minimapper/entity/conversions"
|
31
|
+
|
32
|
+
class User
|
33
|
+
include Minimapper::Entity
|
34
|
+
|
35
|
+
attribute :registered_on, :date
|
36
|
+
attribute :other
|
37
|
+
end
|
38
|
+
|
39
|
+
user = User.new
|
40
|
+
user.registered_on = "2001-01-01"
|
41
|
+
user.registered_on # => Mon, 01 Jan 2001
|
42
|
+
|
43
|
+
user.other = "2001-01-01"
|
44
|
+
user.other # => "2001-01-01"
|
45
|
+
|
26
46
|
## Custom FactoryGirl strategy
|
27
47
|
|
28
48
|
Add this to your spec_helper after loading FactoryGirl:
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "minimapper/entity"
|
2
|
+
|
3
|
+
module Minimapper
|
4
|
+
module Entity
|
5
|
+
module HasMany
|
6
|
+
def has_many(name)
|
7
|
+
attr_writer name
|
8
|
+
|
9
|
+
define_method(name) do
|
10
|
+
# @foo ||= []
|
11
|
+
ivar = instance_variable_get("@#{name}")
|
12
|
+
|
13
|
+
if ivar
|
14
|
+
ivar
|
15
|
+
else
|
16
|
+
instance_variable_set("@#{name}", [])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Minimapper::Entity::Attributes
|
25
|
+
include Minimapper::Entity::HasMany
|
26
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "attr_extras"
|
2
|
+
require "backports/1.9.1/kernel/public_send"
|
3
|
+
|
4
|
+
# Mapping of associated entity changes. Creates when not persisted, updates
|
5
|
+
# when persisted and removes when it has been removed from the in-memory model.
|
6
|
+
#
|
7
|
+
# It assumes there isn't many associated entities and that find will load associated entities.
|
8
|
+
module Minimapper
|
9
|
+
module Mapper
|
10
|
+
class HasManyAssociation
|
11
|
+
pattr_initialize :mapper, :parent_entity, :association_name, :belongs_to_association_name
|
12
|
+
|
13
|
+
def persist_changes
|
14
|
+
remove_deleted
|
15
|
+
create_or_update
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def remove_deleted
|
21
|
+
previous_entities.each do |entity|
|
22
|
+
unless current_entities.include?(entity)
|
23
|
+
association.delete(entity)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_or_update
|
29
|
+
current_entities.each do |entity|
|
30
|
+
if entity.persisted?
|
31
|
+
association.update(entity)
|
32
|
+
else
|
33
|
+
entity.public_send("#{belongs_to_association_name}=", parent_entity)
|
34
|
+
association.create(entity)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def current_entities
|
40
|
+
parent_entity.public_send(association_name)
|
41
|
+
end
|
42
|
+
|
43
|
+
def previous_entities
|
44
|
+
mapper.find(parent_entity.id).public_send(association_name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def association
|
48
|
+
repository.public_send(association_name)
|
49
|
+
end
|
50
|
+
|
51
|
+
def repository
|
52
|
+
mapper.repository
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/minimapper-extras.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_dependency "minimapper"
|
20
|
+
spec.add_dependency "minimapper", ">= 0.5.2"
|
21
21
|
spec.add_dependency "attr_extras"
|
22
22
|
spec.add_dependency "backports"
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "minimapper/entity/has_many"
|
3
|
+
|
4
|
+
class Customer
|
5
|
+
include Minimapper::Entity
|
6
|
+
|
7
|
+
has_many :cars
|
8
|
+
end
|
9
|
+
|
10
|
+
describe Minimapper::Entity::HasMany do
|
11
|
+
it "defaults to an empty array" do
|
12
|
+
Customer.new.cars.should be_empty
|
13
|
+
end
|
14
|
+
|
15
|
+
it "can be added to" do
|
16
|
+
customer = Customer.new
|
17
|
+
customer.cars << :foo
|
18
|
+
customer.cars.should == [ :foo ]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "can be set" do
|
22
|
+
customer = Customer.new
|
23
|
+
customer.cars = [ :foo ]
|
24
|
+
customer.cars.should == [ :foo ]
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "minimapper/mapper/has_many_association"
|
3
|
+
require "minimapper/entity"
|
4
|
+
require "minimapper/entity/belongs_to"
|
5
|
+
require "minimapper/entity/has_many"
|
6
|
+
|
7
|
+
class Contact
|
8
|
+
include Minimapper::Entity
|
9
|
+
|
10
|
+
attribute :name
|
11
|
+
|
12
|
+
belongs_to :customer
|
13
|
+
end
|
14
|
+
|
15
|
+
class Customer
|
16
|
+
include Minimapper::Entity
|
17
|
+
|
18
|
+
has_many :contacts
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Minimapper::Mapper::HasManyAssociation do
|
22
|
+
let(:contact) { Contact.new(:name => "Joe", :id => 1) }
|
23
|
+
let(:customer) { Customer.new(:id => 1, :contacts => [ contact ]) }
|
24
|
+
let(:repository) { mock(:contacts => contacts_mapper) }
|
25
|
+
let(:customer_mapper) { mock(:find => customer, :repository => repository) }
|
26
|
+
let(:contacts_mapper) { mock }
|
27
|
+
|
28
|
+
it "updates changed associated entities" do
|
29
|
+
contacts_mapper.should_receive(:update).with(contact)
|
30
|
+
has_many_association = described_class.new(customer_mapper, customer, "contacts", "customer")
|
31
|
+
has_many_association.persist_changes
|
32
|
+
end
|
33
|
+
|
34
|
+
it "creates new associated entities and assigns the belongs_to association" do
|
35
|
+
contact.id = nil
|
36
|
+
|
37
|
+
contacts_mapper.should_receive(:create).with(contact)
|
38
|
+
has_many_association = described_class.new(customer_mapper, customer, "contacts", "customer")
|
39
|
+
has_many_association.persist_changes
|
40
|
+
contact.customer.should == customer
|
41
|
+
end
|
42
|
+
|
43
|
+
it "removes associated entities that no longer exist" do
|
44
|
+
customer_mapper.stub(:find => Customer.new(:id => 1, :contacts => [ contact ]))
|
45
|
+
customer.contacts = []
|
46
|
+
contacts_mapper.should_receive(:delete).with(contact)
|
47
|
+
has_many_association = described_class.new(customer_mapper, customer, "contacts", "customer")
|
48
|
+
has_many_association.persist_changes
|
49
|
+
end
|
50
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimapper-extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joakim Kolsjö
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minimapper
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - '>='
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 0.5.2
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - '>='
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: 0.5.2
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: attr_extras
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -126,19 +126,23 @@ files:
|
|
126
126
|
- Rakefile
|
127
127
|
- lib/minimapper/entity/belongs_to.rb
|
128
128
|
- lib/minimapper/entity/conversions.rb
|
129
|
+
- lib/minimapper/entity/has_many.rb
|
129
130
|
- lib/minimapper/entity/serialized_association.rb
|
130
131
|
- lib/minimapper/entity/serialized_collection.rb
|
131
132
|
- lib/minimapper/extras.rb
|
132
133
|
- lib/minimapper/extras/version.rb
|
133
134
|
- lib/minimapper/factory_girl.rb
|
135
|
+
- lib/minimapper/mapper/has_many_association.rb
|
134
136
|
- minimapper-extras.gemspec
|
135
137
|
- script/ci
|
136
138
|
- script/turbux_rspec
|
137
139
|
- spec/minimapper/entity/belongs_to_spec.rb
|
138
140
|
- spec/minimapper/entity/conversions_spec.rb
|
141
|
+
- spec/minimapper/entity/has_many_spec.rb
|
139
142
|
- spec/minimapper/entity/serialized_association_spec.rb
|
140
143
|
- spec/minimapper/entity/serialized_collection_spec.rb
|
141
144
|
- spec/minimapper/factory_girl_spec.rb
|
145
|
+
- spec/minimapper/mapper/has_many_association_spec.rb
|
142
146
|
- spec/spec_helper.rb
|
143
147
|
homepage: ''
|
144
148
|
licenses:
|
@@ -167,7 +171,9 @@ summary: Extras for Minimapper.
|
|
167
171
|
test_files:
|
168
172
|
- spec/minimapper/entity/belongs_to_spec.rb
|
169
173
|
- spec/minimapper/entity/conversions_spec.rb
|
174
|
+
- spec/minimapper/entity/has_many_spec.rb
|
170
175
|
- spec/minimapper/entity/serialized_association_spec.rb
|
171
176
|
- spec/minimapper/entity/serialized_collection_spec.rb
|
172
177
|
- spec/minimapper/factory_girl_spec.rb
|
178
|
+
- spec/minimapper/mapper/has_many_association_spec.rb
|
173
179
|
- spec/spec_helper.rb
|