Class: ConvenientService::Utils::Object::DuckClass Private

Inherits:
Support::Command show all
Defined in:
lib/convenient_service/utils/object/duck_class.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Converts an object to a "duck" class in terms of Module#method_defined? or Module#define_method.

Examples:

Avoid if condition for Module#method_defined?.


object = Person.new # || Person

if object.is_a?(Module)
  ##
  # class Person
  #   def self.foo
  #   end
  # end
  #
  # Does `Person` has class method `foo`?
  #
  object.singleton_class.method_defined?(:foo)
else
  ##
  # class Person
  #   def foo
  #   end
  # end
  #
  # Does `Person` has instance method `foo`?
  #
  object.class.method_defined?(:foo)
end

##
# With `ConvenientService::Utils::Object.duck_class` it can be rewritten in the following way:
#
ConvenientService::Utils::Object.duck_class(object).method_defined?(:foo)

Avoid if condition for Module#define_method.


object = Person.new # || Person

if object.is_a?(Module)
  ##
  # Defines class method `foo` in `Person` class.
  #
  object.singleton_class.define_method(:foo) { :bar }
else
  ##
  # Defines instance method `foo` in `Person` class.
  #
  object.class.define_method(:foo) { :bar }
end

##
# With `ConvenientService::Utils::Object.duck_class` it can be rewritten in the following way:
#
ConvenientService::Utils::Object.duck_class(object).define_method(:foo) { :bar }

Possible return values.


module Musician
end

class Person
end

person = Person.new

ConvenientService::Utils::Object::DuckClass.call(42)
# => Integer

ConvenientService::Utils::Object::DuckClass.call("foo")
# => String

ConvenientService::Utils::Object::DuckClass.call(person)
# => Person

ConvenientService::Utils::Object::DuckClass.call(Person)
# => #<Class:Person>

ConvenientService::Utils::Object::DuckClass.call(Musician)
# => #<Class:Musician>

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(object) ⇒ DuckClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DuckClass.

Parameters:

  • object (Object)

    Can be any type.

Since:

  • 1.0.0



107
108
109
# File 'lib/convenient_service/utils/object/duck_class.rb', line 107

def initialize(object)
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



102
103
104
# File 'lib/convenient_service/utils/object/duck_class.rb', line 102

def object
  @object
end

Instance Method Details

#callClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 1.0.0



114
115
116
# File 'lib/convenient_service/utils/object/duck_class.rb', line 114

def call
  object.is_a?(::Module) ? object.singleton_class : object.class
end