hipsterhash 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/hipsterhash.gemspec +1 -1
- data/lib/hipster_hash.rb +41 -4
- data/test/hipster_hash_test.rb +24 -1
- metadata +2 -2
data/hipsterhash.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |gem|
|
6
6
|
gem.name = "hipsterhash"
|
7
|
-
gem.version = "0.0.
|
7
|
+
gem.version = "0.0.2"
|
8
8
|
gem.authors = ["Bryan JJ Buckley"]
|
9
9
|
gem.email = ["jjbuckley@gmail.com"]
|
10
10
|
gem.description = %q{A hash which is all, like, whatever.}
|
data/lib/hipster_hash.rb
CHANGED
@@ -1,17 +1,54 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
|
-
require 'active_support/hash_with_indifferent_access'
|
3
2
|
require 'active_support/core_ext/string/inflections'
|
4
3
|
|
5
4
|
# A HipsterHash is like a regular Hash, except the keys are semi case
|
6
5
|
# insensitive, and symbols and strings are equivalent. Therefore, :foo_bar,
|
7
6
|
# :FooBar, and "foo_bar" are considered to be the same key.
|
8
|
-
class HipsterHash <
|
9
|
-
def
|
10
|
-
|
7
|
+
class HipsterHash < Hash
|
8
|
+
def initialize(initial = {}, &block)
|
9
|
+
case initial
|
10
|
+
when Hash
|
11
|
+
super(&block)
|
12
|
+
initial.each { |k, v| self[k] = v }
|
13
|
+
else
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def [](key)
|
19
|
+
convert_value(super(convert_key(key)))
|
20
|
+
end
|
21
|
+
|
22
|
+
def []=(key, value)
|
23
|
+
super(convert_key(key), value)
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(sym, *args, &block)
|
27
|
+
if args.empty? and !block_given?
|
28
|
+
self[sym] || super
|
29
|
+
else
|
30
|
+
super
|
31
|
+
end
|
11
32
|
end
|
12
33
|
|
13
34
|
protected
|
14
35
|
def convert_key(k)
|
36
|
+
self.class.convert_key(k)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.convert_key(k)
|
15
40
|
k.to_s.underscore.to_sym
|
16
41
|
end
|
42
|
+
|
43
|
+
def self.convert_value(arg)
|
44
|
+
case arg
|
45
|
+
when Hash then new(arg)
|
46
|
+
when Array then arg.map { |a| convert_value(a) }
|
47
|
+
else arg
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def convert_value(arg)
|
52
|
+
self.class.convert_value(arg)
|
53
|
+
end
|
17
54
|
end
|
data/test/hipster_hash_test.rb
CHANGED
@@ -12,12 +12,18 @@ class HipsterHashTest < Test::Unit::TestCase
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_indifference_to_input_type
|
15
|
-
hh = HipsterHash.new(
|
15
|
+
hh = HipsterHash.new(:foo => "bar")
|
16
16
|
assert_equal "bar", hh["foo"]
|
17
17
|
hh['foo'] = "baz"
|
18
18
|
assert_equal "baz", hh[:foo]
|
19
19
|
end
|
20
20
|
|
21
|
+
def test_nesting
|
22
|
+
hh = HipsterHash.new(:foo => { :bar => :baz })
|
23
|
+
assert_kind_of HipsterHash, hh[:foo]
|
24
|
+
assert_equal :baz, hh["Foo"][:Bar]
|
25
|
+
end
|
26
|
+
|
21
27
|
def test_case_indifference
|
22
28
|
hh = HipsterHash.new
|
23
29
|
hh[:foo_bar] = "bar"
|
@@ -25,4 +31,21 @@ class HipsterHashTest < Test::Unit::TestCase
|
|
25
31
|
hh['FooBar'] = "baz"
|
26
32
|
assert_equal "baz", hh[:foo_bar]
|
27
33
|
end
|
34
|
+
|
35
|
+
def test_creation_with_a_hash
|
36
|
+
hh = HipsterHash.new(:foo_bar => "Bar")
|
37
|
+
assert_equal "Bar", hh["FooBar"]
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_subclassing
|
41
|
+
hc = Class.new(HipsterHash)
|
42
|
+
hh = hc.new(:foo_bar => "Baz")
|
43
|
+
assert_equal "Baz", hh["FooBar"]
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_method_missing
|
47
|
+
hh = HipsterHash.new(:Foo => :bar)
|
48
|
+
assert_equal :bar, hh.foo
|
49
|
+
assert_equal :bar, hh.FOO
|
50
|
+
end
|
28
51
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hipsterhash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|