minimapper-extras 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ed4608bcd50ac8efa3a679ce16b038d9afa6982
4
- data.tar.gz: f51fe75fc87a6ab8227e3a13a7440e9ca2c7bf3e
3
+ metadata.gz: e25e9f439adce376313c1e6689e66aab2ee8591d
4
+ data.tar.gz: c927432be7676680810eccd2a0cbf5644cca82bb
5
5
  SHA512:
6
- metadata.gz: 8af1efffe8ef6e20aafcae764e8494d88ced0d47f75e29b1864417f3c8f9489824672ed8575ec7f72dbcd81cc888b049338894b1292ab0eb1c3dc1e427ea0352
7
- data.tar.gz: f3f3d09427adcbc1d83479253915ad64f0c3a86e6900404f8ff11d66d47dbfafc8490cd6a067c7c49b2676fa6a7db286684869c8927887c48400cd97bd960996
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, entity_class = nil)
7
- if entity_class
8
- class_name = entity_class.name
9
- else
10
- class_name = name.to_s.camelcase
11
- end
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
- define_method(name) do # def foo
14
- eval("@#{name} ||= ::#{class_name}.new(#{name}_attributes || {})") # @foo ||= ::Foo.new(foo_attributes || {})
15
- end # end
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
 
@@ -1,5 +1,5 @@
1
1
  module Minimapper
2
2
  module Extras
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -20,7 +20,9 @@ class CreateThroughRepositoryStrategy
20
20
  # "customer { FactoryGirl.build(:customer) }" in factories.
21
21
  create_dependencies
22
22
 
23
- if mapper_with_name(mapper_name).create(entity)
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
- dependency_entity = self.class.new(entity.public_send(name)).create
39
- entity.public_send("#{name}=", dependency_entity)
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
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-03 00:00:00.000000000 Z
12
+ date: 2013-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minimapper