johnny-hash 0.1.0
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/.document +5 -0
- data/.gitignore +22 -0
- data/README.md +17 -0
- data/Rakefile +44 -0
- data/VERSION +1 -0
- data/lib/johnny_hash.rb +36 -0
- data/test/jsony_hash_test.rb +42 -0
- data/test/teststrap.rb +4 -0
- metadata +89 -0
data/.document
ADDED
data/.gitignore
ADDED
data/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Jsony-Hash
|
2
|
+
==========
|
3
|
+
|
4
|
+
Examples
|
5
|
+
--------
|
6
|
+
|
7
|
+
hsh = {:a => {'b' => [1, {:c => :d}]}}.json!
|
8
|
+
|
9
|
+
hsh.a.b # => [1, {:c=>:d}]
|
10
|
+
|
11
|
+
hsh[:a]['b'] # => [1, {:c=>:d}]
|
12
|
+
|
13
|
+
hsh.a.b[1].c # => :d
|
14
|
+
|
15
|
+
json_object = {"a" => {"b" => {"c" => ["foo", {"d" => "bar"}]}}}.json!
|
16
|
+
|
17
|
+
json_object.a['b'].c[1].d # => bar
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
#
|
7
|
+
# Other rake tasks
|
8
|
+
|
9
|
+
Dir[File.dirname(__FILE__) + "/lib/tasks/*.rake"].each { |rake_task| load rake_task }
|
10
|
+
|
11
|
+
#
|
12
|
+
# Console!
|
13
|
+
|
14
|
+
task(:console) { exec "irb -r boot" }
|
15
|
+
|
16
|
+
#
|
17
|
+
# Testing
|
18
|
+
|
19
|
+
Rake::TestTask.new("test") do |t|
|
20
|
+
t.libs << "test"
|
21
|
+
t.pattern = "test/**/*_test.rb"
|
22
|
+
t.verbose = false
|
23
|
+
end
|
24
|
+
Rake::Task["test"].instance_variable_set(:@full_comment, nil) # Dumb dumb dumb
|
25
|
+
Rake::Task["test"].comment = "Run the tests!"
|
26
|
+
|
27
|
+
task :default => :test
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'jeweler'
|
31
|
+
Jeweler::Tasks.new do |gem|
|
32
|
+
gem.name = "johnny-hash"
|
33
|
+
gem.summary = %Q{Adds json-object-style Hash accessors in Ruby for fun.}
|
34
|
+
gem.description = %Q{Read the awful readme for more info.}
|
35
|
+
gem.email = "scissorjammer@gmail.com"
|
36
|
+
gem.homepage = "http://github.com/toothrot/johnny-hash"
|
37
|
+
gem.authors = ["toothrot", "jaknowlden"]
|
38
|
+
gem.add_development_dependency "riot", ">= 0"
|
39
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
40
|
+
end
|
41
|
+
Jeweler::GemcutterTasks.new
|
42
|
+
rescue LoadError
|
43
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
44
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/johnny_hash.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module JohnnyHash
|
2
|
+
def self.walk_the_line!(value, raise_on_missing)
|
3
|
+
if value.respond_to?(:json!) then value.json!(raise_on_missing) else value end
|
4
|
+
end
|
5
|
+
|
6
|
+
module Hash
|
7
|
+
def json!(raise_on_missing = true)
|
8
|
+
@raise_on_missing = raise_on_missing
|
9
|
+
|
10
|
+
unless respond_to?(:non_json_reader)
|
11
|
+
class << self
|
12
|
+
alias_method :non_json_reader, :[]
|
13
|
+
|
14
|
+
def [](key)
|
15
|
+
JohnnyHash.walk_the_line!(non_json_reader(key), @raise_on_missing)
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(sym, *args, &block)
|
19
|
+
self[sym] || self[sym.to_s] || (super if @raise_on_missing)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
self
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
module Array
|
29
|
+
def json!(raise_on_missing = true)
|
30
|
+
each {|element| JohnnyHash.walk_the_line!(element, raise_on_missing) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Hash.send(:include, JohnnyHash::Hash)
|
36
|
+
Array.send(:include, JohnnyHash::Array)
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'teststrap'
|
2
|
+
|
3
|
+
context "JohnnyHash" do
|
4
|
+
asserts "Allows dot access for existing elements" do
|
5
|
+
{:a => 'foo'}.json!.a
|
6
|
+
end.equals 'foo'
|
7
|
+
|
8
|
+
asserts "Raises a NoMethodError for non-existing elements" do
|
9
|
+
{:a => 'foo'}.json!.b
|
10
|
+
end.raises NoMethodError
|
11
|
+
|
12
|
+
asserts "Optionally returns nil instead of raising on missing elements" do
|
13
|
+
{:a => 'foo'}.json!(false).b
|
14
|
+
end.equals nil
|
15
|
+
|
16
|
+
context "an interesting hash" do
|
17
|
+
setup do
|
18
|
+
{:a => {'b' => [1, {:c => :d}]}}.json!
|
19
|
+
end
|
20
|
+
|
21
|
+
asserts "nests jsony-ness" do
|
22
|
+
topic.a.b
|
23
|
+
end.equals [1, {:c => :d}]
|
24
|
+
|
25
|
+
asserts "nests jsony-ness on normal lookups too" do
|
26
|
+
topic[:a].b
|
27
|
+
end.equals [1, {:c => :d}]
|
28
|
+
|
29
|
+
asserts "nests jsony-ness through array-style lookup" do
|
30
|
+
topic.a.b[1].c
|
31
|
+
end.equals :d
|
32
|
+
|
33
|
+
asserts "nests not raising on missing through array-style lookup" do
|
34
|
+
topic.json!(false).a.b[1].z
|
35
|
+
end.equals nil
|
36
|
+
|
37
|
+
asserts "json! can be called multiple times" do
|
38
|
+
topic.json!.json!.a.b
|
39
|
+
end.equals [1, {:c => :d}]
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/test/teststrap.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: johnny-hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- toothrot
|
14
|
+
- jaknowlden
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-09-08 00:00:00 -05:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: riot
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
description: Read the awful readme for more info.
|
37
|
+
email: scissorjammer@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- README.md
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/johnny_hash.rb
|
51
|
+
- test/jsony_hash_test.rb
|
52
|
+
- test/teststrap.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/toothrot/johnny-hash
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --charset=UTF-8
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Adds json-object-style Hash accessors in Ruby for fun.
|
87
|
+
test_files:
|
88
|
+
- test/jsony_hash_test.rb
|
89
|
+
- test/teststrap.rb
|