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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2067c5adf416d694c6e225f0bd15cf81349d4df8
4
- data.tar.gz: bf089acfaa81ab155eccb33da5979390b263b95a
3
+ metadata.gz: 524c67f8c329330b7d28f23df06ad2c373c098c4
4
+ data.tar.gz: c825df69814a65af67c71be62becf8866ee50c38
5
5
  SHA512:
6
- metadata.gz: cc98ea17e5a3b74c51c4115433b15333f1a634bbf5f7f22ec715f661e44fba0576264b4c1120ad337a4f51e0748bcbb0808dde9a02321ba2a82a6640f24f6c4a
7
- data.tar.gz: 0bf4d8a19d393706accb92ff00cd91bf8b344a96e1c006bde03b83afda37064afa83f57f4e27207f61679a900faff750ecaa29a07b6135e2d930d96f56b721cb
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 class Object. For example:
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
- #> "Hello world!"
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 h_t_o is very simple. Just call the `to_obj` method on any class.
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
- #> "Hello world!"
32
+ # => "Hello world!"
33
33
  ```
34
34
 
35
35
  ## Contributing
@@ -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
@@ -1,3 +1,3 @@
1
1
  module HashToObj
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -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.1
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-18 00:00:00.000000000 Z
11
+ date: 2013-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler