virtus-mapper_opp_fork 0.4.2
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/.DS_Store +0 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +15 -0
- data/lib/virtus/mapper.rb +76 -0
- data/lib/virtus/mapper/version.rb +5 -0
- data/scratchpad.md +9 -0
- data/spec/spec_helper.rb +89 -0
- data/spec/virtus/mapper_spec.rb +275 -0
- data/virtus-mapper.gemspec +27 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e27c46f276815c323ccaf8dcbf642f589dfa722
|
4
|
+
data.tar.gz: dddbf24e40a49137936e5324594a92a8ed8edc3f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d0bb4a3f36e0fa358bd828c184d00a235b320a9d059f0a9c4f535680ea283908024bea9fb387a4b70905dd563034abd5697ecff36869bc9bc4c550be2debde2
|
7
|
+
data.tar.gz: 3d1f46cace6b661e07b1603677485b8236646dea8f05bd72ca0cca3a8cbe3e92fb3bb1a0507eb7bf92caf66aa13cecbc3d2adb288e9853976a49a3a7a20b890f
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 TJ Stankus
|
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,58 @@
|
|
1
|
+
# Virtus::Mapper
|
2
|
+
|
3
|
+
Mapper for Virtus attributes
|
4
|
+
|
5
|
+
NOTE: Recent commits allow for mixing in Virtus modules to Virtus object instances, which is a break from recommended Virtus usage.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'virtus-mapper'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install virtus-mapper
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
In your Virtus attributes, set the `:from` option to a symbol to translate keys
|
26
|
+
on object initialization. In the example below, `:surname` gets translated into
|
27
|
+
`:last_name`. If the `:from` option is set to an object that
|
28
|
+
`respond_to?(:call)`, the object will be called and passed the attributes hash.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class Person
|
32
|
+
include Virtus.model
|
33
|
+
include Virtus::Mapper
|
34
|
+
|
35
|
+
attribute :first_name, String
|
36
|
+
attribute :last_name, String, from: :surname
|
37
|
+
attribute :address,
|
38
|
+
String,
|
39
|
+
default: '',
|
40
|
+
from: lambda { |atts| atts[:address][:street] rescue '' }
|
41
|
+
end
|
42
|
+
|
43
|
+
person = Person.new({ first_name: 'John',
|
44
|
+
surname: 'Doe',
|
45
|
+
address: { 'street' => '1122 Boogie Avenue' } })
|
46
|
+
person.first_name # => 'John'
|
47
|
+
person.last_name # => 'Doe'
|
48
|
+
person.address # => '1122 Boogie Avenue'
|
49
|
+
person.surname # => NoMethodError
|
50
|
+
```
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it ( https://github.com/[my-github-username]/virtus-mapper/fork )
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
begin # don't puke if rspec isn't available
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
desc 'Run specs'
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |r|
|
8
|
+
r.verbose = false
|
9
|
+
r.rspec_opts = '-t ~integration'
|
10
|
+
end
|
11
|
+
|
12
|
+
rescue LoadError
|
13
|
+
end
|
14
|
+
|
15
|
+
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'virtus/mapper/version'
|
2
|
+
require 'virtus'
|
3
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
4
|
+
|
5
|
+
HWIA = ActiveSupport::HashWithIndifferentAccess
|
6
|
+
|
7
|
+
module Virtus
|
8
|
+
module Mapper
|
9
|
+
|
10
|
+
attr_reader :raw_attributes, :nil_value_keys
|
11
|
+
attr_accessor :attr_set
|
12
|
+
|
13
|
+
def initialize(attrs={})
|
14
|
+
@attr_set = init_attr_set
|
15
|
+
@raw_attributes = HWIA.new(attrs)
|
16
|
+
@nil_value_keys = @raw_attributes.collect { |k, v| k if v.nil? }.compact
|
17
|
+
super(mapped_attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_attributes(mod)
|
21
|
+
mod_attrs = module_attributes(mod)
|
22
|
+
attr_set.merge(mod_attrs)
|
23
|
+
mapped_attrs = mapped_attributes
|
24
|
+
mod_attrs.each do |attr|
|
25
|
+
value = attr.coerce(mapped_attrs[attr.name])
|
26
|
+
define_singleton_method(attr.name) { value }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def init_attr_set
|
33
|
+
Virtus::AttributeSet.new(nil, class_attributes)
|
34
|
+
end
|
35
|
+
|
36
|
+
def class_attributes
|
37
|
+
self.class.attribute_set.to_a
|
38
|
+
end
|
39
|
+
|
40
|
+
def module_attributes(mod)
|
41
|
+
Class.new { include mod }.attribute_set.to_a
|
42
|
+
end
|
43
|
+
|
44
|
+
def mapped_attributes
|
45
|
+
attrs = raw_attributes.clone
|
46
|
+
attrs.tap do |h|
|
47
|
+
attributes_to_map_by_symbol(attrs).each do |att|
|
48
|
+
h[att.name] = h.delete(from(att))
|
49
|
+
end
|
50
|
+
attributes_to_map_by_call.each do |att|
|
51
|
+
h[att.name] = from(att).call(h)
|
52
|
+
end
|
53
|
+
end.delete_if { |k, v| delete_nil_value_for?(k, v) }
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete_nil_value_for?(k, v)
|
57
|
+
!nil_value_keys.include?(k) && v.nil?
|
58
|
+
end
|
59
|
+
|
60
|
+
def attributes_to_map_by_symbol(attrs)
|
61
|
+
attributes_to_map - attributes_to_map_by_call
|
62
|
+
end
|
63
|
+
|
64
|
+
def attributes_to_map_by_call
|
65
|
+
attributes_to_map.select { |att| from(att).respond_to?(:call) }
|
66
|
+
end
|
67
|
+
|
68
|
+
def attributes_to_map
|
69
|
+
attr_set.select { |att| !(from(att).nil?) }
|
70
|
+
end
|
71
|
+
|
72
|
+
def from(attribute)
|
73
|
+
attribute.options[:from]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/scratchpad.md
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
10
|
+
# a separate helper file that requires the additional dependencies and performs
|
11
|
+
# the additional setup, and require it from the spec files that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
RSpec.configure do |config|
|
18
|
+
# rspec-expectations config goes here. You can use an alternate
|
19
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
20
|
+
# assertions if you prefer.
|
21
|
+
config.expect_with :rspec do |expectations|
|
22
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
23
|
+
# and `failure_message` of custom matchers include text for helper methods
|
24
|
+
# defined using `chain`, e.g.:
|
25
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
26
|
+
# # => "be bigger than 2 and smaller than 4"
|
27
|
+
# ...rather than:
|
28
|
+
# # => "be bigger than 2"
|
29
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
30
|
+
end
|
31
|
+
|
32
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
33
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
34
|
+
config.mock_with :rspec do |mocks|
|
35
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
36
|
+
# a real object. This is generally recommended, and will default to
|
37
|
+
# `true` in RSpec 4.
|
38
|
+
mocks.verify_partial_doubles = true
|
39
|
+
end
|
40
|
+
|
41
|
+
# The settings below are suggested to provide a good initial experience
|
42
|
+
# with RSpec, but feel free to customize to your heart's content.
|
43
|
+
=begin
|
44
|
+
# These two settings work together to allow you to limit a spec run
|
45
|
+
# to individual examples or groups you care about by tagging them with
|
46
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
47
|
+
# get run.
|
48
|
+
config.filter_run :focus
|
49
|
+
config.run_all_when_everything_filtered = true
|
50
|
+
|
51
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
52
|
+
# For more details, see:
|
53
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
54
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
55
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
56
|
+
config.disable_monkey_patching!
|
57
|
+
|
58
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
59
|
+
# be too noisy due to issues in dependencies.
|
60
|
+
config.warnings = true
|
61
|
+
|
62
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
63
|
+
# file, and it's useful to allow more verbose output when running an
|
64
|
+
# individual spec file.
|
65
|
+
if config.files_to_run.one?
|
66
|
+
# Use the documentation formatter for detailed output,
|
67
|
+
# unless a formatter has already been configured
|
68
|
+
# (e.g. via a command-line flag).
|
69
|
+
config.default_formatter = 'doc'
|
70
|
+
end
|
71
|
+
|
72
|
+
# Print the 10 slowest examples and example groups at the
|
73
|
+
# end of the spec run, to help surface which specs are running
|
74
|
+
# particularly slow.
|
75
|
+
config.profile_examples = 10
|
76
|
+
|
77
|
+
# Run specs in random order to surface order dependencies. If you find an
|
78
|
+
# order dependency and want to debug it, you can fix the order by providing
|
79
|
+
# the seed, which is printed after each run.
|
80
|
+
# --seed 1234
|
81
|
+
config.order = :random
|
82
|
+
|
83
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
84
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
85
|
+
# test failures related to randomization by passing the same `--seed` value
|
86
|
+
# as the one that triggered the failure.
|
87
|
+
Kernel.srand config.seed
|
88
|
+
=end
|
89
|
+
end
|
@@ -0,0 +1,275 @@
|
|
1
|
+
require 'virtus'
|
2
|
+
require 'virtus/mapper'
|
3
|
+
|
4
|
+
module Virtus
|
5
|
+
RSpec.describe Mapper do
|
6
|
+
|
7
|
+
before do
|
8
|
+
module Examples
|
9
|
+
class PersonMapper
|
10
|
+
include Virtus.model
|
11
|
+
include Virtus::Mapper
|
12
|
+
|
13
|
+
attribute :id, Integer, from: :person_id, strict: true, required: true
|
14
|
+
attribute :first_name, String
|
15
|
+
attribute :last_name, String, from: :surname
|
16
|
+
attribute :address,
|
17
|
+
String,
|
18
|
+
default: '',
|
19
|
+
from: lambda { |atts| atts[:address][:street] rescue '' }
|
20
|
+
end
|
21
|
+
|
22
|
+
module EmploymentMapper
|
23
|
+
include Virtus.module
|
24
|
+
include Virtus::Mapper
|
25
|
+
|
26
|
+
attribute :company, String, from: :business
|
27
|
+
attribute :job_title, String, from: :position
|
28
|
+
attribute :salary, Integer
|
29
|
+
attribute :fulltime, Virtus::Attribute::Boolean
|
30
|
+
end
|
31
|
+
|
32
|
+
module TraitsMapper
|
33
|
+
include Virtus.module
|
34
|
+
include Virtus::Mapper
|
35
|
+
|
36
|
+
attribute :eye_color, String, from: :eyecolor
|
37
|
+
end
|
38
|
+
|
39
|
+
class DogMapper
|
40
|
+
include Virtus.model
|
41
|
+
include Virtus::Mapper
|
42
|
+
|
43
|
+
attribute :name, String, from: :shelter, default: 'Spot'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
let(:person_id) { 1 }
|
49
|
+
let(:first_name) { 'John' }
|
50
|
+
let(:last_name) { 'Doe' }
|
51
|
+
let(:address) { '1122 Something Avenue' }
|
52
|
+
let(:person_attrs) {
|
53
|
+
{ person_id: person_id,
|
54
|
+
first_name: first_name,
|
55
|
+
surname: last_name,
|
56
|
+
address: { 'street' => address } }
|
57
|
+
}
|
58
|
+
let(:employment_attrs) {
|
59
|
+
{ salary: 100,
|
60
|
+
business: 'RentPath',
|
61
|
+
position: 'Programmer',
|
62
|
+
fulltime: '1' }
|
63
|
+
}
|
64
|
+
let(:person) { Examples::PersonMapper.new(person_attrs) }
|
65
|
+
|
66
|
+
describe '#attr_set' do
|
67
|
+
it 'initializes to non-nil value' do
|
68
|
+
expect(person.attr_set).not_to be_nil
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'contains expected attributes' do
|
72
|
+
attr_set_names = person.attr_set.collect(&:name)
|
73
|
+
[:id, :first_name, :last_name, :address].each do |name|
|
74
|
+
expect(attr_set_names).to include(name)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
it "is not the same as Virtus's class-scope attribute_set" do
|
79
|
+
expect(person.attr_set).not_to equal(person.class.attribute_set)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'attribute with from option as symbol' do
|
84
|
+
it 'translates key' do
|
85
|
+
expect(person.last_name).to eq(last_name)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'does not create method from original key' do
|
89
|
+
expect { person.surname }.to raise_error(NoMethodError)
|
90
|
+
end
|
91
|
+
|
92
|
+
describe 'required attribute with name key, missing from key' do
|
93
|
+
it 'raises error' do
|
94
|
+
expect { Examples::PersonMapper.new({id: 1}) }.to raise_error
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe 'attribute with name key, missing from key' do
|
99
|
+
it 'returns nil from reader method' do
|
100
|
+
data = { person_id: 1, last_name: 'Smith' }
|
101
|
+
person = Examples::PersonMapper.new(data)
|
102
|
+
expect(person.last_name).to be_nil
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe 'when name and from keys exist in initialized attributes' do
|
107
|
+
it 'prefers from data to name data' do
|
108
|
+
data = person_attrs.merge({ last_name: 'Smith' })
|
109
|
+
person = Examples::PersonMapper.new(data)
|
110
|
+
expect(person.last_name).to eq('Doe')
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe 'attribute with from option as callable object' do
|
116
|
+
it 'calls the object and passes the attributes hash' do
|
117
|
+
callable = Examples::PersonMapper.attribute_set[:address].options[:from]
|
118
|
+
expect(callable).to receive(:call) { person_attrs }
|
119
|
+
Examples::PersonMapper.new(person_attrs)
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'sets attribute value to result of call' do
|
123
|
+
expect(person.address).to eq(address)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe 'attribute without from option' do
|
128
|
+
it 'behaves as usual' do
|
129
|
+
expect(person.first_name).to eq(first_name)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'maps attributes with indifferent access' do
|
134
|
+
person = Examples::PersonMapper.new({ person_id: 1,
|
135
|
+
first_name: first_name,
|
136
|
+
'surname' => last_name })
|
137
|
+
expect(person.last_name).to eq('Doe')
|
138
|
+
end
|
139
|
+
|
140
|
+
describe 'given no arguments to constructor' do
|
141
|
+
it 'does not raise error' do
|
142
|
+
expect { Examples::DogMapper.new }.not_to raise_error
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'respects defaults' do
|
146
|
+
expect(Examples::DogMapper.new.name).to eq('Spot')
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe 'given nil values' do
|
151
|
+
it 'respects nil values' do
|
152
|
+
expect(Examples::DogMapper.new(name: nil).name).to be_nil
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe '#raw_attributes' do
|
157
|
+
let(:person) { Examples::PersonMapper.new(person_attrs.merge({ unused: true })) }
|
158
|
+
|
159
|
+
it 'preserves unused attributes' do
|
160
|
+
expect(person.raw_attributes[:unused]).to be true
|
161
|
+
end
|
162
|
+
|
163
|
+
describe 'keys that do not have corresponding attributes' do
|
164
|
+
it 'do not get instance methods' do
|
165
|
+
expect { person.unused }.to raise_error(NoMethodError)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe '#add_attributes' do
|
171
|
+
let(:data) {
|
172
|
+
{ person_id: person_id,
|
173
|
+
first_name: first_name,
|
174
|
+
surname: last_name,
|
175
|
+
address: { 'street' => address },
|
176
|
+
salary: 100,
|
177
|
+
business: 'RentPath',
|
178
|
+
position: 'Programmer',
|
179
|
+
fulltime: '1' }
|
180
|
+
}
|
181
|
+
let(:person1) { Examples::PersonMapper.new(data) }
|
182
|
+
let(:person2) { Examples::PersonMapper.new(data) }
|
183
|
+
let(:mod) { Examples::EmploymentMapper }
|
184
|
+
|
185
|
+
|
186
|
+
it 'does not affect class-scope attribute_set' do
|
187
|
+
expect { person2.add_attributes(mod) }.not_to change {
|
188
|
+
person2.class.attribute_set.to_a }
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'adds module attributes to #attr_set' do
|
192
|
+
module_attributes = Class.new do
|
193
|
+
include Examples::EmploymentMapper
|
194
|
+
end.attribute_set.to_a
|
195
|
+
person2.add_attributes(mod)
|
196
|
+
attr_names = person2.attr_set.collect(&:name)
|
197
|
+
module_attributes.each do |att|
|
198
|
+
expect(attr_names).to include(att.name)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
describe 'for single module' do
|
203
|
+
let(:person) {
|
204
|
+
Examples::PersonMapper.new(person_attrs.merge(employment_attrs))
|
205
|
+
}
|
206
|
+
|
207
|
+
before do
|
208
|
+
person.add_attributes(Examples::EmploymentMapper)
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'coerces data' do
|
212
|
+
expect(person.fulltime).to be true
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'updates unmapped attribute values' do
|
216
|
+
expect(person.salary).to eq(100)
|
217
|
+
end
|
218
|
+
|
219
|
+
it 'updates mapped attribute values' do
|
220
|
+
expect(person.job_title).to eq('Programmer')
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'adds module attributes to attr_set' do
|
224
|
+
attr_names = person.attr_set.collect(&:name)
|
225
|
+
[:id,
|
226
|
+
:first_name,
|
227
|
+
:last_name,
|
228
|
+
:address,
|
229
|
+
:company,
|
230
|
+
:job_title,
|
231
|
+
:salary].each do |attr_name|
|
232
|
+
expect(attr_names).to include(attr_name)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
describe 'for multiple modules' do
|
238
|
+
let(:person) {
|
239
|
+
Examples::PersonMapper.new(
|
240
|
+
person_attrs.merge(employment_attrs.merge({ eyecolor: 'green' }))
|
241
|
+
)
|
242
|
+
}
|
243
|
+
|
244
|
+
before do
|
245
|
+
person.add_attributes(Examples::EmploymentMapper)
|
246
|
+
person.add_attributes(Examples::TraitsMapper)
|
247
|
+
end
|
248
|
+
|
249
|
+
it 'updates mapped attributes for last module' do
|
250
|
+
expect(person.eye_color).to eq('green')
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'updates mapped attributes for first module' do
|
254
|
+
expect(person.salary).to eq(100)
|
255
|
+
expect(person.company).to eq('RentPath')
|
256
|
+
expect(person.job_title).to eq('Programmer')
|
257
|
+
end
|
258
|
+
|
259
|
+
it 'adds module attributes to attr_set' do
|
260
|
+
attr_names = person.attr_set.collect(&:name)
|
261
|
+
[:id,
|
262
|
+
:first_name,
|
263
|
+
:last_name,
|
264
|
+
:address,
|
265
|
+
:company,
|
266
|
+
:job_title,
|
267
|
+
:salary,
|
268
|
+
:eye_color].each do |attr_name|
|
269
|
+
expect(attr_names).to include(attr_name)
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'virtus/mapper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "virtus-mapper_opp_fork"
|
8
|
+
spec.version = Virtus::Mapper::VERSION
|
9
|
+
spec.authors = ["Bob Firestone"]
|
10
|
+
spec.email = ["rbfiresotne@me.com"]
|
11
|
+
spec.summary = %q{A fork of a Mapper for Virtus attributes to work on rails 4.2}
|
12
|
+
spec.description = %q{Mapper for Virtus attributes. See README.}
|
13
|
+
spec.homepage = "https://github.com/bobfirestone/virtus-mapper"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'virtus', '~> 1.0', '>= 1.0.3'
|
22
|
+
spec.add_runtime_dependency 'activesupport', '>= 4.2.0'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: virtus-mapper_opp_fork
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bob Firestone
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: virtus
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.3
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.0.3
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activesupport
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 4.2.0
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 4.2.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.7'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.7'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '10.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '3.1'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.1'
|
89
|
+
description: Mapper for Virtus attributes. See README.
|
90
|
+
email:
|
91
|
+
- rbfiresotne@me.com
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- ".DS_Store"
|
97
|
+
- ".gitignore"
|
98
|
+
- ".rspec"
|
99
|
+
- Gemfile
|
100
|
+
- LICENSE.txt
|
101
|
+
- README.md
|
102
|
+
- Rakefile
|
103
|
+
- lib/virtus/mapper.rb
|
104
|
+
- lib/virtus/mapper/version.rb
|
105
|
+
- scratchpad.md
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
- spec/virtus/mapper_spec.rb
|
108
|
+
- virtus-mapper.gemspec
|
109
|
+
homepage: https://github.com/bobfirestone/virtus-mapper
|
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.4.5.1
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: A fork of a Mapper for Virtus attributes to work on rails 4.2
|
133
|
+
test_files:
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/virtus/mapper_spec.rb
|