minimapper-extras 0.0.4 → 0.0.5
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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e25e9f439adce376313c1e6689e66aab2ee8591d
|
4
|
+
data.tar.gz: c927432be7676680810eccd2a0cbf5644cca82bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9efb298a73ed8bcd662356ca902600d8aad3f817a3ad1019ca3bb5a7815befa94eaf893132250ffd18d2ab68f20fd4cb0ec006d04e47b9cf58a699fc469707a3
|
7
|
+
data.tar.gz: 330bc3914da9714b3e2f7603d7fe6fd3be9aef610dcaf164f3fe253a9e8725666b2667874485143f57f89d8be36d5038241bb424be4d4f3d371c3310f34e67bb
|
@@ -1,18 +1,26 @@
|
|
1
1
|
require "minimapper/entity"
|
2
|
+
require "backports/1.9.1/kernel/public_send"
|
3
|
+
require "backports/1.8.7/kernel/instance_exec"
|
2
4
|
|
3
5
|
module Minimapper
|
4
6
|
module Entity
|
5
7
|
module SerializedAssociation
|
6
|
-
def serialized_association(name,
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
def serialized_association(name, entity_class_or_proc = nil)
|
9
|
+
define_method(name) do # def foo
|
10
|
+
attributes = public_send("#{name}_attributes") || {} # attributes = foo_attributes
|
11
|
+
entity = instance_variable_get("@#{name}") # entity = @foo
|
12
|
+
|
13
|
+
if entity_class_or_proc.is_a?(Proc)
|
14
|
+
entity ||= instance_exec(attributes, &entity_class_or_proc) # call proc in instance context
|
15
|
+
else
|
16
|
+
entity_class = entity_class_or_proc ||
|
17
|
+
name.to_s.camelcase.constantize
|
12
18
|
|
13
|
-
|
14
|
-
|
15
|
-
|
19
|
+
entity ||= entity_class.new(attributes) # entity ||= Foo.new(attributes)
|
20
|
+
end
|
21
|
+
|
22
|
+
instance_variable_set("@#{name}", entity) # @foo = entity
|
23
|
+
end
|
16
24
|
end
|
17
25
|
end
|
18
26
|
|
@@ -20,7 +20,9 @@ class CreateThroughRepositoryStrategy
|
|
20
20
|
# "customer { FactoryGirl.build(:customer) }" in factories.
|
21
21
|
create_dependencies
|
22
22
|
|
23
|
-
if
|
23
|
+
if entity.persisted?
|
24
|
+
entity
|
25
|
+
elsif mapper_with_name(mapper_name).create(entity)
|
24
26
|
entity
|
25
27
|
else
|
26
28
|
errors = entity.errors.full_messages.join(", ")
|
@@ -35,8 +37,12 @@ class CreateThroughRepositoryStrategy
|
|
35
37
|
|
36
38
|
def create_dependencies
|
37
39
|
associated_entity_names.each do |name|
|
38
|
-
|
39
|
-
|
40
|
+
associated_entity = entity.public_send(name)
|
41
|
+
|
42
|
+
if associated_entity
|
43
|
+
dependency_entity = self.class.new(associated_entity).create
|
44
|
+
entity.public_send("#{name}=", dependency_entity)
|
45
|
+
end
|
40
46
|
end
|
41
47
|
end
|
42
48
|
|
@@ -7,14 +7,32 @@ class Address
|
|
7
7
|
attribute :street
|
8
8
|
end
|
9
9
|
|
10
|
+
class CustomerAddress
|
11
|
+
include Minimapper::Entity
|
12
|
+
|
13
|
+
def initialize(customer, attributes)
|
14
|
+
@customer = customer
|
15
|
+
super(attributes)
|
16
|
+
end
|
17
|
+
|
18
|
+
def customer_name
|
19
|
+
@customer.name
|
20
|
+
end
|
21
|
+
|
22
|
+
attribute :street
|
23
|
+
end
|
24
|
+
|
10
25
|
class Customer
|
11
26
|
include Minimapper::Entity
|
12
27
|
|
28
|
+
attribute :name
|
13
29
|
attribute :address_attributes
|
14
30
|
attribute :visit_address_attributes
|
31
|
+
attribute :custom_address_attributes
|
15
32
|
|
16
33
|
serialized_association :address
|
17
34
|
serialized_association :visit_address, Address
|
35
|
+
serialized_association :custom_address, lambda { |attributes| CustomerAddress.new(self, attributes) }
|
18
36
|
end
|
19
37
|
|
20
38
|
describe Minimapper::Entity::SerializedAssociation do
|
@@ -38,4 +56,10 @@ describe Minimapper::Entity::SerializedAssociation do
|
|
38
56
|
customer = Customer.new(:visit_address_attributes => { :street => "Street 66" })
|
39
57
|
customer.visit_address.street.should == "Street 66"
|
40
58
|
end
|
59
|
+
|
60
|
+
it "can use a lambda to construct addresses" do
|
61
|
+
customer = Customer.new(:name => "Joe", :custom_address_attributes => { :street => "Street 66" })
|
62
|
+
customer.custom_address.street.should == "Street 66"
|
63
|
+
customer.custom_address.customer_name.should == "Joe"
|
64
|
+
end
|
41
65
|
end
|
@@ -68,4 +68,20 @@ describe CreateThroughRepositoryStrategy, "when there are belongs_to association
|
|
68
68
|
evaluation = mock(:object => order)
|
69
69
|
CreateThroughRepositoryStrategy.new.result(evaluation)
|
70
70
|
end
|
71
|
+
|
72
|
+
it "does not create dependencies when not defined" do
|
73
|
+
order = Order.new(:payment => nil)
|
74
|
+
order_mapper.should_receive(:create).ordered.with(order).and_return(true)
|
75
|
+
evaluation = mock(:object => order)
|
76
|
+
CreateThroughRepositoryStrategy.new.result(evaluation)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "does not create an entity when it's already persisted" do
|
80
|
+
payment = Payment.new(:id => 1)
|
81
|
+
order = Order.new(:payment => payment)
|
82
|
+
payment_mapper.should_not_receive(:create)
|
83
|
+
order_mapper.should_receive(:create).ordered.with(order).and_return(true)
|
84
|
+
evaluation = mock(:object => order)
|
85
|
+
CreateThroughRepositoryStrategy.new.result(evaluation)
|
86
|
+
end
|
71
87
|
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.5
|
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-05-
|
12
|
+
date: 2013-05-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minimapper
|