indifferent-variable-hash 0.1.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.
Files changed (2) hide show
  1. data/lib/indifferent-variable-hash.rb +49 -0
  2. metadata +55 -0
@@ -0,0 +1,49 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ module IndifferentVariableHash
4
+ attr_accessor :variables
5
+
6
+ def variables
7
+ @variables ||= IndifferentVariableHash::Hash.new
8
+ end
9
+
10
+ def method_missing name, *args
11
+ begin
12
+ self.variables.send name, *args
13
+ rescue Exception => ex
14
+ super
15
+ end
16
+ end
17
+
18
+ # IndifferentVariableHash::Hash extends Hash to be 'indifferent'
19
+ #
20
+ # 'Indifferent' has a few different meanings and needs to be better defined.
21
+ #
22
+ # This is custom in 2 major ways:
23
+ # * All keys will be stringified (so foo[:hi] and foo['hi'] are the same)
24
+ # * Sending messages are translated into keys, eg. foo.hi returns foo['hi']
25
+ #
26
+ class Hash < ::Hash
27
+
28
+ def []= key, value
29
+ super key.to_s, value
30
+ end
31
+
32
+ def [] key
33
+ super key.to_s
34
+ end
35
+
36
+ def method_missing name, *args
37
+ if name.to_s =~ /=$/ # eg. @hash.foo = 'bar'
38
+ self[$`.to_s] = args.first
39
+ else
40
+ if args.empty?
41
+ self[name.to_s] # eg. @hash.foo
42
+ else
43
+ super # anything else ... fall back to super
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: indifferent-variable-hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - remi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Easily add Hash/OpenStruct-like features to any class or instance
17
+ email: remi@remitaylor.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/indifferent-variable-hash.rb
26
+ has_rdoc: true
27
+ homepage: http://github.com/remi/indifferent-variable-hash
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Easily add Hash-like features to anything
54
+ test_files: []
55
+