Module: CommandInitializationMethods

Included in:
Inform::Command
Defined in:
lib/runtime/command.rb

Overview

The CommandInitializationMethods module

Constant Summary collapse

CommandNameTemplate =
'event:%<time>s'.freeze
CommandCauseIdentityTemplate =

rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength rubocop: enable Lint/LambdaWithoutLiteralBlock

'%<identity>s:%<name>s'.freeze
CommandSourceTemplate =
':%<source_location>s:%<source_line_number>s'.freeze
CommandSourceFullTemplate =
':%<method_name>s:%<source_location>s:%<source_line_number>s'.freeze
CommandActivityTemplate =
':%<activity>s'.freeze
CommandAntecedantTemplate =
' < %<antecedent>s'.freeze
CallerPattern =
%r{([^:]+):([^:]+):in `([^']+)'}.freeze

Instance Method Summary collapse

Instance Method Details

#defer(event) ⇒ Object



420
421
422
# File 'lib/runtime/command.rb', line 420

def defer(event)
  event.antecedent << event
end

#generate_identityObject

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/runtime/command.rb', line 391

def generate_identity
  s = format(CommandCauseIdentityTemplate, identity: @cause.identity, name: @cause.name)
  if !callstack.empty?
    trace = @callstack.first.gsub(Inform::Runtime.project_dir_path, '')
    location, line_number, method_name = CallerPattern.match(trace)&.captures
    s << format(CommandSourceFullTemplate,
      method_name: method_name, source_location: location, source_line_number: line_number)
  elsif @activity.respond_to?(:source_location) || @activity.is_a?(Proc)
    activity_source_location_enumerator = @activity.source_location.each
    source_location = activity_source_location_enumerator.next.gsub(Inform::Runtime.project_dir_path, '')
    source_line_number = activity_source_location_enumerator.next
    s << format(CommandSourceTemplate, source_location: source_location, source_line_number: source_line_number)
  elsif name != @activity.to_s
    s << format(CommandActivityTemplate, activity: @activity)
  end
  s << format(CommandAntecedantTemplate, antecedent: @antecedent) unless @antecedent.nil?
  s
end

#init_context(context, antecedent, cause) ⇒ Object

rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength



412
413
414
415
416
417
418
# File 'lib/runtime/command.rb', line 412

def init_context(context, antecedent, cause)
  return contextualize(context) unless context.nil?
  if antecedent&.cause.respond_to?(:inflib)
    return contextualize(antecedent.cause.inflib) unless antecedent&.cause&.inflib.nil?
  end
  contextualize(cause) unless cause.nil? # TODO: Maybe remove
end

#init_fields(params, &block) ⇒ Object

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength rubocop: disable Lint/LambdaWithoutLiteralBlock



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/runtime/command.rb', line 353

def init_fields(params, &block)
  @successors = defined?(Java) ? java.util.concurrent.CopyOnWriteArrayList.new : []
  @time = Time.ms
  @cause = params[:cause]
  @name = params.fetch(:name, format(CommandNameTemplate, time: @time))
  @args = params[:args]
  # TODO: FIXME This causes an error:
  # Command block used return keyword: unexpected return
  @activity = params.fetch(:activity, block_given? ? lambda(&block) : @name)
  # Converting a given block to a lambda supposedly enables the use of return
  # statements inside the event activity blocks.
  # TODO: Cite documentation source.
  # TODO: Implement tests.
  # @activity = params.fetch(:activity, block_given? ? block.to_lambda : @name)
  @antecedent = params[:antecedent]
  @time_delay = params.fetch(:delay, 0)
  @terminus = params[:terminus] ? self : nil
  @type = params.fetch(:type, :integral)
  @when = params.fetch(:when, :eventually)
  @callstack = caller.slice((i[immediately elementally].include?(@when) ? 4 : 3)..-1)
  @concluded = false
  @cause = @cause.player if @cause.is_a? InformLibrary
  @identity = generate_identity
  @context = params[:context]
end

#schedule_or_defer(event) ⇒ Object



424
425
426
427
# File 'lib/runtime/command.rb', line 424

def schedule_or_defer(event)
  return defer(event) if !event.antecedent.nil? && !event.antecedent.concluded?
  schedule(event) if event.antecedent.nil? || event.antecedent.cancelled?
end