hashr 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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hashr (0.0.3)
4
+ hashr (0.0.4)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -0,0 +1,23 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) 2011 Sven Fuchs <svenfuchs@artweb-design.de>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
23
+
@@ -0,0 +1,100 @@
1
+ # Hashr
2
+
3
+ Hashr is a very simple and tiny class derived from Ruby's core Hash class which makes using nested hashes for configuration (and other purposes) easier and less repetive and error prone.
4
+
5
+ It supports the following features:
6
+
7
+ * method read and write access
8
+ * automatic predicate (boolean, i.e. ?) methods
9
+ * easy defaults
10
+ * easy inclusion of modules into nested hashes
11
+ * automatic symbolized keys
12
+
13
+ ## Usage
14
+
15
+ Directly use Hashr instances like this:
16
+
17
+ config = Hashr.new('foo' => { 'bar' => 'bar' })
18
+
19
+ config.foo? # => true
20
+ config.foo # => { :bar => 'bar' }
21
+
22
+ config.foo.bar? # => true
23
+ config.foo.bar # => 'bar'
24
+
25
+ config.foo.bar = 'BAR'
26
+ config.foo.bar # => 'BAR'
27
+
28
+ config.foo.baz = 'baz'
29
+ config.foo.baz # => 'baz'
30
+
31
+ Be aware that by default missing keys won't raise a method missing error but instead behave like Hash access:
32
+
33
+ config = Hashr.new
34
+ config.foo? # => false
35
+ config.foo # => nil
36
+
37
+ You can make Hashr to raise an IndexError though like this:
38
+
39
+ Hashr.raise_missing_keys = true
40
+ config = Hashr.new
41
+ config.foo? # => false
42
+ config.foo # => raises an IndexError "Key :foo is not defined."
43
+
44
+ Derive a custom class to define defaults like this:
45
+
46
+ class Config < Hashr
47
+ define :foo => { :bar => 'bar' }
48
+ end
49
+
50
+ config = Config.new
51
+ config.foo.bar # => 'bar'
52
+
53
+ Include modules to nested hashes like this:
54
+
55
+ class Config < Hashr
56
+ module Boxes
57
+ def count
58
+ self['count'] # overwrites a Hash method to return the Hash's content here
59
+ end
60
+
61
+ def names
62
+ @names ||= (1..count).map { |num| "box-#{num}" }
63
+ end
64
+ end
65
+
66
+ define :boxes => { :count => 3, :_include => Boxes }
67
+ end
68
+
69
+ config = Config.new
70
+ config.boxes # => { :count => 3 }
71
+ config.boxes.count # => 3
72
+ config.boxes.names # => ["box-1", "box-2", "box-3"]
73
+
74
+ ## Environment defaults
75
+
76
+ Hashr includes a simple module that makes it easy to overwrite configuration defaults from environment variables:
77
+
78
+ class Config < Hashr
79
+ extend Hashr::EnvDefaults
80
+
81
+ self.env_namespace = 'foo'
82
+
83
+ define :boxes => { :memory => '1024' }
84
+ end
85
+
86
+ Now when an environment variable is defined then it will overwrite the default:
87
+
88
+ ENV['FOO_BOXES_MEMORY'] = '2048'
89
+ config = Config.new
90
+ config.boxes.memory # => '2048'
91
+
92
+ ## Running the tests
93
+
94
+ You can run the tests as follows:
95
+
96
+ # going through bundler
97
+ bundle exec rake
98
+
99
+ # using just ruby
100
+ ruby -rubygems -Ilib:test test/hashr_test.rb
@@ -4,6 +4,8 @@ class Hashr < Hash
4
4
  TEMPLATE = new
5
5
 
6
6
  class << self
7
+ attr_accessor :raise_missing_keys
8
+
7
9
  def define(definition)
8
10
  @definition = definition
9
11
  end
@@ -33,12 +35,17 @@ class Hashr < Hash
33
35
  when '='
34
36
  self[name.to_s[0..-2].to_sym] = args.first
35
37
  else
36
- self[name]
38
+ read(name)
37
39
  end
38
40
  end
39
41
 
40
42
  protected
41
43
 
44
+ def read(key)
45
+ raise(IndexError.new("Key #{key.inspect} is not defined.")) if !key?(key) && self.class.raise_missing_keys
46
+ self[key]
47
+ end
48
+
42
49
  def include_modules(modules)
43
50
  Array(modules).each { |mod| meta_class.send(:include, mod) } if modules
44
51
  end
@@ -1,3 +1,3 @@
1
1
  class Hashr < Hash
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -2,25 +2,37 @@ require 'test_helper'
2
2
 
3
3
  class HashrTest < Test::Unit::TestCase
4
4
  def teardown
5
- ENV.keys.select { |key| key =~ /^WORKER_/ }.each { |key| ENV.delete(key) }
5
+ Hashr.raise_missing_keys = false
6
6
  end
7
7
 
8
8
  test 'method access on an existing key returns the value' do
9
9
  assert_equal 'foo', Hashr.new({ :foo => 'foo' }).foo
10
10
  end
11
11
 
12
- test 'method access on a non-existing key returns nil' do
12
+ test 'method access on a non-existing key returns nil when raise_missing_keys is false' do
13
+ Hashr.raise_missing_keys = false
13
14
  assert_nil Hashr.new({ :foo => 'foo' }).bar
14
15
  end
15
16
 
17
+ test 'method access on a non-existing key raises an IndexError when raise_missing_keys is true' do
18
+ Hashr.raise_missing_keys = true
19
+ assert_raises(IndexError) { Hashr.new({ :foo => 'foo' }).bar }
20
+ end
21
+
16
22
  test 'method access on an existing nested key returns the value' do
17
23
  assert_equal 'bar', Hashr.new({ :foo => { :bar => 'bar' } }).foo.bar
18
24
  end
19
25
 
20
- test 'method access on a non-existing nested key returns nil' do
26
+ test 'method access on a non-existing nested key returns nil when raise_missing_keys is false' do
27
+ Hashr.raise_missing_keys = false
21
28
  assert_nil Hashr.new({ :foo => { :bar => 'bar' } }).foo.baz
22
29
  end
23
30
 
31
+ test 'method access on a non-existing nested key raises an IndexError when raise_missing_keys is true' do
32
+ Hashr.raise_missing_keys = true
33
+ assert_raises(IndexError) { Hashr.new({ :foo => { :bar => 'bar' } }).foo.baz }
34
+ end
35
+
24
36
  test 'method access with a question mark returns true if the key has a value' do
25
37
  assert_equal true, Hashr.new({ :foo => { :bar => 'bar' } }).foo.bar?
26
38
  end
@@ -62,15 +74,23 @@ class HashrTest < Test::Unit::TestCase
62
74
  test 'defaults to env vars' do
63
75
  klass = Class.new(Hashr) do
64
76
  extend Hashr::EnvDefaults
65
- self.env_namespace = 'worker'
77
+ self.env_namespace = 'hashr'
66
78
  define :foo => 'foo', :bar => { :baz => 'baz' }
67
79
  end
68
80
 
69
- ENV['WORKER_FOO'] = 'env foo'
70
- ENV['WORKER_BAR_BAZ'] = 'env bar baz'
81
+ ENV['HASHR_FOO'] = 'env foo'
82
+ ENV['HASHR_BAR_BAZ'] = 'env bar baz'
83
+
84
+ hashr = klass.new
85
+ assert_equal 'env foo', hashr.foo
86
+ assert_equal 'env bar baz', hashr.bar.baz
87
+
88
+ # ENV.delete('HASHR_FOO')
89
+ # ENV.delete('HASHR_BAR_BAZ')
71
90
 
72
- assert_equal 'env foo', klass.new.foo
73
- assert_equal 'env bar baz', klass.new.bar.baz
91
+ # hashr = klass.new
92
+ # assert_equal 'foo', hashr.foo
93
+ # assert_equal 'bar baz', hashr.bar.baz
74
94
  end
75
95
 
76
96
  test 'a key :_include includes the given modules' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Fuchs
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-23 00:00:00 +02:00
18
+ date: 2011-07-24 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -64,8 +64,9 @@ files:
64
64
  - test/test_helper.rb
65
65
  - Gemfile
66
66
  - Gemfile.lock
67
+ - MIT-LICENSE
67
68
  - Rakefile
68
- - README
69
+ - README.md
69
70
  has_rdoc: true
70
71
  homepage: http://github.com/svenfuchs/hashr
71
72
  licenses: []
data/README DELETED
File without changes