config_mapper 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ed0d0f28834d3999b5b04b181fb26ec7969b325
4
- data.tar.gz: 293d2c6acc73402a951c53e9a65c65b7171f52d5
3
+ metadata.gz: 7d643ab8607c785459650363209b078699513bd5
4
+ data.tar.gz: 1a4ef25c93114686800bc0cf82dc13c7736e448a
5
5
  SHA512:
6
- metadata.gz: e65eabdf9faeb1e2bcc15a4e9f85584da821ccce43f34c1eea852001ee602c9e71c96fbcc11887d509608c4704df0f12391ccf52cb725b284deb6e2a2dac4158
7
- data.tar.gz: b9d158e005ebcddf4cd6ee5695b950d549acfe4a3e893fed07ebf12272f3d1d37b6c139d3e85e16e0cadfc0f8b0b6fa297238a52e17f744e794d03702a218247
6
+ metadata.gz: f323e289fbdc9d7c1f6d8f8c436e7d62bb68b98b35268e210a0f4bc0568a6f69549e7c9fb54c38173ce3b84f797c5dba732a7ab80239b0ccaf4d046bbbca5278
7
+ data.tar.gz: 7fcb8808ee09efb2a932ab5b8f4dc74f49405b12438c50428d2506667a7f5862b31b2a771e08964f49ed8f9d8988355748328c407a80eb376f5ff40ec5a19daf
data/.rubocop.yml ADDED
@@ -0,0 +1,51 @@
1
+ Eval:
2
+ Exclude:
3
+ - "Rakefile"
4
+
5
+ Metrics/AbcSize:
6
+ Enabled: false
7
+
8
+ Metrics/LineLength:
9
+ Max: 120
10
+
11
+ Metrics/MethodLength:
12
+ Max: 30
13
+
14
+ Style/AccessorMethodName:
15
+ Enabled: false
16
+
17
+ Style/ClassAndModuleChildren:
18
+ EnforcedStyle: nested
19
+ Exclude:
20
+ - "spec/**/*"
21
+
22
+ Style/Documentation:
23
+ Exclude:
24
+ - "lib/**/version.rb"
25
+ - "spec/**/*"
26
+
27
+ Style/EmptyLinesAroundBlockBody:
28
+ Enabled: false
29
+
30
+ Style/EmptyLinesAroundClassBody:
31
+ EnforcedStyle: empty_lines
32
+
33
+ Style/EmptyLinesAroundModuleBody:
34
+ Enabled: false
35
+
36
+ Style/Encoding:
37
+ EnforcedStyle: when_needed
38
+ Enabled: true
39
+
40
+ Style/FileName:
41
+ Exclude:
42
+ - "bin/*"
43
+
44
+ Style/HashSyntax:
45
+ EnforcedStyle: hash_rockets
46
+
47
+ Style/StringLiterals:
48
+ EnforcedStyle: double_quotes
49
+
50
+ Style/WordArray:
51
+ Enabled: false
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in config_mapper.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -1,41 +1,84 @@
1
1
  # ConfigMapper
2
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.
3
+ ConfigMapper maps configuration data onto Ruby objects.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## Usage
6
6
 
7
- ## Installation
7
+ Imagine you have some Ruby objects:
8
8
 
9
- Add this line to your application's Gemfile:
9
+ class Position
10
10
 
11
- ```ruby
12
- gem 'config_mapper'
13
- ```
11
+ attr_reader :x
12
+ attr_reader :y
14
13
 
15
- And then execute:
14
+ def x=(arg); @x = Integer(arg); end
15
+ def y=(arg); @y = Integer(arg); end
16
16
 
17
- $ bundle
17
+ end
18
18
 
19
- Or install it yourself as:
19
+ class State
20
20
 
21
- $ gem install config_mapper
21
+ def initialize
22
+ @position = Position.new
23
+ end
22
24
 
23
- ## Usage
25
+ attr_reader :position
26
+ attr_accessor :orientation
24
27
 
25
- TODO: Write usage instructions here
28
+ end
26
29
 
27
- ## Development
30
+ state = State.new
28
31
 
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.
32
+ and wish to populate/modify it, based on plain data:
30
33
 
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).
34
+ config_data = {
35
+ "orientation" => "North",
36
+ "position" => {
37
+ "x" => 2,
38
+ "y" => 4
39
+ }
40
+ }
32
41
 
