hash-path 0.0.1 → 0.0.2
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/README +5 -1
- data/Rakefile +1 -1
- data/lib/hash-path.rb +26 -9
- data/spec/api_spec.rb +59 -0
- data/spec/inherit_spec.rb +34 -0
- data/spec/its_helper.rb +15 -0
- data/spec/spec_helper.rb +10 -0
- metadata +6 -2
data/README
CHANGED
@@ -30,12 +30,16 @@ Example
|
|
30
30
|
path :body, "Body"
|
31
31
|
path :code, "Additional/Code"
|
32
32
|
path :time, "Additional/StartDateTime"
|
33
|
+
|
34
|
+
def time
|
35
|
+
Time.parse(super) # parse is defined in active_support, night-time,... gem
|
36
|
+
end
|
33
37
|
end
|
34
38
|
|
35
39
|
item = Item.new(hash)
|
36
40
|
item.name # => "foo"
|
37
41
|
item.code # => 1234
|
38
|
-
item.time # =>
|
42
|
+
item.time # => Sun Jan 10 19:30:00 +0900 2010
|
39
43
|
|
40
44
|
|
41
45
|
Todo
|
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ AUTHOR = "maiha"
|
|
6
6
|
EMAIL = "maiha@wota.jp"
|
7
7
|
HOMEPAGE = "http://github.com/maiha/hash-path"
|
8
8
|
SUMMARY = "path accessor to hierarchical hash"
|
9
|
-
GEM_VERSION = "0.0.
|
9
|
+
GEM_VERSION = "0.0.2"
|
10
10
|
|
11
11
|
spec = Gem::Specification.new do |s|
|
12
12
|
s.rubyforge_project = 'asakusarb'
|
data/lib/hash-path.rb
CHANGED
@@ -1,24 +1,41 @@
|
|
1
1
|
|
2
|
-
class HashPath
|
2
|
+
class HashPath < Hash
|
3
3
|
def self.paths
|
4
4
|
@paths ||= {}
|
5
5
|
end
|
6
6
|
|
7
7
|
def self.path(key, path)
|
8
|
-
paths[key.
|
8
|
+
paths[key.to_s] = path
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
11
|
+
def [](key)
|
12
|
+
key = key.to_s
|
13
|
+
|
14
|
+
# first, check exact value
|
15
|
+
return super if has_key?(key)
|
16
|
+
|
17
|
+
# second, browse children
|
18
|
+
return key.split("/").inject(self) {|hash, key| hash[key]}
|
19
|
+
rescue Exception => e
|
20
|
+
return rescue_hierarchical_access(key, e)
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(hash = {})
|
24
|
+
super()
|
25
|
+
replace(hash)
|
14
26
|
end
|
15
27
|
|
16
28
|
private
|
29
|
+
def rescue_hierarchical_access(key, e)
|
30
|
+
return nil
|
31
|
+
end
|
32
|
+
|
17
33
|
def method_missing(name, *args, &block)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
34
|
+
if args.empty?
|
35
|
+
self[ self.class.paths[name.to_s] || name ]
|
36
|
+
else
|
37
|
+
super
|
38
|
+
end
|
22
39
|
end
|
23
40
|
end
|
24
41
|
|
data/spec/api_spec.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
|
2
|
+
require File.join(File.dirname(__FILE__), 'spec_helper.rb')
|
3
|
+
|
4
|
+
describe HashPath do
|
5
|
+
it "should provide .path" do
|
6
|
+
HashPath.should respond_to(:path)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should provide #[]" do
|
10
|
+
HashPath.new.should respond_to(:[])
|
11
|
+
end
|
12
|
+
|
13
|
+
def data
|
14
|
+
{
|
15
|
+
"app" => {
|
16
|
+
"models" => "ActiveRecord",
|
17
|
+
"controllers" => "ActionController",
|
18
|
+
"views" => "ActionView",
|
19
|
+
"helpers" => "ActionView::Helpers",
|
20
|
+
},
|
21
|
+
"vendor" => {
|
22
|
+
"plugins" => {
|
23
|
+
"aasm" => "Aasm",
|
24
|
+
"haml" => "Haml",
|
25
|
+
},
|
26
|
+
}
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#[]" do
|
31
|
+
subject { HashPath.new(data) }
|
32
|
+
|
33
|
+
context "(for present)" do
|
34
|
+
its("app/models") { should == "ActiveRecord" }
|
35
|
+
its("vendor/plugins") { should == {"aasm" => "Aasm", "haml" => "Haml"} }
|
36
|
+
end
|
37
|
+
|
38
|
+
context "(for missing)" do
|
39
|
+
its("foo") { should == nil }
|
40
|
+
its("foo/bar") { should == nil }
|
41
|
+
end
|
42
|
+
|
43
|
+
context "(for defined path)" do
|
44
|
+
class Labeled < HashPath
|
45
|
+
path :model , "app/models"
|
46
|
+
path :nothing , "no/such/value"
|
47
|
+
end
|
48
|
+
|
49
|
+
subject { Labeled.new(data) }
|
50
|
+
|
51
|
+
its(:model) { should == "ActiveRecord" }
|
52
|
+
its("model") { should == "ActiveRecord" }
|
53
|
+
its("app/models") { should == "ActiveRecord" }
|
54
|
+
its("vendor/plugins") { should == {"aasm" => "Aasm", "haml" => "Haml"} }
|
55
|
+
its(:nothing) { should == nil }
|
56
|
+
its("nothing") { should == nil }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
|
2
|
+
require File.join(File.dirname(__FILE__), 'spec_helper.rb')
|
3
|
+
|
4
|
+
require 'parsedate'
|
5
|
+
|
6
|
+
describe HashPath do
|
7
|
+
def data
|
8
|
+
{
|
9
|
+
"calendar" => {
|
10
|
+
"start" => "2010/01/10 23:30",
|
11
|
+
"stop" => "2010/01/10 23:56",
|
12
|
+
}
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
class Event < HashPath
|
17
|
+
path :st, "calendar/start"
|
18
|
+
path :en, "calendar/stop"
|
19
|
+
|
20
|
+
def st
|
21
|
+
to_time(super)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def to_time(string)
|
26
|
+
array = ParseDate.parsedate(string)
|
27
|
+
Time.mktime(*array[0,6])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
subject { Event.new(data) }
|
32
|
+
its(:st) { should == Time.mktime(2010, 1, 10, 23, 30) }
|
33
|
+
its(:en) { should == "2010/01/10 23:56" }
|
34
|
+
end
|
data/spec/its_helper.rb
ADDED
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash-path
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maiha
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-12 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -27,6 +27,10 @@ files:
|
|
27
27
|
- README
|
28
28
|
- Rakefile
|
29
29
|
- lib/hash-path.rb
|
30
|
+
- spec/inherit_spec.rb
|
31
|
+
- spec/api_spec.rb
|
32
|
+
- spec/its_helper.rb
|
33
|
+
- spec/spec_helper.rb
|
30
34
|
has_rdoc: true
|
31
35
|
homepage: http://github.com/maiha/hash-path
|
32
36
|
licenses: []
|