shash 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -1
- data/lib/shash.rb +12 -9
- data/lib/version.rb +1 -1
- data/test/test_shash.rb +25 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -34,6 +34,14 @@ You can of course set values directly from a Shash object
|
|
34
34
|
Settings.has_key?("name") #=> true
|
35
35
|
Settings.name #=> "Shash!"
|
36
36
|
|
37
|
+
Slash keys are defaulted to strings. But what about symbols? You can tell Shash to handle keys the way you want
|
38
|
+
|
39
|
+
Settings = Shash.new{ |key| key.to_sym }
|
40
|
+
Settings.name = "Shash!"
|
41
|
+
Settings.has_key?(:name) #=> true
|
42
|
+
Settings.inspect #=> {:name=>"Shash!"}
|
43
|
+
Settings.name #=> "Shash!"
|
44
|
+
|
37
45
|
From a YAML file, or any other key/value kind of file
|
38
46
|
|
39
|
-
Settings = YAML.load_file( "path/to/your/file.yml" ).to_shash
|
47
|
+
Settings = YAML.load_file( File.open("path/to/your/file.yml") ).to_shash
|
data/lib/shash.rb
CHANGED
@@ -1,18 +1,21 @@
|
|
1
|
-
class Shash
|
2
|
-
|
3
|
-
|
4
|
-
def initialize(hash={})
|
1
|
+
class Shash
|
2
|
+
def initialize(hash={}, &proc)
|
5
3
|
@_hash = hash
|
4
|
+
@_proc = proc
|
6
5
|
end
|
6
|
+
|
7
|
+
def _key key
|
8
|
+
@_proc ? @_proc.call(key) : key
|
9
|
+
end
|
7
10
|
|
8
11
|
def method_missing(key, *args, &block)
|
9
12
|
if key[/=$/]
|
10
|
-
@_hash[key[0...-1]] = args.first
|
13
|
+
@_hash[_key(key[0...-1])] = args.first
|
11
14
|
else
|
12
|
-
if value = @_hash[key.to_s]
|
15
|
+
if value = @_hash[_key(key.to_s)]
|
13
16
|
case value
|
14
17
|
when Hash
|
15
|
-
Shash.new(value)
|
18
|
+
Shash.new(value, &@_proc)
|
16
19
|
else
|
17
20
|
value
|
18
21
|
end
|
@@ -23,12 +26,12 @@ class Shash
|
|
23
26
|
end
|
24
27
|
|
25
28
|
def has_key?(key)
|
26
|
-
@_hash.has_key?(key
|
29
|
+
@_hash.has_key?(key)
|
27
30
|
end
|
28
31
|
alias_method :key?, :has_key?
|
29
32
|
|
30
33
|
def ==(other)
|
31
|
-
|
34
|
+
other == @_hash
|
32
35
|
end
|
33
36
|
|
34
37
|
def []=(key,value)
|
data/lib/version.rb
CHANGED
data/test/test_shash.rb
CHANGED
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), "..", "lib", "shash")
|
|
2
2
|
|
3
3
|
describe Hash do
|
4
4
|
it "#to_shash should returns a new Shash object" do
|
5
|
-
{"a"=>1}.to_shash.should == Shash.new(
|
5
|
+
{"a"=>1}.to_shash.should == Shash.new("a"=>1)
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
@@ -43,4 +43,28 @@ describe Shash do
|
|
43
43
|
h.b.should == 2
|
44
44
|
end
|
45
45
|
|
46
|
+
it "#initialize should take a block and call it for each key before set/get" do
|
47
|
+
h = Shash.new(:a=>1){ |k| k.to_sym }
|
48
|
+
h.b = 2
|
49
|
+
h.should == {:a=>1, :b=>2}.to_shash
|
50
|
+
h.has_key?(:b).should == true
|
51
|
+
h.b.should == 2
|
52
|
+
end
|
53
|
+
|
54
|
+
it "#initialize should propagates the proc to sub-shashes when given" do
|
55
|
+
h = Shash.new{ |k| k.to_sym }
|
56
|
+
h.a = {}
|
57
|
+
h.a.b = 1
|
58
|
+
h.should == {:a=>{:b=>1}}
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should handles all sort of fancy things" do
|
62
|
+
h = Shash.new{ |k| k.upcase.reverse }
|
63
|
+
h.test = "are you sure?"
|
64
|
+
h.has_key?("test").should == false
|
65
|
+
h.has_key?("TSET").should == true
|
66
|
+
h.test.should == "are you sure?"
|
67
|
+
h.should == {"TSET"=>"are you sure?"}.to_shash
|
68
|
+
end
|
69
|
+
|
46
70
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
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: 2011-
|
12
|
+
date: 2011-09-01 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: Replace hash keys with method calls
|
15
15
|
email:
|