shash 0.0.6 → 0.0.7
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/.gitignore +1 -0
- data/lib/shash.rb +31 -26
- data/lib/version.rb +1 -1
- data/test/test_shash.rb +14 -28
- metadata +20 -41
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.idea
|
data/lib/shash.rb
CHANGED
@@ -1,27 +1,18 @@
|
|
1
|
-
class Shash
|
2
|
-
def initialize(hash={}
|
3
|
-
@
|
4
|
-
|
1
|
+
class Shash
|
2
|
+
def initialize(hash={})
|
3
|
+
@hash = {}
|
4
|
+
init!(hash)
|
5
5
|
end
|
6
|
-
|
7
|
-
def _key key
|
8
|
-
@_proc ? @_proc.call(key) : key.to_s
|
9
|
-
end
|
10
6
|
|
11
7
|
def method_missing(key, *args, &block)
|
12
8
|
if key[/=$/]
|
13
|
-
|
9
|
+
self[key[0...-1]] = args.first
|
14
10
|
else
|
15
|
-
if
|
16
|
-
|
17
|
-
when Hash
|
18
|
-
Shash.new(value, &@_proc)
|
19
|
-
else
|
20
|
-
value
|
21
|
-
end
|
11
|
+
if @hash.key?(key)
|
12
|
+
@hash[key]
|
22
13
|
else
|
23
14
|
begin
|
24
|
-
@
|
15
|
+
@hash.send(key, *args, &block)
|
25
16
|
rescue NoMethodError
|
26
17
|
nil
|
27
18
|
end
|
@@ -29,22 +20,36 @@ class Shash
|
|
29
20
|
end
|
30
21
|
end
|
31
22
|
|
32
|
-
def has_key?(key)
|
33
|
-
@_hash.has_key?(key)
|
34
|
-
end
|
35
|
-
alias_method :key?, :has_key?
|
36
|
-
|
37
23
|
def ==(other)
|
38
|
-
other == @
|
24
|
+
other == @hash
|
39
25
|
end
|
40
26
|
|
41
27
|
def []=(key,value)
|
42
|
-
@
|
28
|
+
@hash[key.respond_to?(:to_sym) ? key.to_sym : key] = shashify(value)
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def init!(hash)
|
34
|
+
hash.each do |k,v|
|
35
|
+
self[k] = v
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def shashify(value)
|
40
|
+
case value
|
41
|
+
when Hash
|
42
|
+
Shash.new(value)
|
43
|
+
when Array
|
44
|
+
value.map{|v| self.shashify(v)}
|
45
|
+
else
|
46
|
+
value
|
47
|
+
end
|
43
48
|
end
|
44
49
|
end
|
45
50
|
|
46
51
|
class Hash
|
47
|
-
def to_shash
|
48
|
-
Shash.new(self
|
52
|
+
def to_shash
|
53
|
+
Shash.new(self)
|
49
54
|
end
|
50
55
|
end
|
data/lib/version.rb
CHANGED
data/test/test_shash.rb
CHANGED
@@ -26,45 +26,31 @@ describe Shash do
|
|
26
26
|
|
27
27
|
it "#has_key? should check for key existence correctly" do
|
28
28
|
h = {"a"=>1, "b"=>{"c"=>2}}.to_shash
|
29
|
-
h.has_key?(
|
30
|
-
h.has_key?(
|
31
|
-
h.b.has_key?(
|
29
|
+
h.has_key?(:z).should == false
|
30
|
+
h.has_key?(:b).should == true
|
31
|
+
h.b.has_key?(:c).should == true
|
32
32
|
end
|
33
33
|
|
34
34
|
it "#[] should still works as expected" do
|
35
35
|
h = {"a"=>1}.to_shash
|
36
36
|
h.a.should == 1
|
37
|
-
h[
|
37
|
+
h[:a].should == 1
|
38
38
|
end
|
39
39
|
|
40
40
|
it "#[]= should still works as expected" do
|
41
41
|
h = Shash.new
|
42
|
-
h[
|
43
|
-
h.b.should == 2
|
44
|
-
end
|
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
|
42
|
+
h[:b] = 2
|
51
43
|
h.b.should == 2
|
52
44
|
end
|
53
|
-
|
54
|
-
it "#
|
55
|
-
h =
|
56
|
-
h.a
|
57
|
-
h.
|
58
|
-
h.should == {:
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
45
|
+
|
46
|
+
it "#should handle hashes containing arrays properly" do
|
47
|
+
h = {:a => 1, :b => [{:ba => 1, :bb => 2}, [{:ccc => 3}]]}.to_shash
|
48
|
+
h.a.should == 1
|
49
|
+
h.b.should == [{:ba => 1, :bb => 2}.to_shash, [{:ccc => 3}.to_shash]]
|
50
|
+
h.b[0].should == {:ba => 1, :bb => 2}.to_shash
|
51
|
+
h.b[1][0].should == {:ccc => 3}.to_shash
|
52
|
+
h.b[1][0].ccc.should == 3
|
53
|
+
h.b.last.first.ccc.should == 3
|
68
54
|
end
|
69
55
|
|
70
56
|
end
|
metadata
CHANGED
@@ -1,74 +1,53 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: shash
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 6
|
10
|
-
version: 0.0.6
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Christian Blais
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2011-11-07 00:00:00 -05:00
|
19
|
-
default_executable:
|
12
|
+
date: 2012-01-25 00:00:00.000000000 Z
|
20
13
|
dependencies: []
|
21
|
-
|
22
14
|
description: Replace hash keys with method calls
|
23
|
-
email:
|
15
|
+
email:
|
24
16
|
- christ.blais@gmail.com
|
25
17
|
executables: []
|
26
|
-
|
27
18
|
extensions: []
|
28
|
-
|
29
19
|
extra_rdoc_files: []
|
30
|
-
|
31
|
-
|
20
|
+
files:
|
21
|
+
- .gitignore
|
32
22
|
- LICENSE
|
33
23
|
- README.md
|
34
24
|
- lib/shash.rb
|
35
25
|
- lib/version.rb
|
36
26
|
- shash.gemspec
|
37
27
|
- test/test_shash.rb
|
38
|
-
has_rdoc: true
|
39
28
|
homepage: http://github.com/christianblais/shash
|
40
29
|
licenses: []
|
41
|
-
|
42
30
|
post_install_message:
|
43
31
|
rdoc_options: []
|
44
|
-
|
45
|
-
require_paths:
|
32
|
+
require_paths:
|
46
33
|
- lib
|
47
34
|
- test
|
48
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
36
|
none: false
|
50
|
-
requirements:
|
51
|
-
- -
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
|
54
|
-
|
55
|
-
- 0
|
56
|
-
version: "0"
|
57
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
42
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
segments:
|
64
|
-
- 0
|
65
|
-
version: "0"
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
66
47
|
requirements: []
|
67
|
-
|
68
48
|
rubyforge_project:
|
69
|
-
rubygems_version: 1.
|
49
|
+
rubygems_version: 1.8.10
|
70
50
|
signing_key:
|
71
51
|
specification_version: 3
|
72
52
|
summary: Replace hash keys with method calls
|
73
53
|
test_files: []
|
74
|
-
|