Module: EventInitializationMethods

Includes:
EventConstants
Included in:
Inform::Event
Defined in:
lib/runtime/events.rb

Overview

The EventInitializationMethods module

Constant Summary

Constants included from EventConstants

EventConstants::CallerPattern, EventConstants::EventActivityTemplate, EventConstants::EventAntecedantTemplate, EventConstants::EventCauseIdentityTemplate, EventConstants::EventNameTemplate, EventConstants::EventSourceFullTemplate, EventConstants::EventSourceTemplate

Instance Method Summary collapse

Instance Method Details

#defer(event) ⇒ Object



510
511
512
# File 'lib/runtime/events.rb', line 510

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

#generate_identityObject

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



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/runtime/events.rb', line 480

def generate_identity
  s = format(EventCauseIdentityTemplate, 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(EventSourceFullTemplate,
      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(
      EventSourceTemplate, source_location: source_location, source_line_number: source_line_number)
  elsif name != @activity.to_s
    s << format(EventActivityTemplate, activity: @activity)
  end
  s << format(EventAntecedantTemplate, antecedent: @antecedent) unless @antecedent.nil?
  s
end

#init_context(context, antecedent, cause) ⇒ Object

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



502
503
504
505
506
507
508
# File 'lib/runtime/events.rb', line 502

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



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/runtime/events.rb', line 451

def init_fields(params, &block)
  @successors = defined?(Java) ? java.util.concurrent.CopyOnWriteArrayList.new : []
  @time = Time.ms
  @cause = params[:cause]
  @name = params.fetch(:name, format(EventNameTemplate, time: @time))
  @args = params[:args]
  # 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.
  # TODO: Verify this change prevents the error:
  # Event block used return keyword: unexpected return
  @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 # TODO: Test
  @identity = generate_identity
  @context = params[:context]
end

#schedule_or_defer(event) ⇒ Object



514
515
516
517
# File 'lib/runtime/events.rb', line 514

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