hashr 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/hashr.rb +13 -12
  2. data/lib/hashr/version.rb +1 -1
  3. metadata +3 -3
data/lib/hashr.rb CHANGED
@@ -14,7 +14,8 @@ class Hashr < Hash
14
14
  end
15
15
 
16
16
  def initialize(data = {})
17
- replace(deep_includize(deep_hasherize(deep_merge(self.class.definition, data))))
17
+ include_modules(data.delete(:_include)) if data
18
+ replace(deep_hasherize(deep_merge(self.class.definition, data)))
18
19
  end
19
20
 
20
21
  def []=(key, value)
@@ -38,6 +39,14 @@ class Hashr < Hash
38
39
 
39
40
  protected
40
41
 
42
+ def include_modules(modules)
43
+ Array(modules).each { |mod| meta_class.send(:include, mod) } if modules
44
+ end
45
+
46
+ def meta_class
47
+ class << self; self end
48
+ end
49
+
41
50
  def deep_merge(left, right)
42
51
  merger = proc { |key, v1, v2| v1.is_a?(Hash) && v2.is_a?(Hash) ? self.class.new(v1.merge(v2, &merger)) : v2 }
43
52
  left.merge(right || {}, &merger)
@@ -45,17 +54,9 @@ class Hashr < Hash
45
54
 
46
55
  def deep_hasherize(hash)
47
56
  hash.inject(TEMPLATE.dup) do |result, (key, value)|
48
- result.merge(key.to_sym => value.is_a?(Hash) ? deep_hasherize(value) : value)
49
- end
50
- end
51
-
52
- def deep_includize(hash)
53
- if modules = hash.delete(:_include)
54
- meta_class = (class << hash; self end)
55
- Array(modules).each { |mod| meta_class.send(:include, mod) }
56
- end
57
- hash.inject(hash) do |hash, (key, value)|
58
- hash.merge(key => value.is_a?(Hash) ? deep_includize(value) : value)
57
+ result[key.to_sym] = value.is_a?(Hash) ? deep_hasherize(value) : value
58
+ result
59
59
  end
60
60
  end
61
61
  end
62
+
data/lib/hashr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Hashr < Hash
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: test_declarative
17
- requirement: &2165879540 !ruby/object:Gem::Requirement
17
+ requirement: &2165867020 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: 0.0.2
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2165879540
25
+ version_requirements: *2165867020
26
26
  description: Simple Hash extension targetted at configuration.
27
27
  email: svenfuchs@artweb-design.de
28
28
  executables: []