dotconfig 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,3 +14,8 @@
14
14
  ## Version 0.0.4
15
15
 
16
16
  * Adding The RubyDoc link
17
+
18
+ ## Version 0.0.5
19
+
20
+ * Simplification of the DotConfig::Configuration#config= in using the Hash#reduce method
21
+ * Adding a private method to detect the collision name
@@ -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
- keys = (hash.keys + hash.keys.map { |key| "#{key}=" }).map(&:to_sym)
45
- system = self.instance_variables + self.class.instance_methods(true)
46
-
47
- if (conflict = keys & system).empty?
48
- @config = Hash.new
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
- else
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
@@ -12,7 +12,7 @@ module DotConfig
12
12
  # The minor number version.
13
13
  MINOR = 0
14
14
  # The tiny number version.
15
- TINY = 4
15
+ TINY = 5
16
16
  # The pre realese number version.
17
17
  PRE = nil
18
18
  # The complete number version.
@@ -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
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-26 00:00:00.000000000 Z
12
+ date: 2012-12-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport