purple_hash 1.0.0
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.
- data/Gemfile +3 -0
- data/Gemfile.lock +27 -0
- data/LICENSE +20 -0
- data/README.md +50 -0
- data/Rakefile +9 -0
- data/lib/purple_hash.rb +56 -0
- data/spec/purple_hash_spec.rb +64 -0
- data/spec/spec_helper.rb +2 -0
- metadata +104 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
purple_hash (1.0.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: http://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
diff-lcs (1.1.3)
|
|
10
|
+
rake (0.9.2.2)
|
|
11
|
+
rspec (2.10.0)
|
|
12
|
+
rspec-core (~> 2.10.0)
|
|
13
|
+
rspec-expectations (~> 2.10.0)
|
|
14
|
+
rspec-mocks (~> 2.10.0)
|
|
15
|
+
rspec-core (2.10.0)
|
|
16
|
+
rspec-expectations (2.10.0)
|
|
17
|
+
diff-lcs (~> 1.1.3)
|
|
18
|
+
rspec-mocks (2.10.1)
|
|
19
|
+
|
|
20
|
+
PLATFORMS
|
|
21
|
+
ruby
|
|
22
|
+
|
|
23
|
+
DEPENDENCIES
|
|
24
|
+
bundler
|
|
25
|
+
purple_hash!
|
|
26
|
+
rake
|
|
27
|
+
rspec
|
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2012 James Logsdon
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Purple Hash is a configuration library for ruby that builds upon Ruby's core
|
|
2
|
+
Hash. At it's core Purple allows you to access keys that do not exist yet by
|
|
3
|
+
defining a default value proc which provides you with a new instance of a
|
|
4
|
+
Purple Hash. Keys are also normalized to symbols, similar to how Rail's
|
|
5
|
+
`HashWithIndifferentAccess` works.
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
require 'purple_hash'
|
|
9
|
+
|
|
10
|
+
config = PurpleHash.new
|
|
11
|
+
config[:db]['user'] = 'root'
|
|
12
|
+
config # => {:db => {:user => 'root'}}
|
|
13
|
+
|
|
14
|
+
config = PurpleHash.new(:db => {'user' => 'root'})
|
|
15
|
+
config # => {:db => {:user => 'root'}}
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Because a `PurpleHash` is simply a sub-class of `Hash` you can do anything you
|
|
19
|
+
would normally be able to without any hacks or tricks. Though not provided by
|
|
20
|
+
this library, you could extend `Hash` to provide a `#to_purple_hash` like so:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
class Hash
|
|
24
|
+
def to_purple_hash
|
|
25
|
+
PurpleHash.new(self)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
**Note:** If you try to change the default value on a `PurpleHash` a `RuntimeError`
|
|
31
|
+
will be raised informing you that this is not allowed.
|
|
32
|
+
|
|
33
|
+
## API
|
|
34
|
+
|
|
35
|
+
PurpleHash provides a very basic API on top of what Hash already provides.
|
|
36
|
+
|
|
37
|
+
### `#configure_from_hash(hash)`
|
|
38
|
+
|
|
39
|
+
Takes a raw `Hash` and converts itself and any nested hashes to a `PurpleHash`.
|
|
40
|
+
Used internally by `#initialize`
|
|
41
|
+
|
|
42
|
+
### `#freeze!`
|
|
43
|
+
|
|
44
|
+
Locks the hash from any futher modifications. A `PurpleHash::HashFrozen` will be
|
|
45
|
+
raised if the hash is frozen and you attempt to modify it. Keys that are not
|
|
46
|
+
already defined will return `nil` instead of a new `PurpleHash`.
|
|
47
|
+
|
|
48
|
+
### `#frozen?`
|
|
49
|
+
|
|
50
|
+
Find out if the hash is frozen.
|
data/Rakefile
ADDED
data/lib/purple_hash.rb
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
class PurpleHash < ::Hash
|
|
2
|
+
class HashFrozen < RuntimeError
|
|
3
|
+
def initialize
|
|
4
|
+
super('this PurpleHash is frozen')
|
|
5
|
+
end
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def initialize(hash={})
|
|
9
|
+
configure_from_hash(hash)
|
|
10
|
+
@__frozen = false
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def freeze!
|
|
14
|
+
self.each do |k, h|
|
|
15
|
+
h.freeze! if h.respond_to?(:freeze!)
|
|
16
|
+
end
|
|
17
|
+
@__frozen = true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def frozen?
|
|
21
|
+
@__frozen === true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def configure_from_hash(hash)
|
|
25
|
+
hash.each do |key, value|
|
|
26
|
+
if value.is_a?(Hash)
|
|
27
|
+
self[key].configure_from_hash(value)
|
|
28
|
+
else
|
|
29
|
+
self[key] = value
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def [](key)
|
|
35
|
+
super(key.to_sym)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def []=(key, value)
|
|
39
|
+
raise HashFrozen if frozen?
|
|
40
|
+
|
|
41
|
+
if value.is_a?(Hash) && !value.is_a?(self.class)
|
|
42
|
+
self[key].configure_from_hash(value)
|
|
43
|
+
else
|
|
44
|
+
super(key.to_sym, value)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def default(key)
|
|
49
|
+
return nil if frozen?
|
|
50
|
+
self[key] = PurpleHash.new
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def default=(*args)
|
|
54
|
+
raise RuntimeError, "#{self.class} does not allow changing the default hash value"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe PurpleHash do
|
|
4
|
+
subject { described_class.new(foo: {bar: :baz}) }
|
|
5
|
+
|
|
6
|
+
it { should be_a(Hash) }
|
|
7
|
+
|
|
8
|
+
describe '#[]' do
|
|
9
|
+
it 'creates keys on the fly' do
|
|
10
|
+
subject[:foo]
|
|
11
|
+
subject.should have_key(:foo)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'uses our Configuration class when creating keys' do
|
|
15
|
+
subject[:foo].should be_a(described_class)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'normalizes keys to symbols' do
|
|
19
|
+
subject['foo']
|
|
20
|
+
subject.should have_key(:foo)
|
|
21
|
+
subject.should_not have_key('foo')
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
describe '#[]=' do
|
|
26
|
+
it 'normalizes keys to symbols' do
|
|
27
|
+
subject['foo'] = :bar
|
|
28
|
+
subject.should have_key(:foo)
|
|
29
|
+
subject.should_not have_key('foo')
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context 'when a hash is passed as the value' do
|
|
33
|
+
it 'should be converted to a Configuration hash' do
|
|
34
|
+
subject[:foo] = {bar: :baz}
|
|
35
|
+
subject[:foo].should be_a(described_class)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe '#default=' do
|
|
41
|
+
it 'raises an error' do
|
|
42
|
+
expect { subject.default = proc { } }.to raise_error
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe '#freeze!' do
|
|
47
|
+
before(:each) do
|
|
48
|
+
subject.freeze!
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'blocks setting of new values' do
|
|
52
|
+
expect { subject[:foo] = :bar }.to raise_error
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'does not create keys on the fly' do
|
|
56
|
+
subject[:bar]
|
|
57
|
+
subject.should_not have_key(:bar)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it 'blocks the setting of new values on nested hashes' do
|
|
61
|
+
expect { subject[:foo][:bar] = :break }.to raise_error(PurpleHash::HashFrozen)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: purple_hash
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- James Logsdon
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-03-25 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: bundler
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '0'
|
|
30
|
+
- !ruby/object:Gem::Dependency
|
|
31
|
+
name: rake
|
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
|
33
|
+
none: false
|
|
34
|
+
requirements:
|
|
35
|
+
- - ! '>='
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
type: :development
|
|
39
|
+
prerelease: false
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
none: false
|
|
42
|
+
requirements:
|
|
43
|
+
- - ! '>='
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: rspec
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ! '>='
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
type: :development
|
|
55
|
+
prerelease: false
|
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
57
|
+
none: false
|
|
58
|
+
requirements:
|
|
59
|
+
- - ! '>='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
description:
|
|
63
|
+
email: dwarf@girsbrain.org
|
|
64
|
+
executables: []
|
|
65
|
+
extensions: []
|
|
66
|
+
extra_rdoc_files: []
|
|
67
|
+
files:
|
|
68
|
+
- Gemfile
|
|
69
|
+
- Gemfile.lock
|
|
70
|
+
- lib/purple_hash.rb
|
|
71
|
+
- LICENSE
|
|
72
|
+
- Rakefile
|
|
73
|
+
- README.md
|
|
74
|
+
- spec/purple_hash_spec.rb
|
|
75
|
+
- spec/spec_helper.rb
|
|
76
|
+
homepage: http://github.com/jlogsdon/purple_hash
|
|
77
|
+
licenses:
|
|
78
|
+
- MIT
|
|
79
|
+
post_install_message:
|
|
80
|
+
rdoc_options: []
|
|
81
|
+
require_paths:
|
|
82
|
+
- lib
|
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
|
+
none: false
|
|
85
|
+
requirements:
|
|
86
|
+
- - ! '>='
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
|
+
none: false
|
|
91
|
+
requirements:
|
|
92
|
+
- - ! '>='
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
requirements: []
|
|
96
|
+
rubyforge_project:
|
|
97
|
+
rubygems_version: 1.8.24
|
|
98
|
+
signing_key:
|
|
99
|
+
specification_version: 3
|
|
100
|
+
summary: A lightweight Configuration gem built with Hashes
|
|
101
|
+
test_files:
|
|
102
|
+
- spec/purple_hash_spec.rb
|
|
103
|
+
- spec/spec_helper.rb
|
|
104
|
+
has_rdoc:
|