hashr 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/hashr.rb +14 -2
- data/lib/hashr/version.rb +1 -1
- data/test/hashr_test.rb +8 -0
- metadata +6 -6
data/Gemfile.lock
CHANGED
data/lib/hashr.rb
CHANGED
@@ -9,7 +9,7 @@ class Hashr < Hash
|
|
9
9
|
attr_accessor :raise_missing_keys
|
10
10
|
|
11
11
|
def define(definition)
|
12
|
-
@definition = definition.deep_symbolize_keys
|
12
|
+
@definition = deep_accessorize(definition.deep_symbolize_keys)
|
13
13
|
end
|
14
14
|
|
15
15
|
def definition
|
@@ -17,12 +17,22 @@ class Hashr < Hash
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def default(defaults)
|
20
|
-
@defaults = defaults
|
20
|
+
@defaults = deep_accessorize(defaults)
|
21
21
|
end
|
22
22
|
|
23
23
|
def defaults
|
24
24
|
@defaults ||= {}
|
25
25
|
end
|
26
|
+
|
27
|
+
def deep_accessorize(hash)
|
28
|
+
hash.each do |key, value|
|
29
|
+
next unless value.is_a?(Hash)
|
30
|
+
value[:_access] ||= []
|
31
|
+
value[:_access] = Array(value[:_access])
|
32
|
+
value.keys.each { |key| value[:_access] << key if value.respond_to?(key) }
|
33
|
+
deep_accessorize(value)
|
34
|
+
end
|
35
|
+
end
|
26
36
|
end
|
27
37
|
|
28
38
|
def initialize(data = {}, definition = self.class.definition, &block)
|
@@ -92,9 +102,11 @@ class Hashr < Hash
|
|
92
102
|
hash.include_accessors(value)
|
93
103
|
end
|
94
104
|
end
|
105
|
+
|
95
106
|
hash.each do |key, value|
|
96
107
|
deep_defaultize(value) if value.is_a?(Hash)
|
97
108
|
end
|
109
|
+
|
98
110
|
hash
|
99
111
|
end
|
100
112
|
end
|
data/lib/hashr/version.rb
CHANGED
data/test/hashr_test.rb
CHANGED
@@ -146,6 +146,14 @@ class HashrTest < Test::Unit::TestCase
|
|
146
146
|
assert_equal 'key', klass.new(:foo => { :key => 'key' }).foo.key
|
147
147
|
end
|
148
148
|
|
149
|
+
test 'defining defaults always also makes sure an accessor is used' do
|
150
|
+
klass = Class.new(Hashr) do
|
151
|
+
define :foo => { :default => 'default' }
|
152
|
+
end
|
153
|
+
|
154
|
+
assert_equal 'default', klass.new().foo.default
|
155
|
+
end
|
156
|
+
|
149
157
|
test 'all: allows to define :_include modules which will be included into all nested hashes' do
|
150
158
|
klass = Class.new(Hashr) do
|
151
159
|
default :_include => Module.new { def helper; 'helper'; 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.
|
4
|
+
version: 0.0.15
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-09-15 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70171732685660 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70171732685660
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: test_declarative
|
27
|
-
requirement: &
|
27
|
+
requirement: &70171732685160 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 0.0.2
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70171732685160
|
36
36
|
description: Simple Hash extension to make working with nested hashes (e.g. for configuration)
|
37
37
|
easier and less error-prone.
|
38
38
|
email: svenfuchs@artweb-design.de
|