minimapper-extras 0.0.2 → 0.0.3
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/conversions.rb +17 -0
- data/lib/minimapper/entity/serialized_collection.rb +27 -0
- data/lib/minimapper/extras/version.rb +1 -1
- data/spec/minimapper/entity/conversions_spec.rb +23 -0
- data/spec/minimapper/entity/serialized_collection_spec.rb +32 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6610be385fe090c3dc2f46c8cc6a61d194678b5a
|
4
|
+
data.tar.gz: ad86c1431b1c257f204dbe5bb9db80d4c60bbed7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7e84fda8c61543b86562bc344661e3dfed9ffc0e80a5a142b4737bbf7e711a5356af024335ce06cef8adc93d7fba68c782201bb721eb62481dd064f763705e1
|
7
|
+
data.tar.gz: 1fedd7ba4bf5496f9a72831cc1c0ebb3c2e46ad20d4eeb2093a570d0879e7ea5074ab25749e5759efc64065021db8b10cb8a01313e775897b87f27baef6baac5
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'minimapper/entity'
|
2
|
+
require "date"
|
3
|
+
|
4
|
+
class ToDate
|
5
|
+
def convert(value)
|
6
|
+
Date.parse(value) rescue nil
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class ToBoolean
|
11
|
+
def convert(value)
|
12
|
+
!!value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Minimapper::Entity::Convert.register_converter(:date, ToDate.new)
|
17
|
+
Minimapper::Entity::Convert.register_converter(:boolean, ToBoolean.new)
|
@@ -0,0 +1,27 @@
|
|
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
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "minimapper/entity/conversions"
|
3
|
+
|
4
|
+
class User
|
5
|
+
include Minimapper::Entity
|
6
|
+
|
7
|
+
attribute :signed_up_on, :date
|
8
|
+
attribute :flagged, :boolean
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "Convertions" do
|
12
|
+
it "can covert dates" do
|
13
|
+
User.new(:signed_up_on => "2001-01-01").signed_up_on.should == Date.new(2001, 1, 1)
|
14
|
+
User.new(:signed_up_on => "BAD DATA").signed_up_on.should be_nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "can convert booleans" do
|
18
|
+
User.new(:flagged => true).flagged.should eq(true)
|
19
|
+
User.new(:flagged => "yep").flagged.should eq(true)
|
20
|
+
User.new(:flagged => nil).flagged.should be_nil
|
21
|
+
User.new(:flagged => false).flagged.should eq(false)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,32 @@
|
|
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
|
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.3
|
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-04-
|
12
|
+
date: 2013-04-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minimapper
|
@@ -125,7 +125,9 @@ files:
|
|
125
125
|
- README.md
|
126
126
|
- Rakefile
|
127
127
|
- lib/minimapper/entity/belongs_to.rb
|
128
|
+
- lib/minimapper/entity/conversions.rb
|
128
129
|
- lib/minimapper/entity/serialized_association.rb
|
130
|
+
- lib/minimapper/entity/serialized_collection.rb
|
129
131
|
- lib/minimapper/extras.rb
|
130
132
|
- lib/minimapper/extras/version.rb
|
131
133
|
- lib/minimapper/factory_girl.rb
|
@@ -133,7 +135,9 @@ files:
|
|
133
135
|
- script/ci
|
134
136
|
- script/turbux_rspec
|
135
137
|
- spec/minimapper/entity/belongs_to_spec.rb
|
138
|
+
- spec/minimapper/entity/conversions_spec.rb
|
136
139
|
- spec/minimapper/entity/serialized_association_spec.rb
|
140
|
+
- spec/minimapper/entity/serialized_collection_spec.rb
|
137
141
|
- spec/minimapper/factory_girl_spec.rb
|
138
142
|
- spec/spec_helper.rb
|
139
143
|
homepage: ''
|
@@ -162,6 +166,8 @@ specification_version: 4
|
|
162
166
|
summary: Extras for Minimapper.
|
163
167
|
test_files:
|
164
168
|
- spec/minimapper/entity/belongs_to_spec.rb
|
169
|
+
- spec/minimapper/entity/conversions_spec.rb
|
165
170
|
- spec/minimapper/entity/serialized_association_spec.rb
|
171
|
+
- spec/minimapper/entity/serialized_collection_spec.rb
|
166
172
|
- spec/minimapper/factory_girl_spec.rb
|
167
173
|
- spec/spec_helper.rb
|