whiny_hash 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ angry_hash*.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport"
data/README.textile ADDED
@@ -0,0 +1,40 @@
1
+ h1. WhinyHash
2
+
3
+ WhinyHash is a pretty minimalistic gem that offers a slightly tweaked Hash functionality which is primarily intended for the passing of optional parameters. The behavior differs from normal hashes in the following points:
4
+
5
+ * reading unset keys results in an error instead of nil
6
+ * setting previously unset keys results in an error
7
+ * merging previously unset keys results in an error
8
+
9
+ This doesn't sound like a great improvement, but it simplifies coding quite a lot:
10
+
11
+ <pre>
12
+ DEFAULTS = WhinyHash.new({class: "default_class", id: "default_id", value: 0})
13
+
14
+ def foo(opts={})
15
+ options = DEFAULTS.merge(opts)
16
+ # ...do things...
17
+ options[:bar] # raises an error
18
+ end
19
+
20
+ foo class: 'my_class', value: 23 # fine
21
+ foo clas: 'my_class', value: 23 # raises an error
22
+ </pre>
23
+
24
+ So you quickly discover
25
+
26
+ * usage of undefined options
27
+ * overriding of undefined options
28
+
29
+ Since we firmly believe in 'indifferent access', WhinyHash is derived from ActiveSupport::HashWithIndifferentAccess which is part of <a href="https://github.com/rails/rails/tree/master/activesupport" target="_blank">activesupport</a>.
30
+
31
+ h2. License
32
+
33
+ Copyright (c) 2010-2011 Peter Horn, <a href="http://www.provideal.net" target="_blank">Provideal GmbH</a>
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
36
+
37
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40
+
data/lib/whiny_hash.rb ADDED
@@ -0,0 +1,48 @@
1
+ #--
2
+ # Copyright (c) 2010-2011 Peter Horn, Provideal GmbH
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require "active_support/hash_with_indifferent_access"
25
+
26
+ # We're extending the already dubious semantics of HashWithIndifferentAccess
27
+ # to raise errors if either an unset key is requested, or if a previously
28
+ # unset key is set by a merge.
29
+ #
30
+ # This is used to prevent usage of 'wrong' options.
31
+ class WhinyHash < ActiveSupport::HashWithIndifferentAccess
32
+ def [](key)
33
+ key?(key) ? super(key) : raise("Accessing unset key '#{key}'.")
34
+ end
35
+
36
+ def []=(key, val)
37
+ key?(key) ? super(key, val) : raise("Trying to set previously unset key '#{key}'.")
38
+ end
39
+
40
+ alias_method :merge_lidsa, :merge
41
+ def merge(other)
42
+ other.keys.each do |k|
43
+ raise "Trying to override unset key '#{k}'." unless key?(k)
44
+ end
45
+ WhinyHash.new(merge_lidsa(other))
46
+ end
47
+
48
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "whiny_hash"
6
+ s.version = "0.0.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.summary = "A Hash that complains a lot -- used to simplify optional parameters"
9
+ s.email = "info@provideal.net"
10
+ s.homepage = "http://github.com/privideal/whiny_hash"
11
+ s.description = "A Hash that complains a lot -- used to simplify optional parameters. WhinyHash instances raise errors if unset keys are accessed either directly or by merge."
12
+ s.authors = ['Peter Horn']
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.test_files -= Dir["test/support/country_select/**/*"]
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whiny_hash
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Peter Horn
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-05 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A Hash that complains a lot -- used to simplify optional parameters. WhinyHash instances raise errors if unset keys are accessed either directly or by merge.
22
+ email: info@provideal.net
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - .gitignore
31
+ - Gemfile
32
+ - README.textile
33
+ - lib/whiny_hash.rb
34
+ - whiny_hash.gemspec
35
+ has_rdoc: true
36
+ homepage: http://github.com/privideal/whiny_hash
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.7
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A Hash that complains a lot -- used to simplify optional parameters
67
+ test_files: []
68
+