hasherizer 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/hasherizer.rb +57 -0
  2. metadata +46 -0
@@ -0,0 +1,57 @@
1
+ # Makes a hash out of an object recursively.
2
+ module Hasherizer
3
+
4
+ # Extract instance variables names and values into a flat hash no matter how many levels deep your objects are.
5
+ #
6
+ # @param [Object, #read] -- Object which instance variables are to be converted to a hash.
7
+ # @return [Hash]/[Object] -- A hash or an object.
8
+ def self.to_hash(object)
9
+ # Check if object has hash behavior. Check before :each because hashes also respond to :each.
10
+ if object.respond_to? :to_hash
11
+ object
12
+ # Check if object has array behavior.
13
+ elsif object.respond_to? :each
14
+ # If all elements have hash behavior, return them in a flat hash.
15
+ if object.all? {|element| element.respond_to? :to_hash}
16
+ hash = {}
17
+ object.each {|element| hash.merge! element}
18
+ hash
19
+ # If all elements have instance variables, process each element.
20
+ elsif object.all? {|element| element.instance_variables.size > 0}
21
+ hash = {}
22
+ object.each {|element| hash.merge! to_hash(element)}
23
+ hash
24
+ # Otherwise return the object/value.
25
+ else
26
+ object
27
+ end
28
+ # If no hash nor array, its either an object with instance variables or a value.
29
+ else
30
+ # Get object's instance variables.
31
+ instance_vars = object.instance_variables.collect {|var| var[1..-1]}
32
+
33
+ # If no instance variables, return object/value.
34
+ if instance_vars.size.zero?
35
+ object
36
+ # If instance variables, process them.
37
+ else
38
+ hash = {}
39
+
40
+ instance_vars.each do |var|
41
+ resp = to_hash(object.instance_eval(var))
42
+
43
+ # If instance variable has hash behavior add it to the returned hash values.
44
+ if resp.respond_to? :to_hash
45
+ hash.merge! resp
46
+ # If instance variable does not have hash behavior, create hash entry for the variable name and its value.
47
+ else
48
+ hash.merge! var => resp
49
+ end
50
+ end
51
+
52
+ hash
53
+ end
54
+ end
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hasherizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pepe Hipolito
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Extract instance variables names and values into a flat hash no matter
15
+ how many levels deep your objects are.
16
+ email: pepe.hipolito@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/hasherizer.rb
22
+ homepage: https://github.com/pepehipolito/hasherizer
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.11
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Recursively convert object's instance variables to a flat hash.
46
+ test_files: []