hashr 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hashr (0.0.9)
4
+ hashr (0.0.12)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/README.md CHANGED
@@ -94,6 +94,26 @@ Include modules to nested hashes like this:
94
94
  config.boxes.count # => 3
95
95
  config.boxes.names # => ["box-1", "box-2", "box-3"]
96
96
 
97
+ As overwriting Hash methods for method access to keys is a common pattern there's a short cut to it:
98
+
99
+ class Config < Hashr
100
+ define :_access => [:count, :key]
101
+ end
102
+
103
+ config = Config.new(:count => 3, :key => 'key')
104
+ config.count # => 3
105
+ config.key # => 'key'
106
+
107
+ Both `:_include` and `:_access` can be defined as defaults, i.e. so that they will be used on all nested hashes:
108
+
109
+ class Config < Hashr
110
+ default :_access => :key
111
+ end
112
+
113
+ config = Config.new(:key => 'key', :foo => { :key => 'foo.key' })
114
+ config.key # => 'key'
115
+ config.foo.key # => 'foo.key'
116
+
97
117
  ## Environment defaults
98
118
 
99
119
  Hashr includes a simple module that makes it easy to overwrite configuration defaults from environment variables:
data/lib/hashr.rb CHANGED
@@ -15,10 +15,19 @@ class Hashr < Hash
15
15
  def definition
16
16
  @definition ||= {}
17
17
  end
18
+
19
+ def default(defaults)
20
+ @defaults = defaults
21
+ end
22
+
23
+ def defaults
24
+ @defaults ||= {}
25
+ end
18
26
  end
19
27
 
20
28
  def initialize(data = {}, definition = self.class.definition, &block)
21
- replace(deep_hashrize(definition.deep_merge((data || {}).deep_symbolize_keys)))
29
+ replace((deep_hashrize(definition.deep_merge((data || {}).deep_symbolize_keys))))
30
+ deep_defaultize(self)
22
31
  (class << self; self; end).class_eval(&block) if block_given?
23
32
  end
24
33
 
@@ -46,6 +55,10 @@ class Hashr < Hash
46
55
  Array(modules).each { |mod| meta_class.send(:include, mod) } if modules
47
56
  end
48
57
 
58
+ def include_accessors(accessors)
59
+ Array(accessors).each { |accessor| meta_class.send(:define_method, accessor) { self[accessor] } } if accessors
60
+ end
61
+
49
62
  def meta_class
50
63
  class << self; self end
51
64
  end
@@ -54,13 +67,31 @@ class Hashr < Hash
54
67
 
55
68
  def deep_hashrize(hash)
56
69
  hash.inject(TEMPLATE.dup) do |result, (key, value)|
57
- if key.to_sym == :_include
70
+ case key.to_sym
71
+ when :_include
58
72
  result.include_modules(value)
73
+ when :_access
74
+ result.include_accessors(value)
59
75
  else
60
76
  result.store(key.to_sym, value.is_a?(Hash) ? deep_hashrize(value) : value)
61
77
  end
62
78
  result
63
79
  end
64
80
  end
81
+
82
+ def deep_defaultize(hash)
83
+ self.class.defaults.each do |key, value|
84
+ case key.to_sym
85
+ when :_include
86
+ hash.include_modules(value)
87
+ when :_access
88
+ hash.include_accessors(value)
89
+ end
90
+ end
91
+ hash.each do |key, value|
92
+ deep_defaultize(value) if value.is_a?(Hash)
93
+ end
94
+ hash
95
+ end
65
96
  end
66
97
 
data/lib/hashr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Hashr < Hash
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
data/test/hashr_test.rb CHANGED
@@ -130,6 +130,30 @@ class HashrTest < Test::Unit::TestCase
130
130
  assert_equal 'helper', klass.new.foo.helper
131
131
  end
132
132
 
133
+ test 'a key :_access includes an anonymous module with accessors' do
134
+ klass = Class.new(Hashr) do
135
+ define :foo => { :_access => [:key] }
136
+ end
137
+
138
+ assert_equal 'key', klass.new(:foo => { :key => 'key' }).foo.key
139
+ end
140
+
141
+ test 'all: allows to define :_include modules which will be included into all nested hashes' do
142
+ klass = Class.new(Hashr) do
143
+ default :_include => Module.new { def helper; 'helper'; end }
144
+ end
145
+ assert_equal 'helper', klass.new.helper
146
+ assert_equal 'helper', klass.new(:foo => { :bar => {} }).foo.bar.helper
147
+ end
148
+
149
+ test 'all: allows to define :_access which will include an anonymous module with accessors into all nested hashes' do
150
+ klass = Class.new(Hashr) do
151
+ default :_access => :key
152
+ end
153
+ assert_equal 'key', klass.new(:key => 'key').key
154
+ assert_equal 'key', klass.new(:foo => { :bar => { :key => 'key' } }).foo.bar.key
155
+ end
156
+
133
157
  test 'anonymously overwriting core Hash methods' do
134
158
  hashr = Hashr.new(:count => 5) do
135
159
  def count
metadata CHANGED
@@ -1,61 +1,45 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: hashr
3
- version: !ruby/object:Gem::Version
4
- hash: 9
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.12
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 11
10
- version: 0.0.11
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Sven Fuchs
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-08-08 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-08-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rake
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70320929512120 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :development
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: test_declarative
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70320929512120
25
+ - !ruby/object:Gem::Dependency
26
+ name: test_declarative
27
+ requirement: &70320929511400 !ruby/object:Gem::Requirement
38
28
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 27
43
- segments:
44
- - 0
45
- - 0
46
- - 2
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
47
32
  version: 0.0.2
48
33
  type: :development
49
- version_requirements: *id002
50
- description: Simple Hash extension to make working with nested hashes (e.g. for configuration) easier and less error-prone.
34
+ prerelease: false
35
+ version_requirements: *70320929511400
36
+ description: Simple Hash extension to make working with nested hashes (e.g. for configuration)
37
+ easier and less error-prone.
51
38
  email: svenfuchs@artweb-design.de
52
39
  executables: []
53
-
54
40
  extensions: []
55
-
56
41
  extra_rdoc_files: []
57
-
58
- files:
42
+ files:
59
43
  - lib/hashr/core_ext/ruby/hash.rb
60
44
  - lib/hashr/env_defaults.rb
61
45
  - lib/hashr/version.rb
@@ -69,36 +53,27 @@ files:
69
53
  - README.md
70
54
  homepage: http://github.com/svenfuchs/hashr
71
55
  licenses: []
72
-
73
56
  post_install_message:
74
57
  rdoc_options: []
75
-
76
- require_paths:
58
+ require_paths:
77
59
  - lib
78
- required_ruby_version: !ruby/object:Gem::Requirement
60
+ required_ruby_version: !ruby/object:Gem::Requirement
79
61
  none: false
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- hash: 3
84
- segments:
85
- - 0
86
- version: "0"
87
- required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
67
  none: false
89
- requirements:
90
- - - ">="
91
- - !ruby/object:Gem::Version
92
- hash: 3
93
- segments:
94
- - 0
95
- version: "0"
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
96
72
  requirements: []
97
-
98
- rubyforge_project: "[none]"
73
+ rubyforge_project: ! '[none]'
99
74
  rubygems_version: 1.8.6
100
75
  signing_key:
101
76
  specification_version: 3
102
- summary: Simple Hash extension to make working with nested hashes (e.g. for configuration) easier and less error-prone
77
+ summary: Simple Hash extension to make working with nested hashes (e.g. for configuration)
78
+ easier and less error-prone
103
79
  test_files: []
104
-