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 +4 -4
- data/.rubocop.yml +51 -0
- data/Gemfile +1 -1
- data/README.md +61 -18
- data/config_mapper.gemspec +2 -3
- data/lib/config_mapper/version.rb +3 -1
- data/lib/config_mapper.rb +30 -9
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d643ab8607c785459650363209b078699513bd5
|
4
|
+
data.tar.gz: 1a4ef25c93114686800bc0cf82dc13c7736e448a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/README.md
CHANGED
@@ -1,41 +1,84 @@
|
|
1
1
|
# ConfigMapper
|
2
2
|
|
3
|
-
|
3
|
+
ConfigMapper maps configuration data onto Ruby objects.
|
4
4
|
|
5
|
-
|
5
|
+
## Usage
|
6
6
|
|
7
|
-
|
7
|
+
Imagine you have some Ruby objects:
|
8
8
|
|
9
|
-
|
9
|
+
class Position
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
```
|
11
|
+
attr_reader :x
|
12
|
+
attr_reader :y
|
14
13
|
|
15
|
-
|
14
|
+
def x=(arg); @x = Integer(arg); end
|
15
|
+
def y=(arg); @y = Integer(arg); end
|
16
16
|
|
17
|
-
|
17
|
+
end
|
18
18
|
|
19
|
-
|
19
|
+
class State
|
20
20
|
|
21
|
-
|
21
|
+
def initialize
|
22
|
+
@position = Position.new
|
23
|
+
end
|
22
24
|
|
23
|
-
|
25
|
+
attr_reader :position
|
26
|
+
attr_accessor :orientation
|
24
27
|
|
25
|
-
|
28
|
+
end
|
26
29
|
|
27
|
-
|
30
|
+
state = State.new
|
28
31
|
|
29
|
-
|
32
|
+
and wish to populate/modify it, based on plain data:
|
30
33
|
|
31
|
-
|
34
|
+
config_data = {
|
35
|
+
"orientation" => "North",
|
36
|
+
"position" => {
|
37
|
+
"x" => 2,
|
38
|
+
"y" => 4
|
39
|
+
}
|
40
|
+
}
|
32
41
|
|
33
|
-
|
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
|
-
|
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.
|
data/config_mapper.gemspec
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
|
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
|
3
|
+
require "config_mapper/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
6
|
|
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
|
-
|
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
|
47
|
+
target[key] = value
|
46
48
|
end
|
47
49
|
rescue NoMethodError, ArgumentError => e
|
48
50
|
errors[key] = e
|
49
51
|
end
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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.
|
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-
|
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.
|
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
|