33
- ## Contributing
42
+ ConfigMapper will help you out:
43
+
44
+ require 'config_mapper'
45
+
46
+ errors = ConfigMapper.set(config_data, state)
47
+ state.orientation #=> "North"
48
+ state.position.x #=> 2
49
+
50
+ It can even populate Hashes of objects, e.g.
51
+
52
+ positions = Hash.new { |h,k| h[k] = Position.new }
34
53
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/config_mapper.
54
+ config_data = {
55
+ "fred" => { "x" => 2, "y" => 4 },
56
+ "mary" => { "x" => 3, "y" => 5 }
57
+ }
36
58
 
59
+ ConfigMapper.set(config_data, positions)
60
+ positions["fred"].x #=> 2
61
+ positions["mary"].y #=> 5
62
+
63
+ ### Errors
64
+
65
+ `ConfigMapper.set` returns a Hash of errors encountered while mapping data
66
+ onto objects. The errors are Exceptions (typically ArgumentError or NoMethodError),
67
+ keyed by a dot-delimited path to the offending data. e.g.
68
+
69
+ config_data = {
70
+ "position" => {
71
+ "bogus" => "flibble"
72
+ }
73
+ }
74
+
75
+ errors = ConfigMapper.set(config_data, state)
76
+ errors #=> { "position.bogus" => #<NoMethodError> }
37
77
 
38
78
  ## License
39
79
 
40
80
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
81
 
82
+ ## Contributing
83
+
84
+ It's on GitHub; you know the drill.
@@ -1,7 +1,6 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("../lib", __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'config_mapper/version'
3
+ require "config_mapper/version"
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
 
@@ -1,3 +1,5 @@
1
1
  class ConfigMapper
2
- VERSION = "0.1.0"
2
+
3
+ VERSION = "0.2.0"
4
+
3
5
  end
data/lib/config_mapper.rb CHANGED
@@ -19,7 +19,7 @@ class ConfigMapper
19
19
  end
20
20
 
21
21
  def initialize(target, errors = {})
22
- @target = target
22
+ @target = ObjectAsHash[target]
23
23
  @errors = errors
24
24
  end
25
25
 
@@ -39,22 +39,43 @@ class ConfigMapper
39
39
  # Set a single attribute.
40
40
  #
41
41
  def set_attribute(key, value)
42
- if value.is_a?(Hash)
43
- set_nested(key, value)
42
+ if value.is_a?(Hash) && !target[key].nil?
43
+ nested_errors = ErrorProxy.new(errors, "#{key}.")
44
+ nested_mapper = self.class.new(target[key], nested_errors)
45
+ nested_mapper.set_attributes(value)
44
46
  else
45
- target.public_send("#{key}=", value)
47
+ target[key] = value
46
48
  end
47
49
  rescue NoMethodError, ArgumentError => e
48
50
  errors[key] = e
49
51
  end
50
52
 
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)
53
+ class ObjectAsHash
54
+
55
+ def self.[](target)
56
+ if target.is_a?(Hash)
57
+ target
58
+ else
59
+ ObjectAsHash.new(target)
60
+ end
61
+ end
62
+
63
+ def initialize(target)
64
+ @target = target
65
+ end
66
+
67
+ def [](key)
68
+ @target.public_send(key)
69
+ end
70
+
71
+ def []=(key, value)
72
+ @target.public_send("#{key}=", value)
73
+ end
74
+
56
75
  end
57
76
 
77
+ # Wraps a Hash of errors, injecting prefixes
78
+ #
58
79
  class ErrorProxy
59
80
 
60
81
  def initialize(errors, prefix)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-03 00:00:00.000000000 Z
11
+ date: 2015-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,6 +61,7 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
+ - ".rubocop.yml"
64
65
  - ".travis.yml"
65
66
  - Gemfile
66
67
  - LICENSE.txt
@@ -89,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
90
  version: '0'
90
91
  requirements: []
91
92
  rubyforge_project:
92
- rubygems_version: 2.4.8
93
+ rubygems_version: 2.4.5
93
94
  signing_key:
94
95
  specification_version: 4
95
96
  summary: Maps config data onto plain old objects