minimapper-extras 0.1.0 → 0.2.0

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: 6cc35405d6f7b7f30359b489d1f6752dc2c8c1a7
4
- data.tar.gz: a57bf9bfd4470d7fed953cae1525ff16a4827b0b
3
+ metadata.gz: ee41db88d4c2c64577d469e09c24d8a350db3fd5
4
+ data.tar.gz: de05db712f656b5802d997637d0b73c2c4191a57
5
5
  SHA512:
6
- metadata.gz: 756bd40e72e990aa5e4138ccb0057ce106fb54bde9b4b31ee6bbcb22aa1fa308fba534b50fa7711e7c47306d10d76aab7e9b0de660cee007293ff38596fc633a
7
- data.tar.gz: 41cd71d233c1dcb8c8255da4722de18ef6444810f4ace5afd4fd9bb0f5d217104972f28e86f724845997eddf65609f28369a771705243c0156eecd445e36e00f
6
+ metadata.gz: 9b4d3bcf25b07ffbefcf1eab86250fff4a6faecf023576d4757aaa2a4fa4bd1d93ebb90b412a7f857e4b8c8f439c5e7450bb6b7e32499c51b4bbdd472fff2647
7
+ data.tar.gz: 22c327686c6c75bc6b8085d117dc7c67d33c03d42a5c7a8449a508c81ad0dd7467b21a48451ea5c8aa0d98055f8e6c7151c924c45be04c5c86241fc120eff6cb
@@ -3,18 +3,30 @@ require "minimapper/entity"
3
3
  module Minimapper
4
4
  module Entity
5
5
  module HasMany
6
- def has_many(name)
7
- attr_writer name
6
+ def has_many(name, opts = {})
7
+ serialize = opts.fetch(:serialize, false)
8
8
 
9
- define_method(name) do
10
- # @foo ||= []
11
- ivar = instance_variable_get("@#{name}")
9
+ attr_writer name
12
10
 
13
- if ivar
14
- ivar
15
- else
16
- instance_variable_set("@#{name}", [])
11
+ if serialize
12
+ setup_has_many(name) do
13
+ attributes = send("#{name}_attributes")
14
+ entity_class = name.to_s.singularize.camelcase.constantize
15
+ (attributes || []).map { |attr| entity_class.new(attr) }
17
16
  end
17
+ else
18
+ setup_has_many(name) { [] }
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def setup_has_many(name, &data)
25
+ # Basically does: @thename ||= data.call
26
+ # The data block is evaluated in the instance context.
27
+ define_method(name) do
28
+ instance_variable_get("@#{name}") ||
29
+ instance_variable_set("@#{name}", instance_eval(&data))
18
30
  end
19
31
  end
20
32
  end
@@ -1,5 +1,5 @@
1
1
  module Minimapper
2
2
  module Extras
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -12,7 +12,12 @@ Gem::Specification.new do |spec|
12
12
  spec.homepage = ""
13
13
  spec.license = "MIT"
14
14
 
15
- spec.files = `git ls-files`.split($/)
15
+ # We bundle this gem within the app, having this leads
16
+ # to git errors when deploying.
17
+ if File.exists?(".git")
18
+ spec.files = `git ls-files`.split($/)
19
+ end
20
+
16
21
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
22
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
23
  spec.require_paths = ["lib"]
@@ -1,26 +1,60 @@
1
1
  require "spec_helper"
2
2
  require "minimapper/entity/has_many"
3
3
 
4
+ class Address
5
+ include Minimapper::Entity
6
+
7
+ attribute :street
8
+ end
9
+
4
10
  class Customer
5
11
  include Minimapper::Entity
6
12
 
7
13
  has_many :cars
14
+
15
+ attribute :addresses_attributes
16
+ has_many :addresses, :serialize => true
8
17
  end
9
18
 
10
19
  describe Minimapper::Entity::HasMany do
11
- it "defaults to an empty array" do
12
- Customer.new.cars.should be_empty
13
- end
20
+ context "with serialize: false (the default)" do
21
+ it "defaults to an empty array" do
22
+ Customer.new.cars.should be_empty
23
+ end
24
+
25
+ it "can be added to" do
26
+ customer = Customer.new
27
+ customer.cars << :foo
28
+ customer.cars.should == [ :foo ]
29
+ end
14
30
 
15
- it "can be added to" do
16
- customer = Customer.new
17
- customer.cars << :foo
18
- customer.cars.should == [ :foo ]
31
+ it "can be set" do
32
+ customer = Customer.new
33
+ customer.cars = [ :foo ]
34
+ customer.cars.should == [ :foo ]
35
+ end
19
36
  end
