Class: Dottie::Freckle

Inherits:
Object
  • Object
show all
Includes:
Methods
Defined in:
lib/dottie/freckle.rb

Instance Method Summary collapse

Methods included from Methods

#[], #[]=, #delete, #dottie_flatten, #dottie_keys, #fetch, #has_key?

Constructor Details

#initialize(obj) ⇒ Freckle

Creates a new Freckle to wrap the supplied object.



10
11
12
13
14
15
16
17
# File 'lib/dottie/freckle.rb', line 10

def initialize(obj)
  case obj
  when Hash, Array
    @_wrapped_object = obj
  else
    raise TypeError, 'must be a Hash or Array'
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



51
52
53
# File 'lib/dottie/freckle.rb', line 51

def method_missing(method, *args)
  wrapped_object.send(method, *args)
end

Instance Method Details

#arrayObject

Returns the wrapped Array, and raises an error if the wrapped object is not an Array.



31
32
33
# File 'lib/dottie/freckle.rb', line 31

def array
  wrapped_object(Array)
end

#hashObject

Returns the wrapped Hash, and raises an error if the wrapped object is not a Hash.



23
24
25
# File 'lib/dottie/freckle.rb', line 23

def hash
  wrapped_object(Hash)
end

#inspectObject



47
48
49
# File 'lib/dottie/freckle.rb', line 47

def inspect
  "<Dottie::Freckle #{wrapped_object.inspect}>"
end

#wrapped_object(type = nil) ⇒ Object

Returns the wrapped object, and raises an error if a type class is provided and the wrapped object is not of that type.



39
40
41
42
43
44
45
# File 'lib/dottie/freckle.rb', line 39

def wrapped_object(type = nil)
  if type.nil? || @_wrapped_object.is_a?(type)
    @_wrapped_object
  else
    raise TypeError.new("expected #{type.name} but got #{@_wrapped_object.class.name}")
  end
end