mostash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +0 -0
- data/Rakefile +23 -0
- data/VERSION +1 -0
- data/lib/mostash.rb +8 -0
- data/lib/mostash/mostash.rb +43 -0
- data/spec/mostash_spec.rb +63 -0
- data/spec/spec_helper.rb +4 -0
- metadata +80 -0
data/README.markdown
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gemspec|
|
7
|
+
gemspec.name = "mostash"
|
8
|
+
gemspec.summary = "A combo of OpenStruct and a ruby hash"
|
9
|
+
gemspec.description = "You can treat an object as either a hash or as an OpenStruct. In additon to this you can create them nested, unlike OpenStruct"
|
10
|
+
gemspec.email = "asher.friedman@gmail.com"
|
11
|
+
#gemspec.homepage = "http://github.com/technicalpickles/the-perfect-gem"
|
12
|
+
gemspec.authors = ["Joel Friedman"]
|
13
|
+
|
14
|
+
gemspec.add_development_dependency "rspec"
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "run all specs"
|
21
|
+
Spec::Rake::SpecTask.new( "spec" ) do |t|
|
22
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
23
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/mostash.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
class MoStash < OpenStruct
|
2
|
+
def initialize(init={})
|
3
|
+
super({})
|
4
|
+
__init__ init
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_missing(method_name, *args)
|
8
|
+
#dbg "#{method_name} was sent #{args.inspect}"
|
9
|
+
if __is_setter__( method_name )
|
10
|
+
super method_name, __adjusted_value__( args.first )
|
11
|
+
else
|
12
|
+
super
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def []=(key, value)
|
17
|
+
self.send "#{key.to_s}=", value
|
18
|
+
end
|
19
|
+
|
20
|
+
def [](key)
|
21
|
+
self.send "#{key.to_s}"
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def __init__(hash)
|
27
|
+
hash.each_pair do |key, value|
|
28
|
+
self.send "#{key.to_s}=", __adjusted_value__( value )
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def __adjusted_value__(value)
|
33
|
+
case value
|
34
|
+
when Hash then MoStash.new( value )
|
35
|
+
when Array then value.map{ |v| __adjusted_value__( v ) }
|
36
|
+
else value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def __is_setter__(method_name)
|
41
|
+
method_name.to_s =~ /=$/
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MoStash do
|
4
|
+
|
5
|
+
it "should act like basic OpenStruct" do
|
6
|
+
mo = MoStash.new
|
7
|
+
mo.foo = "bar"
|
8
|
+
|
9
|
+
mo.foo.should == "bar"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should allow basic Hash methods" do
|
13
|
+
mo = MoStash.new
|
14
|
+
mo.foo = "bar"
|
15
|
+
mo[:baz] = "foo"
|
16
|
+
|
17
|
+
mo[:foo].should == "bar"
|
18
|
+
mo.baz.should == "foo"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should be initializable via a Hash" do
|
22
|
+
mo = MoStash.new(:foo => "bar")
|
23
|
+
|
24
|
+
mo.foo.should == "bar"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be initializable via nested Hash" do
|
28
|
+
h = {:foo => {:bar => "baz"}, :oh => "hai"}
|
29
|
+
mo = MoStash.new h
|
30
|
+
|
31
|
+
mo.foo.bar.should == "baz"
|
32
|
+
mo.oh.should == "hai"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should allow values to be arrays" do
|
36
|
+
mo = MoStash.new
|
37
|
+
mo.foo = ['hello']
|
38
|
+
|
39
|
+
mo.foo.first.should == "hello"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should automatically make a hash a MoStash" do
|
43
|
+
mo = MoStash.new
|
44
|
+
mo.foo = {:bar => 'baz'}
|
45
|
+
|
46
|
+
mo.foo.bar.should == 'baz'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should correctly handle an array of hashes" do
|
50
|
+
mo = MoStash.new
|
51
|
+
mo.foo = [{:hey => 'you'}, {:oh => 'hai'}]
|
52
|
+
|
53
|
+
mo.foo[1].oh.should == 'hai'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should create method when new method called" do
|
57
|
+
mo = MoStash.new
|
58
|
+
mo.foo = "bar"
|
59
|
+
|
60
|
+
mo.methods.should include("foo")
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mostash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Joel Friedman
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-02 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
description: You can treat an object as either a hash or as an OpenStruct. In additon to this you can create them nested, unlike OpenStruct
|
33
|
+
email: asher.friedman@gmail.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README.markdown
|
40
|
+
files:
|
41
|
+
- README.markdown
|
42
|
+
- Rakefile
|
43
|
+
- VERSION
|
44
|
+
- lib/mostash.rb
|
45
|
+
- lib/mostash/mostash.rb
|
46
|
+
- spec/mostash_spec.rb
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage:
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.6
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: A combo of OpenStruct and a ruby hash
|
78
|
+
test_files:
|
79
|
+
- spec/mostash_spec.rb
|
80
|
+
- spec/spec_helper.rb
|