config_mapper 0.1.0

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: 8ed0d0f28834d3999b5b04b181fb26ec7969b325
4
+ data.tar.gz: 293d2c6acc73402a951c53e9a65c65b7171f52d5
5
+ SHA512:
6
+ metadata.gz: e65eabdf9faeb1e2bcc15a4e9f85584da821ccce43f34c1eea852001ee602c9e71c96fbcc11887d509608c4704df0f12391ccf52cb725b284deb6e2a2dac4158
7
+ data.tar.gz: b9d158e005ebcddf4cd6ee5695b950d549acfe4a3e893fed07ebf12272f3d1d37b6c139d3e85e16e0cadfc0f8b0b6fa297238a52e17f744e794d03702a218247
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in config_mapper.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Mike Williams
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # ConfigMapper
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/config_mapper`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'config_mapper'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install config_mapper
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/config_mapper.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'config_mapper/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = "config_mapper"
9
+ spec.version = ConfigMapper::VERSION
10
+
11
+ spec.summary = "Maps config data onto plain old objects"
12
+ spec.license = "MIT"
13
+
14
+ spec.authors = ["Mike Williams"]
15
+ spec.email = ["mdub@dogbiscuit.org"]
16
+ spec.homepage = "https://github.com/mdub/config_mapper"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.test_files = spec.files.grep(%r{^spec/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+
26
+ end
@@ -0,0 +1,76 @@
1
+ # Supports marshalling of plain-old data (e.g. loaded from
2
+ # YAML files) onto strongly-typed objects.
3
+ #
4
+ class ConfigMapper
5
+
6
+ # Attempt to set attributes on a target object.
7
+ #
8
+ # For simple, scalar values, set the attribute by calling the
9
+ # named writer-method on the target object.
10
+ #
11
+ # For Hash values, set attributes of the named sub-component.
12
+ #
13
+ # @return [Hash] exceptions encountered
14
+ #
15
+ def self.set(data, target)
16
+ mapper = new(target)
17
+ mapper.set_attributes(data)
18
+ mapper.errors
19
+ end
20
+
21
+ def initialize(target, errors = {})
22
+ @target = target
23
+ @errors = errors
24
+ end
25
+
26
+ attr_reader :target
27
+ attr_reader :errors
28
+
29
+ # Set multiple attributes from a Hash.
30
+ #
31
+ def set_attributes(data)
32
+ data.each do |key, value|
33
+ set_attribute(key, value)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ # Set a single attribute.
40
+ #
41
+ def set_attribute(key, value)
42
+ if value.is_a?(Hash)
43
+ set_nested(key, value)
44
+ else
45
+ target.public_send("#{key}=", value)
46
+ end
47
+ rescue NoMethodError, ArgumentError => e
48
+ errors[key] = e
49
+ end
50
+
51
+ def set_nested(key, value)
52
+ nested_target = target.public_send(key)
53
+ nested_errors = ErrorProxy.new(errors, "#{key}.")
54
+ nested_mapper = self.class.new(nested_target, nested_errors)
55
+ nested_mapper.set_attributes(value)
56
+ end
57
+
58
+ class ErrorProxy
59
+
60
+ def initialize(errors, prefix)
61
+ @errors = errors
62
+ @prefix = prefix
63
+ end
64
+
65
+ def []=(key, value)
66
+ errors[prefix + key] = value
67
+ end
68
+
69
+ private
70
+
71
+ attr_reader :errors
72
+ attr_reader :prefix
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,3 @@
1
+ class ConfigMapper
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: config_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - mdub@dogbiscuit.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - config_mapper.gemspec
70
+ - lib/config_mapper.rb
71
+ - lib/config_mapper/version.rb
72
+ homepage: https://github.com/mdub/config_mapper
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.8
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Maps config data onto plain old objects
96
+ test_files: []