20
37
 
21
- it "can be set" do
22
- customer = Customer.new
23
- customer.cars = [ :foo ]
24
- customer.cars.should == [ :foo ]
38
+ context "with serialize: true" do
39
+ it "returns an empty collection for a new instance" do
40
+ Customer.new.addresses.should be_empty
41
+ end
42
+
43
+ it "fills the collection using data in entities_attributes" do
44
+ addresses = Customer.new(:addresses_attributes => [ { :street => "Street 55" } ]).addresses
45
+ addresses.size.should == 1
46
+ addresses.first.street.should == "Street 55"
47
+ end
48
+
49
+ it "memoizes the collection" do
50
+ customer = Customer.new(:addresses_attributes => [ { :street => "Street 55" } ])
51
+ customer.addresses.should == customer.addresses
52
+ end
53
+
54
+ it "can be set" do
55
+ customer = Customer.new
56
+ customer.addresses = [ :foo ]
57
+ customer.addresses.should == [ :foo ]
58
+ end
25
59
  end
26
60
  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.1.0
4
+ version: 0.2.0
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-17 00:00:00.000000000 Z
12
+ date: 2013-05-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minimapper
@@ -128,7 +128,6 @@ files:
128
128
  - lib/minimapper/entity/conversions.rb
129
129
  - lib/minimapper/entity/has_many.rb
130
130
  - lib/minimapper/entity/serialized_association.rb
131
- - lib/minimapper/entity/serialized_collection.rb
132
131
  - lib/minimapper/extras.rb
133
132
  - lib/minimapper/extras/version.rb
134
133
  - lib/minimapper/factory_girl.rb
@@ -140,7 +139,6 @@ files:
140
139
  - spec/minimapper/entity/conversions_spec.rb
141
140
  - spec/minimapper/entity/has_many_spec.rb
142
141
  - spec/minimapper/entity/serialized_association_spec.rb
143
- - spec/minimapper/entity/serialized_collection_spec.rb
144
142
  - spec/minimapper/factory_girl_spec.rb
145
143
  - spec/minimapper/mapper/has_many_association_spec.rb
146
144
  - spec/spec_helper.rb
@@ -173,7 +171,6 @@ test_files:
173
171
  - spec/minimapper/entity/conversions_spec.rb
174
172
  - spec/minimapper/entity/has_many_spec.rb
175
173
  - spec/minimapper/entity/serialized_association_spec.rb
176
- - spec/minimapper/entity/serialized_collection_spec.rb
177
174
  - spec/minimapper/factory_girl_spec.rb
178
175
  - spec/minimapper/mapper/has_many_association_spec.rb
179
176
  - spec/spec_helper.rb
@@ -1,27 +0,0 @@
1
- require "minimapper/entity"
2
-
3
- module Minimapper
4
- module Entity
5
- module SerializedCollection
6
- def serialized_collection(name)
7
- entity_class = name.to_s.singularize.camelcase.constantize
8
-
9
- define_method(name) do
10
- ivar = instance_variable_get("@#{name}")
11
-
12
- if ivar
13
- ivar
14
- else
15
- attributes = send("#{name}_attributes")
16
- data = (attributes || []).map { |attr| entity_class.new(attr) }
17
- instance_variable_set("@#{name}", data)
18
- end
19
- end
20
- end
21
- end
22
-
23
- module Attributes
24
- include SerializedCollection
25
- end
26
- end
27
- end
@@ -1,32 +0,0 @@
1
- require "spec_helper"
2
- require "minimapper/entity/serialized_collection"
3
-
4
- class Address
5
- include Minimapper::Entity
6
-
7
- attribute :street
8
- end
9
-
10
- class Customer
11
- include Minimapper::Entity
12
-
13
- attribute :addresses_attributes
14
- serialized_collection :addresses
15
- end
16
-
17
- describe Minimapper::Entity::SerializedCollection do
18
- it "returns an empty collection for a new instance" do
19
- Customer.new.addresses.should be_empty
20
- end
21
-
22
- it "fills the collection using data in entities_attributes" do
23
- addresses = Customer.new(:addresses_attributes => [ { :street => "Street 55" } ]).addresses
24
- addresses.size.should == 1
25
- addresses.first.street.should == "Street 55"
26
- end
27
-
28
- it "memoizes the collection" do
29
- customer = Customer.new(:addresses_attributes => [ { :street => "Street 55" } ])
30
- customer.addresses.should == customer.addresses
31
- end
32
- end