hashi 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.textile +123 -0
  2. data/Rakefile +54 -0
  3. data/lib/hashi.rb +31 -0
  4. metadata +57 -0
@@ -0,0 +1,123 @@
1
+ h1. Hash and Objects
2
+
3
+ It's a common task to simulate objects from hashes, but one has to remember using hash access format in order to access it. Hashi helps you by allowing direct access to its content:
4
+
5
+ <pre>
6
+ hash = { :team => {:players => [{:name=>"guilherme silveira"},{:name=>"jose donizetti"}]}}
7
+ object = Hashi.to_object(hash)
8
+ puts object.team.players[0].name
9
+ </pre>
10
+
11
+ h1. Why would I use Hashi?
12
+
13
+ <img src="http://robotomi.files.wordpress.com/2007/06/hashi.jpg" />
14
+
15
+ h2. The reality without Hashi
16
+
17
+ <pre>
18
+ hash = { :team => {:players => [{:name=>"guilherme silveira"},{:name=>"jose donizetti"}]}}
19
+ puts hash[:team][:players][0][:name] # his name
20
+ </pre>
21
+
22
+ Although the Ruby language provides an easy way to access hashes, it becomes easier if you use Rails and
23
+ has created an ActiveRecord for your representation:
24
+
25
+ h2. The reality without Hashi, within Rails
26
+
27
+ <pre>
28
+ class Team < ActiveRecord::Base
29
+ has_many :players
30
+ end
31
+ class Player < ActiveRecord::Base
32
+ # create a migration with field name:string
33
+ end
34
+ hash = { :team => {:players => [{:name=>"guilherme silveira"},{:name=>"jose donizetti"}]}}
35
+ puts hash[:player][0][:name] # his name
36
+ </pre>
37
+
38
+ h2. How does it compare to Hashie?
39
+
40
+ Hashi actually does not create your attributes, instead it deals with *method_missing* invocations to
41
+ simulate those properties, never creating a copy of your hash.
42
+
43
+ h2. More info
44
+
45
+ You can access the original hash (updated if you have made any changes) by one of those two ways:
46
+
47
+ <pre>
48
+ hash = { :team => {:players => [{:name=>"guilherme silveira"},{:name=>"jose donizetti"}]}}
49
+ object = Hashi.to_object(hash)
50
+ object.team.players[0].name = 'jose donizetti'
51
+
52
+ # the original hash was modified!
53
+ puts hash
54
+
55
+ # you can extract the original hash if you wish
56
+ puts object.hash
57
+
58
+ </pre>
59
+
60
+ h1. Installing
61
+
62
+ h2. Ruby
63
+
64
+ gem install gemcutter
65
+ gem tumble
66
+ gem install hashi
67
+
68
+ h2. Rails
69
+
70
+ Just add in your environment.rb the following line:
71
+
72
+ <pre>
73
+ config.gem "hashi", :source => "http://gemcutter.org"
74
+ </pre>
75
+
76
+ And then execute:
77
+ <pre>rake gems:install</pre>
78
+
79
+ or, if you prefer to install it as a plugin:
80
+
81
+ <pre>script/plugin install git://github.com/caelum/hashi.git</pre>
82
+
83
+
84
+ h2. Help
85
+
86
+ If you are looking for or want to help, let us know at the mailing list:
87
+
88
+ "http://groups.google.com/group/hashi-ruby":http://groups.google.com/group/hashi-ruby
89
+
90
+ h2. Team
91
+
92
+ Hashi was created and is maintained within Caelum:http://www.caelum.com.br by
93
+
94
+ Projetct Founder
95
+ * "Guilherme Silveira":mailto:guilherme.silveira@caelum.com.br - twitter:http://www.twitter.com/guilhermecaelum "http://guilhermesilveira.wordpress.com":http://guilhermesilveira.wordpress.com
96
+
97
+ Active Commiters
98
+ * Guilherme Silveira
99
+ * Jose Donizetti
100
+
101
+ h3. Sources
102
+
103
+ You can see its source code at: "github":http://github.com/caelum/hashi
104
+
105
+ h2. License
106
+
107
+ /***
108
+ * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
109
+ * All rights reserved.
110
+ *
111
+ * Licensed under the Apache License, Version 2.0 (the "License");
112
+ * you may not use this file except in compliance with the License.
113
+ * You may obtain a copy of the License at
114
+ *
115
+ * http://www.apache.org/licenses/LICENSE-2.0
116
+ *
117
+ * Unless required by applicable law or agreed to in writing, software
118
+ * distributed under the License is distributed on an "AS IS" BASIS,
119
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120
+ * See the License for the specific language governing permissions and
121
+ * limitations under the License.
122
+ */
123
+
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rubygems/specification'
3
+ require 'rake'
4
+ require 'rake/gempackagetask'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM = "hashi"
8
+ GEM_VERSION = "1.0"
9
+ SUMMARY = "Hash to object helper methods."
10
+ AUTHOR = "Guilherme Silveira, Jose Donizetti"
11
+ EMAIL = "guilherme.silveira@caelum.com.br"
12
+ HOMEPAGE = "http://github.com/caelum/hashi"
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = GEM
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.summary = SUMMARY
19
+ s.require_paths = ['lib']
20
+ s.files = FileList['lib/**/*.rb', '[A-Z]*'].to_a
21
+
22
+ # s.add_dependency(%q<rubigen>, [">= 1.3.4"])
23
+
24
+ s.author = AUTHOR
25
+ s.email = EMAIL
26
+ s.homepage = HOMEPAGE
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new do |t|
30
+ t.spec_files = FileList['spec/**/*_spec.rb']
31
+ t.spec_opts = %w(-fs -fh:doc/specs.html --color)
32
+ end
33
+
34
+ Rake::GemPackageTask.new(spec) do |pkg|
35
+ pkg.gem_spec = spec
36
+ end
37
+
38
+ desc "Install the gem locally"
39
+ task :install => [:package] do
40
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
41
+ end
42
+
43
+ desc "Create a gemspec file"
44
+ task :make_spec do
45
+ File.open("#{GEM}.gemspec", "w") do |file|
46
+ file.puts spec.to_ruby
47
+ end
48
+ end
49
+
50
+ desc "Builds the project"
51
+ task :build => :spec
52
+
53
+ desc "Default build will run specs"
54
+ task :default => :spec
@@ -0,0 +1,31 @@
1
+ module Hashi
2
+ class CustomHash
3
+
4
+ attr_reader :hash
5
+
6
+ def initialize(h)
7
+ @hash = h
8
+ end
9
+ def method_missing(name, *args)
10
+ name = name.to_s if name.kind_of? Symbol
11
+ if name[-1,1] == "="
12
+ name = name.chop
13
+ @hash[name] = args[0]
14
+ else
15
+ transform(@hash[name])
16
+ end
17
+ end
18
+ def [](x)
19
+ transform(@hash[x])
20
+ end
21
+ private
22
+ def transform(value)
23
+ return CustomHash.new(value) if value.kind_of?(Hash) || value.kind_of?(Array)
24
+ value
25
+ end
26
+
27
+ end
28
+ def self.to_object(h)
29
+ CustomHash.new(h)
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashi
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Guilherme Silveira, Jose Donizetti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-26 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: guilherme.silveira@caelum.com.br
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/hashi.rb
26
+ - Rakefile
27
+ - README.textile
28
+ has_rdoc: true
29
+ homepage: http://github.com/caelum/hashi
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Hash to object helper methods.
56
+ test_files: []
57
+