Class: HashyObject

Inherits:
Object
  • Object
show all
Defined in:
lib/hashy_object.rb,
lib/hashy_object/version.rb

Defined Under Namespace

Classes: TooMuchRecursionException

Constant Summary collapse

MAX_RECURSION_LEVELS =
100
VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, recursion_level = 0) ⇒ HashyObject

Returns a new instance of HashyObject.



11
12
13
14
15
# File 'lib/hashy_object.rb', line 11

def initialize(obj, recursion_level = 0)
  raise TooMuchRecursionException if recursion_level > MAX_RECURSION_LEVELS
  self.obj = obj
  self.recursion_level = recursion_level
end

Instance Attribute Details

#objObject

Returns the value of attribute obj.



8
9
10
# File 'lib/hashy_object.rb', line 8

def obj
  @obj
end

#recursion_levelObject

Returns the value of attribute recursion_level.



9
10
11
# File 'lib/hashy_object.rb', line 9

def recursion_level
  @recursion_level
end

Instance Method Details

#inspectObject



17
18
19
20
21
# File 'lib/hashy_object.rb', line 17

def inspect
  return obj if obj.is_a?(String)
  return obj.inspect if inspectable?
  inspectify
end

#inspectable?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/hashy_object.rb', line 23

def inspectable?
  return true if obj.is_a?(String)
  inspected = obj.inspect
  return false if inspected.match /#</
  true
end

#inspectifyObject



30
31
32
33
34
35
# File 'lib/hashy_object.rb', line 30

def inspectify
  return handle_hashy_array(obj).inspect if obj.class == Array
  return handle_hashy_hash(obj).inspect if obj.class == Hash
  return handle_hashy_to_hash(obj).inspect if obj.respond_to?(:to_hash)
  return handle_hashy_instance_variables(obj).inspect
end