minimapper-extras 0.0.1
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 +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +8 -0
- data/lib/minimapper/entity/belongs_to.rb +30 -0
- data/lib/minimapper/entity/serialized_association.rb +23 -0
- data/lib/minimapper/extras/version.rb +5 -0
- data/lib/minimapper/extras.rb +6 -0
- data/minimapper-extras.gemspec +28 -0
- data/script/ci +3 -0
- data/script/turbux_rspec +8 -0
- data/spec/minimapper/entity/belongs_to_spec.rb +64 -0
- data/spec/minimapper/entity/serialized_association_spec.rb +41 -0
- data/spec/spec_helper.rb +3 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42df3b244862e78e980ea66d96b31a8bd2d8c6ce
|
4
|
+
data.tar.gz: efe6cba3602a1253e5f775b22bb162050362f90a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a58cb2feb537a00740dbe89c1f84ee55b7a116cdda136f5fe29e2fccad7d2c4f73462400bba4044f44eb2b7bc46810c81d93122c2220a6dc3b7402f36c29537a
|
7
|
+
data.tar.gz: b88fa0ed5eacf7b8e89e638d96d91ca55003574cf34bcd7834ca89e2c8f8679335ac84d02174b58f0d2db4279a4e730bb525839b884069a19c22a35af4f1006d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Barsoom AB
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
[](http://travis-ci.org/barsoom/minimapper-extras)
|
2
|
+
[](https://codeclimate.com/github/barsoom/minimapper-extras)
|
3
|
+
|
4
|
+
# Minimapper::Extras
|
5
|
+
|
6
|
+
Extra tools for minimapper.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'minimapper-extras'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install minimapper-extras
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
For now, see the specs. TODO: Write docs.
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "minimapper/entity"
|
2
|
+
|
3
|
+
module Minimapper
|
4
|
+
module Entity
|
5
|
+
module BelongsTo
|
6
|
+
def belongs_to(association)
|
7
|
+
define_method("#{association}=") do |value| # def employee=(employee)
|
8
|
+
instance_variable_set("@#{association}", value) # @employee = employee
|
9
|
+
id = value ? value.id : nil # id = employee ? employee.id : nil
|
10
|
+
send("#{association}_id=", id) # self.employee_id = id
|
11
|
+
end # end
|
12
|
+
|
13
|
+
attribute :"#{association}_id" # attribute :employee_id
|
14
|
+
|
15
|
+
define_method(association) do # def employee
|
16
|
+
ivar = instance_variable_get("@#{association}") # ivar = @employee
|
17
|
+
if send("#{association}_id") && !ivar # if employee_id && !ivar
|
18
|
+
raise "Was assigned #{association}_id but no record!" # raise "..."
|
19
|
+
else # else
|
20
|
+
ivar # ivar
|
21
|
+
end # end
|
22
|
+
end # end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module Minimapper::Entity::Attributes
|
29
|
+
include Minimapper::Entity::BelongsTo
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "minimapper/entity"
|
2
|
+
|
3
|
+
module Minimapper
|
4
|
+
module Entity
|
5
|
+
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
|
12
|
+
|
13
|
+
define_method(name) do # def foo
|
14
|
+
eval("@#{name} ||= ::#{class_name}.new(#{name}_attributes || {})") # @foo ||= ::Foo.new(foo_attributes || {})
|
15
|
+
end # end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Attributes
|
20
|
+
include SerializedAssociation
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'minimapper/extras/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "minimapper-extras"
|
8
|
+
spec.version = Minimapper::Extras::VERSION
|
9
|
+
spec.authors = ["Joakim Kolsjö", "Henrik Nyh"]
|
10
|
+
spec.email = ["joakim.kolsjo@gmail.com", "henrik@barsoom.se"]
|
11
|
+
spec.summary = %q{Extras for Minimapper.}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "minimapper"
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
|
25
|
+
# We can't assume that everything in this gem needs activemodel, but we
|
26
|
+
# need it to run all of the specs.
|
27
|
+
spec.add_development_dependency "activemodel"
|
28
|
+
end
|
data/script/ci
ADDED
data/script/turbux_rspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "minimapper/entity/belongs_to"
|
3
|
+
|
4
|
+
class Balloon
|
5
|
+
include Minimapper::Entity
|
6
|
+
end
|
7
|
+
|
8
|
+
class Customer
|
9
|
+
include Minimapper::Entity
|
10
|
+
|
11
|
+
belongs_to :balloon
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Minimapper::Entity::BelongsTo do
|
15
|
+
it "defines an _id attribute" do
|
16
|
+
customer = Customer.new
|
17
|
+
customer.balloon_id = 123
|
18
|
+
customer.attributes.should include(:balloon_id)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "assigns id when assigning entity" do
|
22
|
+
customer = Customer.new
|
23
|
+
balloon = Balloon.new(:id => 123)
|
24
|
+
customer.balloon = balloon
|
25
|
+
customer.balloon_id.should == 123
|
26
|
+
end
|
27
|
+
|
28
|
+
it "provides an entity writer" do
|
29
|
+
customer = Customer.new
|
30
|
+
balloon = Balloon.new
|
31
|
+
customer.balloon = balloon
|
32
|
+
customer.balloon.should == balloon
|
33
|
+
end
|
34
|
+
|
35
|
+
# The mapper should load the record.
|
36
|
+
# If it doesn't, we complain to help avoid confusion.
|
37
|
+
it "complains if id exists but record isn't loaded" do
|
38
|
+
# Only id, no loaded entity.
|
39
|
+
customer = Customer.new
|
40
|
+
customer.balloon_id = 123
|
41
|
+
expect { customer.balloon }.to raise_error(/balloon_id but no record/)
|
42
|
+
|
43
|
+
# Loaded entity, also sets id.
|
44
|
+
customer = Customer.new
|
45
|
+
balloon = Balloon.new(:id => 123)
|
46
|
+
customer.balloon = balloon
|
47
|
+
customer.balloon_id.should == 123 # Sanity.
|
48
|
+
customer.balloon.should == balloon
|
49
|
+
|
50
|
+
# Loaded entity without id.
|
51
|
+
# NOTE: a mapper won't see the balloon.
|
52
|
+
customer = Customer.new
|
53
|
+
balloon = Balloon.new(:id => nil)
|
54
|
+
customer.balloon = balloon
|
55
|
+
customer.balloon_id.should be_nil
|
56
|
+
customer.balloon.should == balloon
|
57
|
+
|
58
|
+
# No id, no entity.
|
59
|
+
customer = Customer.new
|
60
|
+
customer.balloon = nil
|
61
|
+
customer.balloon_id = nil
|
62
|
+
customer.balloon.should be_nil
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "minimapper/entity/serialized_association"
|
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 :address_attributes
|
14
|
+
attribute :visit_address_attributes
|
15
|
+
|
16
|
+
serialized_association :address
|
17
|
+
serialized_association :visit_address, Address
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Minimapper::Entity::SerializedAssociation do
|
21
|
+
it "returns an empty associated entity for a new instance" do
|
22
|
+
address = Customer.new.address
|
23
|
+
address.should be_kind_of(Address)
|
24
|
+
address.attributes.should == {}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "fills the associated entity using data in entity_attributes" do
|
28
|
+
address = Customer.new(:address_attributes => { :street => "Street 55" }).address
|
29
|
+
address.street.should == "Street 55"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "memoizes the associated entity" do
|
33
|
+
customer = Customer.new(:address_attributes => { :street => "Street 55" })
|
34
|
+
customer.address.should == customer.address
|
35
|
+
end
|
36
|
+
|
37
|
+
it "can use a specified entity type" do
|
38
|
+
customer = Customer.new(:visit_address_attributes => { :street => "Street 66" })
|
39
|
+
customer.visit_address.street.should == "Street 66"
|
40
|
+
end
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minimapper-extras
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joakim Kolsjö
|
8
|
+
- Henrik Nyh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minimapper
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.3'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.3'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: activemodel
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description:
|
85
|
+
email:
|
86
|
+
- joakim.kolsjo@gmail.com
|
87
|
+
- henrik@barsoom.se
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- .rspec
|
94
|
+
- .travis.yml
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- lib/minimapper/entity/belongs_to.rb
|
100
|
+
- lib/minimapper/entity/serialized_association.rb
|
101
|
+
- lib/minimapper/extras.rb
|
102
|
+
- lib/minimapper/extras/version.rb
|
103
|
+
- minimapper-extras.gemspec
|
104
|
+
- script/ci
|
105
|
+
- script/turbux_rspec
|
106
|
+
- spec/minimapper/entity/belongs_to_spec.rb
|
107
|
+
- spec/minimapper/entity/serialized_association_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
homepage: ''
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.0.0
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Extras for Minimapper.
|
133
|
+
test_files:
|
134
|
+
- spec/minimapper/entity/belongs_to_spec.rb
|
135
|
+
- spec/minimapper/entity/serialized_association_spec.rb
|
136
|
+
- spec/spec_helper.rb
|