Module: Inform::Library::ClassMethods

Included in:
Runtime
Defined in:
lib/runtime/library_loader.rb

Overview

The Inform::Library::ClassMethods module

Constant Summary collapse

InclusionGrammarGuessTemplate =
'%<inclusion>s.h.inf.rb'.freeze
InclusionGuessesTemplate =
'%<inclusion>s.h'.freeze
PropertyModifiers =
%i[additive].freeze

Instance Method Summary collapse

Instance Method Details

#Attribute(attribute) ⇒ Object

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



95
96
97
# File 'lib/runtime/library_loader.rb', line 95

def Attribute(attribute)
  DeclaredAttributes.memo << attribute
end

#Constant(constant, value, target = ::Object) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/runtime/library_loader.rb', line 117

def Constant(constant, value, target = ::Object)
  return if constant.nil?
  silence_warnings do
    target.instance_eval { const_set(constant.to_sym, value) }
  end
rescue StandardError => e
  log.error "Unexpected error resetting constant #{constant}", e
  nil
end

#declare_property(property, value, _modifier = nil) ⇒ Object



99
100
101
102
# File 'lib/runtime/library_loader.rb', line 99

def declare_property(property, value, _modifier = nil)
  DeclaredProperties.memo[property] ||= []
  DeclaredProperties.memo[property] << value
end

#Default(property, value) ⇒ Object



113
114
115
# File 'lib/runtime/library_loader.rb', line 113

def Default(property, value)
  Constant(property, value)
end

#import_global(symbol) ⇒ Object



127
128
129
# File 'lib/runtime/library_loader.rb', line 127

def import_global(symbol)
  DeclaredGlobals.memo << symbol
end

#Include(inclusion) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/runtime/library_loader.rb', line 63

def Include(inclusion)
  inclusion = Inform::Runtime.language_name if /^language__$/.match(inclusion)
  grammar_guess = format(InclusionGrammarGuessTemplate, inclusion: inclusion)
  guess = File.expand_path(
    File.join(
      Inform::Library.inform_gem_lib_path,
      Inform::Runtime.inform_dir_path,
      grammar_guess))
  return Inform.load_grammar_by_path(guess) if File.exist?(guess)
  require_first_existing(format(InclusionGuessesTemplate, inclusion: inclusion).split)
end


53
54
55
56
57
58
# File 'lib/runtime/library_loader.rb', line 53

def Link(link)
  # TODO: Implement
  # Not a priority, because in the original Inform6 links were
  # effectively just pointers to objects loaded in memory, and
  # weren't persisted using a database relation.
end

#Property(*args) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/runtime/library_loader.rb', line 105

def Property(*args)
  if PropertyModifiers.include?(modifier_or_property = args.shift)
    declare_property(args.shift, args.shift, modifier_or_property)
  else
    declare_property(modifier_or_property, args.shift)
  end
end

#require_first_existing(guesses) ⇒ Object

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/runtime/library_loader.rb', line 77

def require_first_existing(guesses)
  guessed_file_path = File.join(Inform::Runtime.inform_dir_path, guesses.shift)
  guessed_local_file_path = File.expand_path(guessed_file_path)
  if File.exist?(guessed_local_file_path)
    log.debug "Loading local library file: #{guessed_local_file_path}"
    require_relative guessed_local_file_path
  else
    log.debug "Loading library: #{guessed_local_file_path}"
    require guessed_file_path
  end
rescue LoadError, StandardError => e
  log.warn e.message
  e.backtrace.each { |t| log.warn t } if defined?(Logging)
  retry unless guesses.empty?
end