hash-path 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +52 -0
  3. data/Rakefile +51 -0
  4. data/lib/hash-path.rb +24 -0
  5. metadata +59 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [maiha@wota.jp]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,52 @@
1
+ hash-path
2
+ =========
3
+
4
+ path accessor to hierarchical hash
5
+
6
+
7
+ Class
8
+ =====
9
+
10
+ * HashPath
11
+ .path : define path accessor
12
+ .paths : defined paths
13
+
14
+
15
+ Example
16
+ =======
17
+
18
+ # JSON.parse(...) returns like this
19
+ hash = {
20
+ "Name" => "foo",
21
+ "Body" => "content",
22
+ "Additional" => {
23
+ "Code" => 1234,
24
+ "StartDateTime"=>"2010/01/10 19:30"
25
+ }
26
+ }
27
+
28
+ class Item < HashPath
29
+ path :name, "Name"
30
+ path :body, "Body"
31
+ path :code, "Additional/Code"
32
+ path :time, "Additional/StartDateTime"
33
+ end
34
+
35
+ item = Item.new(hash)
36
+ item.name # => "foo"
37
+ item.code # => 1234
38
+ item.time # => "2010/01/10 19:30"
39
+
40
+
41
+ Todo
42
+ ====
43
+
44
+ * create spec
45
+ * convert value to some type automatically
46
+
47
+
48
+ Author
49
+ ======
50
+
51
+ maiha@wota.jp
52
+
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ GEM_NAME = "hash-path"
5
+ AUTHOR = "maiha"
6
+ EMAIL = "maiha@wota.jp"
7
+ HOMEPAGE = "http://github.com/maiha/hash-path"
8
+ SUMMARY = "path accessor to hierarchical hash"
9
+ GEM_VERSION = "0.0.1"
10
+
11
+ spec = Gem::Specification.new do |s|
12
+ s.rubyforge_project = 'asakusarb'
13
+ s.executables = []
14
+ s.name = GEM_NAME
15
+ s.version = GEM_VERSION
16
+ s.platform = Gem::Platform::RUBY
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ["README", "MIT-LICENSE"]
19
+ s.summary = SUMMARY
20
+ s.description = s.summary
21
+ s.author = AUTHOR
22
+ s.email = EMAIL
23
+ s.homepage = HOMEPAGE
24
+ s.require_path = 'lib'
25
+ s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec}/**/*")
26
+ end
27
+
28
+ Rake::GemPackageTask.new(spec) do |pkg|
29
+ pkg.gem_spec = spec
30
+ end
31
+
32
+ desc "Install the gem"
33
+ task :install do
34
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
35
+ end
36
+
37
+ desc "Uninstall the gem"
38
+ task :uninstall do
39
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
40
+ end
41
+
42
+ desc "Create a gemspec file"
43
+ task :gemspec do
44
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
45
+ file.puts spec.to_ruby
46
+ end
47
+ end
48
+
49
+ require 'spec/rake/spectask'
50
+ desc 'Default: run spec examples'
51
+ task :default => 'spec'
@@ -0,0 +1,24 @@
1
+
2
+ class HashPath
3
+ def self.paths
4
+ @paths ||= {}
5
+ end
6
+
7
+ def self.path(key, path)
8
+ paths[key.to_sym] = path
9
+ end
10
+
11
+ def initialize(hash)
12
+ @hash = hash
13
+ @cached = {}
14
+ end
15
+
16
+ private
17
+ def method_missing(name, *args, &block)
18
+ name = name.to_sym
19
+ return @cached[name] if @cached.has_key?(name)
20
+ return super unless path = self.class.paths[name]
21
+ return @cached[name] = path.split("/").inject(@hash) {|hash, key| hash[key]} rescue nil
22
+ end
23
+ end
24
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash-path
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - maiha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-10 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: path accessor to hierarchical hash
17
+ email: maiha@wota.jp
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - MIT-LICENSE
25
+ files:
26
+ - MIT-LICENSE
27
+ - README
28
+ - Rakefile
29
+ - lib/hash-path.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/maiha/hash-path
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project: asakusarb
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: path accessor to hierarchical hash
58
+ test_files: []
59
+