Module: StoryTeller::Engine

Defined in:
lib/story_teller/engine.rb,
lib/story_teller/version.rb

Overview

module Engine

Defined Under Namespace

Modules: Config, Library

Constant Summary collapse

VERSION =
'1.3.1'.freeze

Class Method Summary collapse

Class Method Details

.bind_managed_library(inform_library) ⇒ Object

Raises:

  • (ManagedLibraryError)


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/story_teller/engine.rb', line 101

def bind_managed_library(inform_library)
  raise ManagedLibraryError, "InformLibrary is nil" if inform_library.nil?

  selfobj = inform_library.selfobj
  raise ManagedLibraryError, "InformLibrary selfobj is nil" if selfobj.nil?

  libraries[selfobj] = inform_library
  selfobj.inflib = inform_library if selfobj.respond_to?(:inflib=)

  ensure_player_location(inform_library) if persist_player_location?

  inform_library
end

.default_configObject



145
146
147
# File 'lib/story_teller/engine.rb', line 145

def default_config
  @default_config ||= StoryTeller::Engine::Config::DEFAULTS
end

.default_environmentObject



169
170
171
# File 'lib/story_teller/engine.rb', line 169

def default_environment
  StoryTeller::Engine.default_config[:environment]
end

.default_invocation_propertiesObject



173
174
175
# File 'lib/story_teller/engine.rb', line 173

def default_invocation_properties
  StoryTeller::Engine.default_config.fetch(:properties, '').split.map(&:to_sym).freeze
end

.ensure_player_location(inflib) ⇒ Object

rubocop: disable Metrics/CyclomaticComplexity



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/story_teller/engine.rb', line 116

def ensure_player_location(inflib)
  return unless persist_player_location?
  return if inflib.nil?

  existing_location = inflib.instance_variable_get(:@location)
  return unless existing_location.nil?

  player = inflib.selfobj
  location = player.location if player.respond_to?(:location)
  location ||= player.spawn_point if player.respond_to?(:spawn_point)

  inflib.instance_variable_set(:@location, location) unless location.nil?
end

.executableObject



165
166
167
# File 'lib/story_teller/engine.rb', line 165

def executable
  @executable
end

.executable=(executable) ⇒ Object



161
162
163
# File 'lib/story_teller/engine.rb', line 161

def executable=(executable)
  @executable = executable
end

.inform_code_prefixObject



149
150
151
# File 'lib/story_teller/engine.rb', line 149

def inform_code_prefix
  StoryTeller::Engine.default_config[:inform6lib_gem_code_prefix]
end

.invocation_contextObject



186
187
188
# File 'lib/story_teller/engine.rb', line 186

def invocation_context
  @invocation_context ||= Struct.new(*invocation_properties)
end

.invocation_propertiesObject



177
178
179
# File 'lib/story_teller/engine.rb', line 177

def invocation_properties
  @invocation_properties ||= default_invocation_properties
end

.invocation_properties=(properties) ⇒ Object



181
182
183
184
# File 'lib/story_teller/engine.rb', line 181

def invocation_properties=(properties)
  @invocation_properties = properties
  @invocation_context = nil
end

.language_nameObject



190
191
192
# File 'lib/story_teller/engine.rb', line 190

def language_name
  StoryTeller::Engine.default_config[:language]
end

.librariesObject



59
60
61
# File 'lib/story_teller/engine.rb', line 59

def libraries
  @libraries ||= {}
end

.library(selfobj = player_object) ⇒ Object



85
86
87
88
89
90
# File 'lib/story_teller/engine.rb', line 85

def library(selfobj = player_object)
  return libraries[selfobj] if !selfobj.nil? && libraries.key?(selfobj)

  inflib = InformLibrary.new.tap { |inflib| inflib.selfobj = selfobj }
  bind_managed_library(inflib)
end

.main_objectObject



141
142
143
# File 'lib/story_teller/engine.rb', line 141

def main_object
  @main_object
end

.main_object=(obj) ⇒ Object



137
138
139
# File 'lib/story_teller/engine.rb', line 137

def main_object=(obj)
  @main_object = obj
end

.parser_selfobjObject

rubocop: enable Metrics/CyclomaticComplexity



131
132
133
134
135
# File 'lib/story_teller/engine.rb', line 131

def parser_selfobj
  return nil unless defined?(Inform::Parser::SelfObj)

  Inform::Parser::SelfObj
end

.persist_player_location=(value) ⇒ Object



69
70
71
# File 'lib/story_teller/engine.rb', line 69

def persist_player_location=(value)
  @persist_player_location = value
end

.persist_player_location?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/story_teller/engine.rb', line 63

def persist_player_location?
  return @persist_player_location unless @persist_player_location.nil?

  default_config.fetch(:persist_player_location, false)
end

.player?(obj) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
197
# File 'lib/story_teller/engine.rb', line 194

def player?(obj)
  return false unless defined?(StoryTeller::IO::Session) && StoryTeller::IO::Session.respond_to?(:players)
  StoryTeller::IO::Session.players.include?(obj)
end

.player_objectObject



73
74
75
# File 'lib/story_teller/engine.rb', line 73

def player_object
  @player_object || parser_selfobj
end

.player_object=(object) ⇒ Object

Raises:

  • (ManagedLibraryError)


77
78
79
80
81
82
83
# File 'lib/story_teller/engine.rb', line 77

def player_object=(object)
  raise ManagedLibraryError, "player object is nil" if object.nil?

  @player_object = object
  replace_parser_selfobj(object)
  libraries.clear
end

.prime_dictionaryObject



199
200
201
202
203
204
205
# File 'lib/story_teller/engine.rb', line 199

def prime_dictionary
  Inform::Object.select_map(:name).compact.each do |n|
    Array(n).join(' ').split(/[,\s]+/).grep_v(/[^a-z]/i).map(&:downcase).map(&:to_sym).each do |word|
      StoryTeller::Dictionary.merge([word])
    end
  end
end

.project_dir_pathObject



153
154
155
156
157
158
159
# File 'lib/story_teller/engine.rb', line 153

def project_dir_path
  @project_dir_path ||= begin
    story_dir_path = File.expand_path(__dir__)
    lib_dir_path = File.expand_path(File.dirname(story_dir_path))
    File.expand_path(File.dirname(lib_dir_path))
  end
end

.replace_parser_selfobj(object) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/story_teller/engine.rb', line 92

def replace_parser_selfobj(object)
  return object unless defined?(Inform::Parser)

  Inform::Parser.send(:remove_const, :SelfObj) if Inform::Parser.const_defined?(:SelfObj, false)
  Inform::Parser.const_set(:SelfObj, object)

  object
end