virtus-mapper 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f6d3dd7f2d82df13a3cf325565bf29661a41be65
4
+ data.tar.gz: abaf9425991ef6d6490147d12e6fbb6364a6b71b
5
+ SHA512:
6
+ metadata.gz: 6ac467f783e37e77253df86073802a90f0b1b0fc07b8a6fb4e35c0e840708f13947a2190e7c9a0071c332372433d3d2a249ad05e90d355fa13ff08cb04e2ae42
7
+ data.tar.gz: a8a66858592778990e52e7462847205516191bd7c324981887aec292a1448d052699daab8d07b632697ef7a3de4d242baecdf018ec6b4774cc54baeffc78447c
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ source 'http://gems.idg.primedia.com'
3
+
4
+ # Specify your gem's dependencies in virtus-mapper.gemspec
5
+ gemspec
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,56 @@
1
+ # Virtus::Mapper
2
+
3
+ Mapper for Virtus attributes
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'virtus-mapper'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install virtus-mapper
20
+
21
+ ## Usage
22
+
23
+ In your Virtus attributes, set the `:from` option to a symbol to translate keys
24
+ on object initialization. In the example below, `:surname` gets translated into
25
+ `:last_name`. If the `:from` option is set to an object that
26
+ `respond_to?(:call)`, the object will be called and passed the attributes hash.
27
+
28
+ ```ruby
29
+ class Person
30
+ include Virtus.model
31
+ include Virtus::Mapper
32
+
33
+ attribute :first_name, String
34
+ attribute :last_name, String, from: :surname
35
+ attribute :address,
36
+ String,
37
+ default: '',
38
+ from: lambda { |atts| atts[:address][:street] rescue '' }
39
+ end
40
+
41
+ person = Person.new({ first_name: 'John',
42
+ surname: 'Doe',
43
+ address: { 'street' => '1122 Boogie Avenue' } })
44
+ person.first_name # => 'John'
45
+ person.last_name # => 'Doe'
46
+ person.address # => '1122 Boogie Avenue'
47
+ person.surname # => NoMethodError
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/[my-github-username]/virtus-mapper/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 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,46 @@
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
+ def initialize(attrs)
11
+ super(map_attributes!(HWIA.new(attrs)))
12
+ end
13
+
14
+ private
15
+
16
+ def map_attributes!(attrs)
17
+ attrs.tap do |h|
18
+ attributes_to_map_by_symbol(attrs).each do |att|
19
+ h[att.name] = h.delete(from(att))
20
+ end
21
+ attributes_to_map_by_call.each do |att|
22
+ h[att.name] = from(att).call(h)
23
+ end
24
+ end
25
+ end
26
+
27
+ def attributes_to_map_by_symbol(attrs)
28
+ attributes_to_map.select do |att|
29
+ !from(att).respond_to?(:call) &&
30
+ !attrs.has_key?(att.name)
31
+ end
32
+ end
33
+
34
+ def attributes_to_map_by_call
35
+ attributes_to_map.select { |att| from(att).respond_to?(:call) }
36
+ end
37
+
38
+ def attributes_to_map
39
+ attribute_set.select { |att| !(from(att).nil?) }
40
+ end
41
+
42
+ def from(attribute)
43
+ attribute.options[:from]
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module Virtus
2
+ module Mapper
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -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,83 @@
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 Person
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
+ end
22
+ end
23
+
24
+ let(:person_id) { 1 }
25
+ let(:first_name) { 'John' }
26
+ let(:last_name) { 'Doe' }
27
+ let(:address) { '1122 Something Avenue' }
28
+ let(:attrs) {
29
+ { person_id: person_id,
30
+ first_name: first_name,
31
+ surname: last_name,
32
+ address: { 'street' => address } }
33
+ }
34
+ let(:mapper) { Examples::Person.new(attrs) }
35
+
36
+ describe 'attribute with from option as symbol' do
37
+ it 'translates key' do
38
+ expect(mapper.last_name).to eq(last_name)
39
+ end
40
+
41
+ it 'does not create method from original key' do
42
+ expect { mapper.surname }.to raise_error(NoMethodError)
43
+ end
44
+
45
+ describe 'with attribute name as key' do
46
+ it 'does not raise error' do
47
+ expect { Examples::Person.new({id: 1}) }.not_to raise_error
48
+ end
49
+
50
+ it 'returns expected value' do
51
+ expect(Examples::Person.new({id: 1}).id).to eq(1)
52
+ end
53
+ end
54
+ end
55
+
56
+ describe 'attribute with from option as callable object' do
57
+ it 'calls the object and passes the attributes hash' do
58
+ callable = Examples::Person.attribute_set[:address].options[:from]
59
+ expect(callable).to receive(:call) { attrs }
60
+ Examples::Person.new(attrs)
61
+ end
62
+
63
+ it 'sets attribute to result of call' do
64
+ expect(mapper.address).to eq(address)
65
+ end
66
+ end
67
+
68
+
69
+ describe 'attribute without from option' do
70
+ it 'behaves as usual' do
71
+ expect(mapper.first_name).to eq(first_name)
72
+ end
73
+ end
74
+
75
+ it 'maps attributes with indifferent access' do
76
+ mapper = Examples::Person.new({ person_id: 1,
77
+ first_name: first_name,
78
+ 'surname' => last_name })
79
+ expect(mapper.last_name).to eq('Doe')
80
+ end
81
+
82
+ end
83
+ 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"
8
+ spec.version = Virtus::Mapper::VERSION
9
+ spec.authors = ["RentPath"]
10
+ spec.email = ["tstankus@rentpath.com", "tcampbell@rentpath.com"]
11
+ spec.summary = %q{Mapper for Virtus attributes}
12
+ spec.description = %q{Mapper for Virtus attributes. See README.}
13
+ spec.homepage = ""
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.1', '>= 4.1.6'
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,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtus-mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - RentPath
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-16 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.1'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 4.1.6
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '4.1'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 4.1.6
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.7'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.7'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '10.0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '10.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.1'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '3.1'
95
+ description: Mapper for Virtus attributes. See README.
96
+ email:
97
+ - tstankus@rentpath.com
98
+ - tcampbell@rentpath.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".DS_Store"
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/virtus/mapper.rb
111
+ - lib/virtus/mapper/version.rb
112
+ - spec/spec_helper.rb
113
+ - spec/virtus/mapper_spec.rb
114
+ - virtus-mapper.gemspec
115
+ homepage: ''
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.2.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Mapper for Virtus attributes
139
+ test_files:
140
+ - spec/spec_helper.rb
141
+ - spec/virtus/mapper_spec.rb