hash_to_obj 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/hash_to_obj.rb +13 -2
- data/lib/hash_to_obj/version.rb +1 -1
- data/test/test_hash_to_obj.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 524c67f8c329330b7d28f23df06ad2c373c098c4
|
4
|
+
data.tar.gz: c825df69814a65af67c71be62becf8866ee50c38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea06ff41465b64bdf71572512453a7aa1bd4257ee4b935e9e61afcae5d1d3b45481610bef53bcb96970acdb61ab7fa9212d7463f35d81b4d794ac3386850f0fb
|
7
|
+
data.tar.gz: aa4c2a5ee5095e867d30349230d0103e501e3518d2acf8bfbe679e8235e1db57ba145681c14ca95e952c1e56e5e23b967eae921eeea04532685be89223599838
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# HashToObj
|
2
2
|
|
3
|
-
The hash_to_object gem converts a hash into a
|
3
|
+
The hash_to_object gem converts a hash into a Class Object. For example:
|
4
4
|
|
5
5
|
```ruby
|
6
6
|
h = { :title => 'Hello world!' }.to_obj
|
7
7
|
p h.title
|
8
|
-
|
8
|
+
# => "Hello world!"
|
9
9
|
```
|
10
10
|
|
11
11
|
## Installation
|
@@ -24,12 +24,12 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
27
|
-
Using
|
27
|
+
Using hash_to_object is very simple. Just call the `to_obj` method on any valid hash.
|
28
28
|
|
29
29
|
```ruby
|
30
30
|
h = { :title => 'Hello world!' }.to_obj
|
31
31
|
p h.title
|
32
|
-
|
32
|
+
# => "Hello world!"
|
33
33
|
```
|
34
34
|
|
35
35
|
## Contributing
|
data/lib/hash_to_obj.rb
CHANGED
@@ -1,13 +1,24 @@
|
|
1
1
|
require "hash_to_obj/version"
|
2
2
|
|
3
3
|
class Hash
|
4
|
+
# Convert a Hash to a Class object. This method accepts multi-dimensional hashes.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
# >> myhash = { :title => "hello world" }.to_obj
|
8
|
+
# >> myhash.title
|
9
|
+
# => "hello world"
|
4
10
|
def to_obj
|
5
11
|
hash = self
|
6
12
|
klass = Class.new
|
7
13
|
hash.each do |key, val|
|
8
|
-
klass.instance_variable_set("@#{key}", val)
|
9
|
-
klass.send(:define_method, key, proc { klass.instance_variable_get("@#{key}") })
|
10
14
|
klass.send(:define_method, "#{key}=", proc { |val| klass.instance_variable_set("@#{key}", val) })
|
15
|
+
klass.send(:define_method, key, proc { klass.instance_variable_get("@#{key}") })
|
16
|
+
|
17
|
+
unless val.instance_of? Hash
|
18
|
+
klass.instance_variable_set("@#{key}", val)
|
19
|
+
else
|
20
|
+
klass.instance_variable_set("@#{key}", val.to_obj)
|
21
|
+
end
|
11
22
|
end
|
12
23
|
|
13
24
|
klass.new
|
data/lib/hash_to_obj/version.rb
CHANGED
data/test/test_hash_to_obj.rb
CHANGED
@@ -6,5 +6,10 @@ class HashToObjTest < Test::Unit::TestCase
|
|
6
6
|
o = { :title => 'Hello world!' }.to_obj
|
7
7
|
assert_equal "Hello world!", o.title
|
8
8
|
end
|
9
|
+
def test_convert_multidimensional
|
10
|
+
h = { :title => 'Hello world!', :multi => { :dimensional => 'array' } }.to_obj
|
11
|
+
assert_equal "Hello world!", h.title
|
12
|
+
assert_equal "array", h.multi.dimensional
|
13
|
+
end
|
9
14
|
end
|
10
15
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_to_obj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Chittenden
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|