hash_brown 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +37 -0
- data/lib/hash_brown.rb +9 -3
- data/lib/hash_brown/version.rb +1 -1
- data/spec/hash_brown_spec.rb +53 -2
- metadata +5 -4
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# hash_brown
|
2
|
+
|
3
|
+
Access your Hash elements with a method call!
|
4
|
+
|
5
|
+
If you're using strings or symbols as keys, save yourself a character by using hash_brown.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
$ gem install hash_brown
|
10
|
+
|
11
|
+
If you're using Bundler, add this line to your `Gemfile`:
|
12
|
+
|
13
|
+
gem 'hash_brown'
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
hash = { :key_sym => 'sym_val', 'key_str' => 'str_val' }
|
17
|
+
# => {:key_sym=>"sym_val", "key_str"=>"str_val"}
|
18
|
+
|
19
|
+
hash.key_sym # => "sym_val"
|
20
|
+
hash.key_str # => "str_val"
|
21
|
+
hash.key_str = 'new_val' # => "new_val"
|
22
|
+
hash.new_key = 'next_val' # => "next_val"
|
23
|
+
hash
|
24
|
+
# => {:key_sym=>"sym_val", "key_str"=>"new_val", :new_key=>"next_val"}
|
25
|
+
|
26
|
+
|
27
|
+
# WARNING! Modifies the behavior of Hash.
|
28
|
+
|
29
|
+
This is not a very useful gem. You probably don't want to use it.
|
30
|
+
|
31
|
+
Namespacing? Modules? Nope, just hijacking Hash's `method_missing`.
|
32
|
+
|
33
|
+
Behavior that I didn't intend is very likely. Please open an issue if you find some.
|
34
|
+
|
35
|
+
## Why?
|
36
|
+
|
37
|
+
My first gem. This is mostly an exercise in gem creation. High on education, low on utility.
|
data/lib/hash_brown.rb
CHANGED
@@ -2,8 +2,14 @@ require 'hash_brown/version'
|
|
2
2
|
|
3
3
|
class Hash
|
4
4
|
def method_missing meth, *args, &block
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
if meth[-1] == '='
|
6
|
+
key = meth[0..-2]
|
7
|
+
return self[key] = args[0] if key? key
|
8
|
+
return self[key.to_sym] = args[0]
|
9
|
+
else
|
10
|
+
return self[meth] if key? meth
|
11
|
+
return self[meth.to_s] if key? meth.to_s
|
12
|
+
super
|
13
|
+
end
|
8
14
|
end
|
9
15
|
end
|
data/lib/hash_brown/version.rb
CHANGED
data/spec/hash_brown_spec.rb
CHANGED
@@ -59,14 +59,65 @@ describe HashBrown do
|
|
59
59
|
|
60
60
|
context 'writer' do
|
61
61
|
|
62
|
-
|
62
|
+
it 'assigns a symbol' do
|
63
63
|
hash.key_sym = 'new_val'
|
64
|
+
|
64
65
|
hash.key_sym.should == 'new_val'
|
65
66
|
end
|
66
67
|
|
67
|
-
|
68
|
+
it 'assigns a string' do
|
68
69
|
hash.key_str = 'new_val'
|
70
|
+
|
69
71
|
hash.key_str.should == 'new_val'
|
72
|
+
hash.key_str.should == hash['key_str']
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'given comparable symbol and string keys' do
|
76
|
+
before { hash[key_value] = 'old_val' }
|
77
|
+
|
78
|
+
context 'no key exists' do
|
79
|
+
let(:key_value) { 'old_key' }
|
80
|
+
|
81
|
+
it 'assigns to symbol' do
|
82
|
+
hash.new_key = 'new_val'
|
83
|
+
|
84
|
+
hash.new_key.should == 'new_val'
|
85
|
+
hash.new_key.should == hash[:new_key]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'only symbol key exists' do
|
90
|
+
let(:key_value) { :key_sym }
|
91
|
+
|
92
|
+
it 'assigns to symbol' do
|
93
|
+
hash.key_sym = 'new_val'
|
94
|
+
|
95
|
+
hash.key_sym.should == 'new_val'
|
96
|
+
hash.key_sym.should == hash[key_value]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context 'only string key exists' do
|
101
|
+
let(:key_value) { 'key_str' }
|
102
|
+
|
103
|
+
it 'assigns to string' do
|
104
|
+
hash.key_str = 'new_val'
|
105
|
+
|
106
|
+
hash.key_str.should == 'new_val'
|
107
|
+
hash.key_str.should == hash[key_value]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'both symbol and string keys exist' do
|
112
|
+
let(:key_value) { :key_sym }
|
113
|
+
|
114
|
+
it 'assigns to symbol' do
|
115
|
+
hash.key_sym = 'new_val'
|
116
|
+
|
117
|
+
hash.key_sym.should == 'new_val'
|
118
|
+
hash.key_sym.should == hash[key_value]
|
119
|
+
end
|
120
|
+
end
|
70
121
|
end
|
71
122
|
end
|
72
123
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_brown
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-01-05 00:00:00.000000000 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
17
|
-
requirement: &
|
17
|
+
requirement: &2153250780 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,7 +22,7 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2153250780
|
26
26
|
description: ! "Now you don't need to fuss with brackets when using Hashes.\n Abuses
|
27
27
|
method_missing responsibly."
|
28
28
|
email:
|
@@ -33,6 +33,7 @@ extra_rdoc_files: []
|
|
33
33
|
files:
|
34
34
|
- .gitignore
|
35
35
|
- Gemfile
|
36
|
+
- README.md
|
36
37
|
- Rakefile
|
37
38
|
- hash_brown.gemspec
|
38
39
|
- lib/hash_brown.rb
|