mash 0.0.1
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/History.txt +6 -0
- data/Manifest.txt +7 -0
- data/README.txt +51 -0
- data/Rakefile +18 -0
- data/lib/mash.rb +105 -0
- data/spec/mash_spec.rb +60 -0
- data/spec/spec_helper.rb +1 -0
- metadata +71 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= Mash (Mocking Hash)
|
2
|
+
|
3
|
+
http://github.com/mbleigh/mash
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Mash is an extended Hash that gives simple pseudo-object functionality
|
8
|
+
that can be built from hashes and easily extended. It is designed to
|
9
|
+
be used in RESTful API libraries to provide easy object-like access
|
10
|
+
to JSON and XML parsed hashes.
|
11
|
+
|
12
|
+
== SYNOPSIS:
|
13
|
+
|
14
|
+
mash = Mash.new
|
15
|
+
mash.name? # => false
|
16
|
+
mash.name = "My Mash"
|
17
|
+
mash.name # => "My Mash"
|
18
|
+
mash.name? # => true
|
19
|
+
mash.inspect # => <Mash name="My Mash">
|
20
|
+
|
21
|
+
== INSTALL:
|
22
|
+
|
23
|
+
RubyGem:
|
24
|
+
|
25
|
+
sudo gem install mash
|
26
|
+
|
27
|
+
Git:
|
28
|
+
git clone git://github.com/mbleigh/mash
|
29
|
+
|
30
|
+
== LICENSE:
|
31
|
+
|
32
|
+
Copyright (c) 2008 Michael Bleigh
|
33
|
+
|
34
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
35
|
+
a copy of this software and associated documentation files (the
|
36
|
+
'Software'), to deal in the Software without restriction, including
|
37
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
38
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
39
|
+
permit persons to whom the Software is furnished to do so, subject to
|
40
|
+
the following conditions:
|
41
|
+
|
42
|
+
The above copyright notice and this permission notice shall be
|
43
|
+
included in all copies or substantial portions of the Software.
|
44
|
+
|
45
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
46
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
47
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
48
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
49
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
50
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
51
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/mash.rb'
|
6
|
+
require 'spec/rake/spectask'
|
7
|
+
|
8
|
+
Hoe.new('mash', Mash::VERSION) do |p|
|
9
|
+
p.rubyforge_name = 'mash-hash' # if different than lowercase project name
|
10
|
+
p.developer('Michael Bleigh', 'michael@intridea.com')
|
11
|
+
p.remote_rdoc_dir = ''
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Run specs."
|
15
|
+
Spec::Rake::SpecTask.new("spec") do |t|
|
16
|
+
t.spec_files = "spec/*_spec.rb"
|
17
|
+
end
|
18
|
+
# vim: syntax=Ruby
|
data/lib/mash.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# Mash allows you to create pseudo-objects that have method-like
|
2
|
+
# accessors for hash keys. This is useful for such implementations
|
3
|
+
# as an API-accessing library that wants to fake robust objects
|
4
|
+
# without the overhead of actually doing so. Think of it as OpenStruct
|
5
|
+
# with some additional goodies.
|
6
|
+
#
|
7
|
+
# == Basic Example
|
8
|
+
#
|
9
|
+
# mash = Mash.new
|
10
|
+
# mash.name? # => false
|
11
|
+
# mash.name = "Bob"
|
12
|
+
# mash.name # => "Bob"
|
13
|
+
# mash.name? # => true
|
14
|
+
#
|
15
|
+
# == Hash Conversion Example
|
16
|
+
#
|
17
|
+
# hash = {:a => {:b => 23, :d => {:e => "abc"}}, :f => [{:g => 44, :h => 29}, 12]}
|
18
|
+
# mash = Mash.new(hash)
|
19
|
+
# mash.a.b # => 23
|
20
|
+
# mash.a.d.e # => "abc"
|
21
|
+
# mash.f.first.g # => 44
|
22
|
+
# mash.f.last # => 12
|
23
|
+
#
|
24
|
+
class Mash < Hash
|
25
|
+
VERSION = '0.0.1'
|
26
|
+
|
27
|
+
# If you pass in an existing hash, it will
|
28
|
+
# convert it to a Mash including recursively
|
29
|
+
# descending into arrays and hashes, converting
|
30
|
+
# them as well.
|
31
|
+
def initialize(source_hash = nil)
|
32
|
+
mash_a_hash(source_hash) if source_hash
|
33
|
+
super(nil)
|
34
|
+
end
|
35
|
+
|
36
|
+
def id #:nodoc:
|
37
|
+
self["id"] ? self["id"] : super
|
38
|
+
end
|
39
|
+
|
40
|
+
def [](key) #:nodoc:
|
41
|
+
key = key.to_s
|
42
|
+
return Mash.new unless key?(key)
|
43
|
+
super
|
44
|
+
end
|
45
|
+
|
46
|
+
def []=(key,value) #:nodoc:
|
47
|
+
key = key.to_s
|
48
|
+
super
|
49
|
+
end
|
50
|
+
|
51
|
+
# Prints out a pretty object-like string of the
|
52
|
+
# defined attributes.
|
53
|
+
def inspect
|
54
|
+
ret = "<#{self.class.to_s}"
|
55
|
+
keys.sort.each do |key|
|
56
|
+
ret << " #{key}=#{self[key].inspect}"
|
57
|
+
end
|
58
|
+
ret << ">"
|
59
|
+
ret
|
60
|
+
end
|
61
|
+
|
62
|
+
alias :to_s :inspect
|
63
|
+
|
64
|
+
def method_missing(method_name, *args) #:nodoc:
|
65
|
+
if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1
|
66
|
+
self[match[1]] = args.first
|
67
|
+
elsif (match = method_name.to_s.match(/(.*)\?$/)) && args.size == 0
|
68
|
+
key?(match[1])
|
69
|
+
elsif keys.include?(method_name.to_s)
|
70
|
+
self[method_name]
|
71
|
+
elsif match = method_name.to_s.match(/^([a-z][a-z0-9A-Z_]+)$/)
|
72
|
+
Mash.new
|
73
|
+
else
|
74
|
+
super
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def mash_a_hash(hash) #:nodoc:
|
81
|
+
hash.each do |k,v|
|
82
|
+
case v
|
83
|
+
when Hash
|
84
|
+
v = Mash.new(v) if v.is_a?(Hash)
|
85
|
+
when Array
|
86
|
+
v = collect_mashed_hashes_in(v) if v.is_a?(Array)
|
87
|
+
end
|
88
|
+
|
89
|
+
self[k] = v
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def collect_mashed_hashes_in(array) #:nodoc:
|
94
|
+
array.collect do |value|
|
95
|
+
case value
|
96
|
+
when Hash
|
97
|
+
Mash.new(value)
|
98
|
+
when Array
|
99
|
+
collect_mashed_hashes_in(value)
|
100
|
+
else
|
101
|
+
value
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
data/spec/mash_spec.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),"..","lib","mash")
|
2
|
+
require File.join(File.dirname(__FILE__),"spec_helper")
|
3
|
+
|
4
|
+
describe Mash do
|
5
|
+
before(:each) do
|
6
|
+
@mash = Mash.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should inherit from hash" do
|
10
|
+
@mash.is_a?(Hash).should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should be able to set hash values through method= calls" do
|
14
|
+
@mash.test = "abc"
|
15
|
+
@mash["test"].should == "abc"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be able to retrieve set values through method calls" do
|
19
|
+
@mash["test"] = "abc"
|
20
|
+
@mash.test.should == "abc"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should test for already set values when passed a ? method" do
|
24
|
+
@mash.test?.should be_false
|
25
|
+
@mash.test = "abc"
|
26
|
+
@mash.test?.should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should make all [] and []= into strings for consistency" do
|
30
|
+
@mash["abc"] = 123
|
31
|
+
@mash.key?('abc').should be_true
|
32
|
+
@mash["abc"].should == 123
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should have a to_s that is identical to its inspect" do
|
36
|
+
@mash.abc = 123
|
37
|
+
@mash.to_s.should == @mash.inspect
|
38
|
+
end
|
39
|
+
|
40
|
+
context "#initialize" do
|
41
|
+
it "should convert an existing hash to a Mash" do
|
42
|
+
converted = Mash.new({:abc => 123, :name => "Bob"})
|
43
|
+
converted.abc.should == 123
|
44
|
+
converted.name.should == "Bob"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should convert hashes recursively into mashes" do
|
48
|
+
converted = Mash.new({:a => {:b => 1, :c => {:d => 23}}})
|
49
|
+
converted.a.is_a?(Mash).should be_true
|
50
|
+
converted.a.b.should == 1
|
51
|
+
converted.a.c.d.should == 23
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should convert hashes in arrays into mashes" do
|
55
|
+
converted = Mash.new({:a => [{:b => 12}, 23]})
|
56
|
+
converted.a.first.b.should == 12
|
57
|
+
converted.a.last.should == 23
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),"..","lib","mash")
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Bleigh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-04-12 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.5.1
|
23
|
+
version:
|
24
|
+
description: Mash is an extended Hash that gives simple pseudo-object functionality that can be built from hashes and easily extended. It is designed to be used in RESTful API libraries to provide easy object-like access to JSON and XML parsed hashes.
|
25
|
+
email:
|
26
|
+
- michael@intridea.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- History.txt
|
33
|
+
- Manifest.txt
|
34
|
+
- README.txt
|
35
|
+
files:
|
36
|
+
- History.txt
|
37
|
+
- Manifest.txt
|
38
|
+
- README.txt
|
39
|
+
- Rakefile
|
40
|
+
- lib/mash.rb
|
41
|
+
- spec/mash_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/mbleigh/mash
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --main
|
48
|
+
- README.txt
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: mash-hash
|
66
|
+
rubygems_version: 1.1.0
|
67
|
+
signing_key:
|
68
|
+
specification_version: 2
|
69
|
+
summary: Mash is an extended Hash that gives simple pseudo-object functionality that can be built from hashes and easily extended
|
70
|
+
test_files: []
|
71
|
+
|