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.



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

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



49
50
51
# File 'lib/dottie/freckle.rb', line 49

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.



29
30
31
# File 'lib/dottie/freckle.rb', line 29

def array
  wrapped_object(Array)
end

#hashObject

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



21
22
23
# File 'lib/dottie/freckle.rb', line 21

def hash
  wrapped_object(Hash)
end

#inspectObject



45
46
47
# File 'lib/dottie/freckle.rb', line 45

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.



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

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