hash_key_as_attribute 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/README +57 -0
- data/lib/hash_key_as_attribute.rb +16 -0
- data/spec/lib/hash_key_as_attribute_spec.rb +38 -0
- metadata +56 -0
data/README
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
Author
|
2
|
+
====
|
3
|
+
Niranjan Sarade
|
4
|
+
|
5
|
+
About the utility - HashKeyAsAttribute
|
6
|
+
====
|
7
|
+
|
8
|
+
In ruby, we have OpenStruct which allows the creation of data objects with arbitrary attributes.
|
9
|
+
With ruby's metaprogramming capability, we can also allow hash values to be set and retrieved as if
|
10
|
+
they were its attributes. So setting and getting the value for the key would be treated as if they
|
11
|
+
were method calls on the hash object.
|
12
|
+
If the key does not correspond to any hash entry, it should return “The key does not correspond to any hash entry” message.
|
13
|
+
The hook that has been used is Kernel's method_missing.
|
14
|
+
|
15
|
+
Example
|
16
|
+
====
|
17
|
+
|
18
|
+
require 'hash_key_as_attribute'
|
19
|
+
|
20
|
+
h = {}
|
21
|
+
h.one = 1
|
22
|
+
puts h.one #=> 1
|
23
|
+
|
24
|
+
h.two= [1,2,3,4]
|
25
|
+
puts h.two.inspect #=> [1,2,3,4]
|
26
|
+
|
27
|
+
puts h.three #=> "The key does not correspond to any hash entry"
|
28
|
+
|
29
|
+
puts h.inspect #=> {:one=>1, :two=>[1, 2, 3, 4]}
|
30
|
+
|
31
|
+
h2 = {}
|
32
|
+
h2.four = 4
|
33
|
+
|
34
|
+
h.three = h2
|
35
|
+
|
36
|
+
puts h.three.inspect #=> {:four=>4}
|
37
|
+
|
38
|
+
puts h.three.four #=> 4
|
39
|
+
|
40
|
+
|
41
|
+
Install
|
42
|
+
====
|
43
|
+
gem install hash_key_as_attribute
|
44
|
+
|
45
|
+
(It has been pushed to http://gemcutter.org)
|
46
|
+
|
47
|
+
OR
|
48
|
+
|
49
|
+
Download the gem file from http://github.com/NiranjanSarade/hash_key_as_attribute.git/
|
50
|
+
gem install hash_key_as_attribute-0.0.1.gem
|
51
|
+
|
52
|
+
|
53
|
+
Uninstall
|
54
|
+
====
|
55
|
+
gem uninstall hash_key_as_attribute
|
56
|
+
|
57
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class HashKeyAsAttribute; end
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
def method_missing(symbol, *args)
|
5
|
+
key = symbol.to_s
|
6
|
+
if key.split('').last == "="
|
7
|
+
self[key.chop.to_sym] = args[0]
|
8
|
+
else
|
9
|
+
unless self[symbol]
|
10
|
+
"The key does not correspond to any hash entry"
|
11
|
+
else
|
12
|
+
self[symbol]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../../lib/hash_key_as_attribute")
|
2
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
3
|
+
|
4
|
+
describe HashKeyAsAttribute do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@myhash = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should set the value for the key as if the key were attribute of hash " do
|
11
|
+
@myhash.one = 1
|
12
|
+
@myhash[:one].should == 1
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should retrieve the value for the key as if the key were attribute of hash " do
|
16
|
+
@myhash.two = [1,2,3]
|
17
|
+
@myhash.two.size.should == 3
|
18
|
+
@myhash.two[1] == 2
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should give message if the key does not correspond to any hash entry" do
|
22
|
+
@myhash.three.should == "The key does not correspond to any hash entry"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should return specified default value if the key doesn‘t correspond to a hash entry" do
|
26
|
+
myhash2 = Hash.new("No hash entry!")
|
27
|
+
myhash2.three.should == "No hash entry!"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should allow nested hash value access as if they were attributes" do
|
31
|
+
@myhash.one = 1
|
32
|
+
myhash2 = {}
|
33
|
+
myhash2.two = 2
|
34
|
+
@myhash.two = myhash2
|
35
|
+
@myhash.two.two.should == 2
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_key_as_attribute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Niranjan Sarade
|
8
|
+
autorequire: hash_key_as_attribute
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-06-04 00:00:00 +05:30
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: nirusuma@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- ./README
|
24
|
+
files:
|
25
|
+
- lib/hash_key_as_attribute.rb
|
26
|
+
- ./README
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://github.com/NiranjanSarade/hash_key_as_attribute
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.5
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Allow hash values to be set and retrieved as if they were its attributes
|
55
|
+
test_files:
|
56
|
+
- spec/lib/hash_key_as_attribute_spec.rb
|