Class: ConvenientService::Service::Plugins::CanHaveStepAwareEnumerables::Commands::CastStepAwareEnumerator

Inherits:
ConvenientService::Support::Command show all
Defined in:
lib/convenient_service/service/plugins/can_have_step_aware_enumerables/commands/cast_step_aware_enumerator.rb

Constant Summary collapse

RUBY_CLASS_TO_STEP_AWARE_CLASS_MAP =

Returns:

  • (Hash{Class => Class})
{
  ::Enumerator => Entities::StepAwareEnumerables::Enumerator,
  ::Enumerator::Lazy => Entities::StepAwareEnumerables::LazyEnumerator,
  ::Enumerator::Chain => Entities::StepAwareEnumerables::ChainEnumerator,
  ::Enumerator::ArithmeticSequence => Entities::StepAwareEnumerables::ArithmeticSequenceEnumerator
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConvenientService::Support::Command

[], call

Constructor Details

#initialize(object:, organizer:, propagated_result: nil) ⇒ void

Parameters:



48
49
50
51
52
# File 'lib/convenient_service/service/plugins/can_have_step_aware_enumerables/commands/cast_step_aware_enumerator.rb', line 48

def initialize(object:, organizer:, propagated_result: nil)
  @object = object
  @organizer = organizer
  @propagated_result = propagated_result
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



28
29
30
# File 'lib/convenient_service/service/plugins/can_have_step_aware_enumerables/commands/cast_step_aware_enumerator.rb', line 28

def object
  @object
end

#organizerObject (readonly)

Returns the value of attribute organizer.



34
35
36
# File 'lib/convenient_service/service/plugins/can_have_step_aware_enumerables/commands/cast_step_aware_enumerator.rb', line 34

def organizer
  @organizer
end

#propagated_resultObject (readonly)

Returns the value of attribute propagated_result.



40
41
42
# File 'lib/convenient_service/service/plugins/can_have_step_aware_enumerables/commands/cast_step_aware_enumerator.rb', line 40

def propagated_result
  @propagated_result
end

Instance Method Details

#callConvenientService::Service::Plugins::CanHaveStepAwareEnumerables::Entities::StepAwareEnumerables::Base



58
59
60
61
62
63
# File 'lib/convenient_service/service/plugins/can_have_step_aware_enumerables/commands/cast_step_aware_enumerator.rb', line 58

def call
  return step_aware_enumerator_klass.new(object: object, organizer: organizer, propagated_result: propagated_result) if step_aware_enumerator_klass
  return fallback_step_aware_enumerator_klass.new(object: object, organizer: organizer, propagated_result: propagated_result) if fallback_step_aware_enumerator_klass

  raise ::ConvenientService.raise Exceptions::ObjectIsNotEnumerator.new(object: object)
end

#fallback_step_aware_enumerator_klassClass?

Returns:

  • (Class, nil)


75
76
77
# File 'lib/convenient_service/service/plugins/can_have_step_aware_enumerables/commands/cast_step_aware_enumerator.rb', line 75

def fallback_step_aware_enumerator_klass
  Utils.memoize_including_falsy_values(self, :@fallback_step_aware_enumerator_klass) { Entities::StepAwareEnumerables::Enumerator if object.respond_to?(:each) }
end

#step_aware_enumerator_klassClass?

Returns:

  • (Class, nil)


68
69
70
# File 'lib/convenient_service/service/plugins/can_have_step_aware_enumerables/commands/cast_step_aware_enumerator.rb', line 68

def step_aware_enumerator_klass
  Utils.memoize_including_falsy_values(self, :@step_aware_enumerator_klass) { RUBY_CLASS_TO_STEP_AWARE_CLASS_MAP[object.class] }
end