dotconfig 0.0.4 → 0.0.5
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.
- data/CHANGELOG.md +5 -0
- data/lib/dot_config/configuration.rb +22 -13
- data/lib/dot_config/version.rb +1 -1
- data/test/dot_config/configuration_test.rb +6 -0
- metadata +2 -2
data/CHANGELOG.md
CHANGED
@@ -40,21 +40,15 @@ module DotConfig
|
|
40
40
|
# @return [Hash] The +Hash+ configuration.
|
41
41
|
def config=(hash)
|
42
42
|
raise ArgumentError, 'Invalid argument' if !hash.is_a?(Hash) || hash.empty?
|
43
|
+
raise "Some keys are already used by Ruby" if collision_name?(hash.keys)
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
hash.each do |key, value|
|
50
|
-
if value.is_a?(Hash)
|
51
|
-
@config[key.to_sym] = self.class.new(config: value, writing: @writing)
|
52
|
-
else
|
53
|
-
@config[key.to_sym] = value
|
54
|
-
end
|
45
|
+
@config = hash.reduce(Hash.new) do |config, (key, value)|
|
46
|
+
config[key.to_sym] = if value.is_a?(Hash)
|
47
|
+
self.class.new(config: value, writing: @writing)
|
48
|
+
else
|
49
|
+
value
|
55
50
|
end
|
56
|
-
|
57
|
-
raise "The key(s) (#{conflict.join(', ')}) is already used by Ruby"
|
51
|
+
config
|
58
52
|
end
|
59
53
|
end
|
60
54
|
|
@@ -85,5 +79,20 @@ module DotConfig
|
|
85
79
|
@config.each { |k, v| hash[k] = v.is_a?(self.class) ? v.to_hash : v }
|
86
80
|
hash
|
87
81
|
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
# Checks if the keys duplicate the name of the instance methods.
|
86
|
+
#
|
87
|
+
# @param [Array] keys The keys of the configuration.
|
88
|
+
# @return [Boolean] +true+ if some keys match with the instance methods
|
89
|
+
# name, otherwise +false+.
|
90
|
+
def collision_name?(keys)
|
91
|
+
# Retrieves all names of the instance methods.
|
92
|
+
methods = self.class.instance_methods(true)
|
93
|
+
keys.reduce(false) do |value, key|
|
94
|
+
value || methods.include?(key.to_sym) || methods.include?(:"#{key}=")
|
95
|
+
end
|
96
|
+
end
|
88
97
|
end
|
89
98
|
end
|
data/lib/dot_config/version.rb
CHANGED
@@ -82,4 +82,10 @@ class DotConfig::ConfigurationTest < ActiveSupport::TestCase
|
|
82
82
|
config = Configuration.new(config: nested_hash)
|
83
83
|
assert_equal config.to_hash, nested_hash
|
84
84
|
end
|
85
|
+
|
86
|
+
test 'private collision_name? method' do
|
87
|
+
config = Configuration.new
|
88
|
+
assert config.send(:collision_name?, %w{config})
|
89
|
+
assert !config.send(:collision_name?, %w{world})
|
90
|
+
end
|
85
91
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dotconfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|