mashed 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +1 -0
- data/lib/mashed/mash.rb +102 -0
- data/lib/mashed/stringy_hash.rb +46 -0
- data/lib/mashed/version.rb +3 -0
- data/lib/mashed.rb +4 -0
- data/mashed.gemspec +24 -0
- data/spec/mashed/mash_spec.rb +80 -0
- data/spec/mashed/stringy_hash_spec.rb +9 -0
- data/spec/spec_helper.rb +18 -0
- metadata +113 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3-p385
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 myobie
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Mashed
|
2
|
+
|
3
|
+
A Mash.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mashed'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mashed
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
Mash = Mashed::Mash
|
23
|
+
|
24
|
+
m = Mash.new(title: "Hello", starred: false, completed_at: nil)
|
25
|
+
|
26
|
+
m.title # => "Hello"
|
27
|
+
m.methods # => ["title", "starred", "completed_at"]
|
28
|
+
|
29
|
+
tasks = Api(:tasks, :v1).get("/tasks", list_id: "inbox").json.map { |hash| Mash.new(hash) }
|
30
|
+
|
31
|
+
tasks.each do |task|
|
32
|
+
puts tasks.title
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/mashed/mash.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
module Mashed
|
2
|
+
class Mash < BasicObject
|
3
|
+
def singleton_method_added(symbol)
|
4
|
+
@singleton_methods ||= []
|
5
|
+
@singleton_methods << symbol
|
6
|
+
end
|
7
|
+
def singleton_method_removed(symbol)
|
8
|
+
@singleton_methods ||= []
|
9
|
+
@singleton_methods.delete symbol
|
10
|
+
end
|
11
|
+
def singleton_method_undefined(symbol)
|
12
|
+
singleton_method_removed(symbol)
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_hash
|
16
|
+
@hash.to_hash
|
17
|
+
end
|
18
|
+
alias to_h to_hash
|
19
|
+
|
20
|
+
def to_json(*args)
|
21
|
+
to_hash.to_json(*args)
|
22
|
+
end
|
23
|
+
|
24
|
+
def puts(*args)
|
25
|
+
::Kernel.puts(*args)
|
26
|
+
end
|
27
|
+
|
28
|
+
klass = self
|
29
|
+
define_method(:class) { klass }
|
30
|
+
define_method(:methods) { klass.instance_methods + @singleton_methods + keys }
|
31
|
+
|
32
|
+
def self.name
|
33
|
+
"Mashed::Mash"
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(hash)
|
37
|
+
@singleton_methods ||= []
|
38
|
+
hash = if hash.respond_to?(:to_h)
|
39
|
+
hash.to_h
|
40
|
+
else
|
41
|
+
hash.to_hash
|
42
|
+
end
|
43
|
+
@hash = hash.stringify
|
44
|
+
end
|
45
|
+
|
46
|
+
def is_a?(other)
|
47
|
+
other == self.class
|
48
|
+
end
|
49
|
+
alias kind_of? is_a?
|
50
|
+
|
51
|
+
def [](key)
|
52
|
+
key = key.to_s
|
53
|
+
wrap_up @hash[key]
|
54
|
+
end
|
55
|
+
|
56
|
+
def keys
|
57
|
+
@hash.keys
|
58
|
+
end
|
59
|
+
|
60
|
+
def delete(key)
|
61
|
+
wrap_up @hash.delete(key)
|
62
|
+
end
|
63
|
+
|
64
|
+
def method_missing(symbol, *args, &blk)
|
65
|
+
string = symbol.to_s
|
66
|
+
if @hash.key?(string)
|
67
|
+
self[string]
|
68
|
+
elsif string =~ /\?$/
|
69
|
+
!!self[string[0..-2]]
|
70
|
+
else
|
71
|
+
nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def respond_to?(symbol)
|
76
|
+
methods.map(&:to_s).include?(symbol.to_s)
|
77
|
+
end
|
78
|
+
|
79
|
+
def inspect
|
80
|
+
"#<Mashed::Mash @hash=>#{@hash.inspect}>"
|
81
|
+
end
|
82
|
+
alias to_s inspect
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def wrap_up(thing)
|
87
|
+
if thing.respond_to?(:to_ary)
|
88
|
+
thing.map { |t| wrap(t) }
|
89
|
+
else
|
90
|
+
wrap(thing)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def wrap(thing)
|
95
|
+
if thing.respond_to?(:to_hash)
|
96
|
+
self.class.new thing
|
97
|
+
else
|
98
|
+
thing
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module Mashed
|
4
|
+
module ExtendHash
|
5
|
+
def stringify
|
6
|
+
StringyHash.new(dup.each_with_object({}) { |(k,v), h| h[k.to_s] = v })
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class StringyHash < SimpleDelegator
|
11
|
+
def stringify
|
12
|
+
dup
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](key)
|
16
|
+
super(key.to_s)
|
17
|
+
end
|
18
|
+
|
19
|
+
def []=(key, value)
|
20
|
+
super(key.to_s, value)
|
21
|
+
end
|
22
|
+
alias store []=
|
23
|
+
|
24
|
+
def delete(key, &blk)
|
25
|
+
super(key.to_s, &blk)
|
26
|
+
end
|
27
|
+
|
28
|
+
def merge(other_hash, &blk)
|
29
|
+
super(other_hash.stringify, &blk)
|
30
|
+
end
|
31
|
+
|
32
|
+
def merge!(other_hash, &blk)
|
33
|
+
super(other_hash.stringify, &blk)
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace(other_hash, &blk)
|
37
|
+
super(other_hash.stringify, &blk)
|
38
|
+
end
|
39
|
+
|
40
|
+
def update(other_hash, &blk)
|
41
|
+
super(other_hash.stringify, &blk)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Hash.send :include, Mashed::ExtendHash
|
data/lib/mashed.rb
ADDED
data/mashed.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mashed/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mashed"
|
8
|
+
spec.version = Mashed::VERSION
|
9
|
+
spec.authors = ["myobie"]
|
10
|
+
spec.email = ["me@nathanherald.com"]
|
11
|
+
spec.description = %q{A Mash}
|
12
|
+
spec.summary = %q{A Mash which is an object that uses method_missing to provide methods for a Hash's keys.}
|
13
|
+
spec.homepage = "http://github.com/6wunderkinder/mashed"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mashed::Mash do
|
4
|
+
let(:mash) { Mashed::Mash.new(a: 1, b: 2, c: 3) }
|
5
|
+
|
6
|
+
describe "#to_hash" do
|
7
|
+
it { expect(mash.to_hash).to eq("a" => 1, "b" => 2, "c" => 3) }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#class" do
|
11
|
+
it { expect(mash.class).to be(Mashed::Mash) }
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#keys" do
|
15
|
+
it { expect(mash.keys).to eq(["a", "b", "c"]) }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#delete" do
|
19
|
+
before { mash.delete(:a) }
|
20
|
+
it { expect(mash.keys).to eq(["b", "c"]) }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#[]" do
|
24
|
+
it { expect(mash[:a]).to eq(1) }
|
25
|
+
it { expect(mash["b"]).to eq(2) }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#method_missing" do
|
29
|
+
it {
|
30
|
+
expect(mash.a).to eq(1)
|
31
|
+
expect(mash.b).to eq(2)
|
32
|
+
expect(mash.c).to eq(3)
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#methods" do
|
37
|
+
it { expect(mash.methods).to include(:__send__) }
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "respond_to?" do
|
41
|
+
it { expect(mash.respond_to?(:a)).to be_true }
|
42
|
+
it { expect(mash.respond_to?(:[])).to be_true }
|
43
|
+
it { expect(mash.respond_to?(:to_hash)).to be_true }
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "wrapping itself" do
|
47
|
+
let(:incepted) { Mashed::Mash.new(mash) }
|
48
|
+
it { expect(incepted.to_hash).to be_a(Hash) }
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "nested hashes" do
|
52
|
+
let(:nested) { Mashed::Mash.new(inside: { of: 'you' }) }
|
53
|
+
it { expect(nested.inside).to be_a(Mashed::Mash) }
|
54
|
+
it { expect(nested.inside.of).to eq('you') }
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "query methods should return booleans" do
|
58
|
+
let(:grumpy) { Mashed::Mash.new(yes: "hooray", no: nil) }
|
59
|
+
it { expect(grumpy.yes?).to be(true) }
|
60
|
+
it { expect(grumpy.yes?).to_not eq("hooray") }
|
61
|
+
it { expect(grumpy.no?).to be(false) }
|
62
|
+
it { expect(grumpy.no?).to_not be_nil }
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "singleton methods" do
|
66
|
+
before {
|
67
|
+
def mash.something; end
|
68
|
+
}
|
69
|
+
it { expect(mash.methods).to include(:something) }
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "#to_json" do
|
73
|
+
it {
|
74
|
+
m = double
|
75
|
+
expect(mash).to receive(:to_hash).and_return(m)
|
76
|
+
expect(m).to receive(:to_json)
|
77
|
+
mash.to_json
|
78
|
+
}
|
79
|
+
end
|
80
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
$: << File.expand_path("../../lib", __FILE__)
|
2
|
+
|
3
|
+
require "rspec/autorun"
|
4
|
+
require "mashed"
|
5
|
+
|
6
|
+
puts "woooo"
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
config.filter_run :focus
|
12
|
+
|
13
|
+
# Run specs in random order to surface order dependencies. If you find an
|
14
|
+
# order dependency and want to debug it, you can fix the order by providing
|
15
|
+
# the seed, which is printed after each run.
|
16
|
+
# --seed 1234
|
17
|
+
config.order = 'random'
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mashed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- myobie
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A Mash
|
63
|
+
email:
|
64
|
+
- me@nathanherald.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- .ruby-version
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/mashed.rb
|
77
|
+
- lib/mashed/mash.rb
|
78
|
+
- lib/mashed/stringy_hash.rb
|
79
|
+
- lib/mashed/version.rb
|
80
|
+
- mashed.gemspec
|
81
|
+
- spec/mashed/mash_spec.rb
|
82
|
+
- spec/mashed/stringy_hash_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
homepage: http://github.com/6wunderkinder/mashed
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 1.8.23
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: A Mash which is an object that uses method_missing to provide methods for
|
109
|
+
a Hash's keys.
|
110
|
+
test_files:
|
111
|
+
- spec/mashed/mash_spec.rb
|
112
|
+
- spec/mashed/stringy_hash_spec.rb
|
113
|
+
- spec/spec_helper.rb
|