Class: Object

Inherits:
BasicObject
Defined in:
lib/upcastable/core_ext/object/upcasting.rb

Instance Method Summary collapse

Instance Method Details

#downcastObject

Downcasts the object to the original class

Returns:

  • the downcasted object (the original object)



53
54
55
# File 'lib/upcastable/core_ext/object/upcasting.rb', line 53

def downcast
  self
end

#upcast_to(ancestor) ⇒ Object

Upcasts the object to the specified class or module. Internally, the upcasted object is a Upcastable::UpcastedObject instance which delegates almost all methods to the original object.

Examples:


module Animal
  def talk; end
end

class Cat
  include Animal

  def talk
    'Meow!'
  end

  def run
    'Running...'
  end
end

animal = Cat.new.upcast_to(Animal)
animal.class #=> Cat
animal.talk  #=> "Meow!"
animal.run   #=> NoMethodError: `run' is not defined in Animal

Parameters:

  • ancestor (Class, Module)

    the ancestor to which the object is upcasted

Returns:

  • the upcasted object

Raises:

  • (ArgumentError)

    if ancestor is not an ancestor



34
35
36
# File 'lib/upcastable/core_ext/object/upcasting.rb', line 34

def upcast_to(ancestor)
  ::Upcastable::UpcastedObject.new(self, ancestor)
end

#upcasted?true, false

Returns true if the object is upcasted

Returns:

  • (true, false)


46
47
48
# File 'lib/upcastable/core_ext/object/upcasting.rb', line 46

def upcasted?
  false
end

#upcastingClass, ...

Returns the ancestor to which the object have been upcasted.

Returns:

  • (Class, Module, nil)

    the ancestor to which the object have been upcasted



39
40
41
# File 'lib/upcastable/core_ext/object/upcasting.rb', line 39

def upcasting
  nil
end