minimapper-extras 0.1.0 → 0.2.0
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 +4 -4
- data/lib/minimapper/entity/has_many.rb +21 -9
- data/lib/minimapper/extras/version.rb +1 -1
- data/minimapper-extras.gemspec +6 -1
- data/spec/minimapper/entity/has_many_spec.rb +45 -11
- metadata +2 -5
- data/lib/minimapper/entity/serialized_collection.rb +0 -27
- data/spec/minimapper/entity/serialized_collection_spec.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee41db88d4c2c64577d469e09c24d8a350db3fd5
|
4
|
+
data.tar.gz: de05db712f656b5802d997637d0b73c2c4191a57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
6
|
+
def has_many(name, opts = {})
|
7
|
+
serialize = opts.fetch(:serialize, false)
|
8
8
|
|
9
|
-
|
10
|
-
# @foo ||= []
|
11
|
-
ivar = instance_variable_get("@#{name}")
|
9
|
+
attr_writer name
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
data/minimapper-extras.gemspec
CHANGED
@@ -12,7 +12,12 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.homepage = ""
|
13
13
|
spec.license = "MIT"
|
14
14
|
|
15
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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.
|
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-
|
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
|