superhash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 James Wilding
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.md ADDED
@@ -0,0 +1,48 @@
1
+ # superhash!
2
+
3
+ Superhash adds OpenStruct-like features to your hashes, but the magic goes as deep as you like:
4
+
5
+ @person = {
6
+ :name => 'James',
7
+ :age => 28,
8
+ :male => true,
9
+ :friends => {
10
+ :chuck => {
11
+ :age => 29,
12
+ :location => 'USA'
13
+ },
14
+ :john_paul => {
15
+ :age => 25,
16
+ :location => 'France'
17
+ }
18
+ }
19
+ }
20
+
21
+ @person.name # => 'James'
22
+ @person.age # => 28
23
+ @person.friends.chuck.age # => 29
24
+ @person.friends.john_paul.location # => 'France'
25
+
26
+ # You also get questioning methods:
27
+ @person.male? # => true
28
+
29
+ You get the idea :)
30
+
31
+ ## Installation
32
+
33
+ From Github -- gem to follow.
34
+
35
+ == Note on Patches/Pull Requests
36
+
37
+ * Fork the project.
38
+ * Make your feature addition or bug fix.
39
+ * Add tests for it. This is important so I don't break it in a
40
+ future version unintentionally.
41
+ * Commit, do not mess with rakefile, version, or history.
42
+ (if you want to have your own version, that is fine but
43
+ bump version in a commit by itself I can ignore when I pull)
44
+ * Send me a pull request. Bonus points for topic branches.
45
+
46
+ == Copyright
47
+
48
+ Copyright (c) 2010 jameswilding. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "superhash"
8
+ gem.summary = %Q{Magic powers for your hashes}
9
+ gem.description = %Q{Access hash values with method calls. Easy on the eye! Not actually magic!}
10
+ gem.email = "james@jameswilding.net"
11
+ gem.homepage = "http://github.com/jameswilding/superhash"
12
+ gem.authors = ["James Wilding"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.add_development_dependency "redgreen"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION')
46
+ version = File.read('VERSION')
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "superhash #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/superhash.rb ADDED
@@ -0,0 +1,18 @@
1
+ # Hours of work, this!
2
+ class Hash
3
+ def method_missing(method, *args, &block)
4
+ self[method] or
5
+ self[method.to_s] or
6
+ self[_boolean_key(method)] or
7
+ self[_boolean_key(method).to_s] or
8
+ super
9
+ end
10
+
11
+ private
12
+ # Strictly speaking, the method? pattern works on *any* key in
13
+ # a hash -- not just those with true/false values -- but shhh,
14
+ # don't tell anyone ;)
15
+ def _boolean_key(method)
16
+ method.to_s.sub(/\?$/, '').to_sym
17
+ end
18
+ end
data/superhash.gemspec ADDED
@@ -0,0 +1,53 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{superhash}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["James Wilding"]
9
+ s.date = %q{2010-01-13}
10
+ s.description = %q{Access hash values with method calls. Easy on the eye! Not actually magic!}
11
+ s.email = %q{james@jameswilding.net}
12
+ s.extra_rdoc_files = [
13
+ "LICENSE",
14
+ "README.md"
15
+ ]
16
+ s.files = [
17
+ ".document",
18
+ ".gitignore",
19
+ "LICENSE",
20
+ "README.md",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "lib/superhash.rb",
24
+ "superhash.gemspec",
25
+ "test/superhash_test.rb",
26
+ "test/test_helper.rb"
27
+ ]
28
+ s.homepage = %q{http://github.com/jameswilding/superhash}
29
+ s.rdoc_options = ["--charset=UTF-8"]
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.3.5}
32
+ s.summary = %q{Magic powers for your hashes}
33
+ s.test_files = [
34
+ "test/superhash_test.rb",
35
+ "test/test_helper.rb"
36
+ ]
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
44
+ s.add_development_dependency(%q<redgreen>, [">= 0"])
45
+ else
46
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
47
+ s.add_dependency(%q<redgreen>, [">= 0"])
48
+ end
49
+ else
50
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ s.add_dependency(%q<redgreen>, [">= 0"])
52
+ end
53
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ class SuperhashTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @person = {
7
+ :name => 'James',
8
+ :age => 28,
9
+ :twitter => {
10
+ :followers => {:number => 10000, :lie => true}
11
+ }
12
+ }
13
+ end
14
+
15
+
16
+ should 'access values using methods at the top level' do
17
+ assert_equal 'James', @person.name
18
+ assert_equal 28, @person.age
19
+ end
20
+
21
+ should 'access values in nested hashes' do
22
+ assert_equal 10000, @person.twitter.followers.number
23
+ end
24
+
25
+ should 'accept question-mark methods for true/false values' do
26
+ assert @person.twitter.followers.lie?
27
+ end
28
+
29
+ should 'act like a normal hash with regard to errors when a method references a missing key' do
30
+ assert_raise(NoMethodError) { @person.hobbies }
31
+ end
32
+
33
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'redgreen'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'superhash'
9
+
10
+ class Test::Unit::TestCase
11
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: superhash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Wilding
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-13 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: redgreen
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Access hash values with method calls. Easy on the eye! Not actually magic!
36
+ email: james@jameswilding.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.md
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/superhash.rb
52
+ - superhash.gemspec
53
+ - test/superhash_test.rb
54
+ - test/test_helper.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/jameswilding/superhash
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --charset=UTF-8
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.5
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Magic powers for your hashes
83
+ test_files:
84
+ - test/superhash_test.rb
85
+ - test/test_helper.rb