Class: ConvenientService::Utils::Object::ClampClass Private

Inherits:
Support::Command show all
Defined in:
lib/convenient_service/utils/object/clamp_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.

Note:

Name is inspired by Comparable#clamp.

Returns class when object is a class, module when object is a module or object's class.

Examples:

Aloid if condition for ConvenientService::Core::Concern::ClassMethod#middlewares.


service = Service.new # || Service

if service.is_a?(Module)
  service.middlewares(:result)
else
  service.class.middelewares(:result)
end

##
# With `ConvenientService::Utils::Object::clamp_class` it can be rewritten in the followiing way:
#
ConvenientService::Utils::Object::clamp_class(service).middlewares(:result)

Possible returns values.


module Musician
end

class Person
end

person = Person.new

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

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

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

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

ConvenientService::Utils::Object::ClampClass.call(Musician)
# => Musician

See Also:

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(object) ⇒ void

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.

Parameters:

Since:

  • 1.0.0



72
73
74
# File 'lib/convenient_service/utils/object/clamp_class.rb', line 72

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



66
67
68
# File 'lib/convenient_service/utils/object/clamp_class.rb', line 66

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



79
80
81
# File 'lib/convenient_service/utils/object/clamp_class.rb', line 79

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