Class: Inform::Ephemeral::Object

Inherits:
Object show all
Defined in:
lib/story_teller/inform/ephemeral/tag.rb,
lib/story_teller/inform/ephemeral/object.rb

Overview

The Inform::Ephemeral::Object class rubocop: disable Metrics/ClassLength

Direct Known Subclasses

StoryTeller::Daemons::Entheogen

Constant Summary collapse

Instances =
Struct.new(:memo).new(defined?(Java) ? java.util.concurrent.CopyOnWriteArrayList.new : [])

Constants included from Prototypical

Prototypical::AccessorMethodTemplate, Prototypical::LinkPattern, Prototypical::MISSING_PROPERTIES_MESSAGE, Prototypical::MethodBooleanOrBangPattern, Prototypical::MethodWriterPattern, Prototypical::MethodWriterTemplate, Prototypical::VariableTemplate, Prototypical::WriterMethodTemplate

Constants included from Inform::Events

Inform::Events::DEFAULT_ADD_EVENT_PARAMS, Inform::Events::REGISTRY, Inform::Events::THREADS_PER_PROCESSOR

Constants included from Context

Context::AttributeFieldReferenceTemplate, Context::AttributeSetterMethodTemplate

Constants included from StoryTeller::Daemons

StoryTeller::Daemons::DaemonExecutionElapsedMessage, StoryTeller::Daemons::DaemonScanElapsedMessage, StoryTeller::Daemons::RecordNotFoundPattern

Constants included from StoryTeller::Subscribers

StoryTeller::Subscribers::REGISTRY

Constants included from StoryTeller::IO

StoryTeller::IO::ItemPaddingString, StoryTeller::IO::MinimumCellPadding, StoryTeller::IO::MinimumColumnPadding, StoryTeller::IO::MinimumLineLength

Constants included from StoryTeller::Builtins

StoryTeller::Builtins::MethodDefinitionPattern, StoryTeller::Builtins::MethodTerminationPattern, StoryTeller::Builtins::NOUN, StoryTeller::Builtins::PLURAL, StoryTeller::Builtins::PREPOSITION, StoryTeller::Builtins::VERB

Constants included from StoryTeller::Color

StoryTeller::Color::BackgroundColorCodes, StoryTeller::Color::ColorCodes, StoryTeller::Color::NearestColor, StoryTeller::Color::StyleCodes

Constants included from StoryTeller::Articles

StoryTeller::Articles::LowercaseASpaceString, StoryTeller::Articles::LowercaseSomeSpaceString, StoryTeller::Articles::LowercaseTheSpaceString

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

adapter_class, build, new

Methods included from AdapterClassDelegation

#===, #delegated_adapter_class, #method_missing, #respond_to_missing?

Methods included from Genealogical

#>, #all?, #any?, #branch, #child, #collect, #concat, #count, #delete_if, #each, #each_with_object, #find, #find_all, #having, #in?, #include?, #map, #notin?, #reject, #select, #sibling, #siblings, #within?

Methods included from Modular

#after_initialize, #apply_modules, #mod, #modules_semaphore, #nil_safe_modules, #reset_modules, #unmod

Methods included from Taggable

#nil_safe_tags, #tag_names, #tagged_with?, #tagged_with_all?, #tagged_with_any?, #tagged_with_none?

Methods included from TagHelpers

#attributes, #has, #has?, #hasany?, #hasnt, #hasnt?

Methods included from Prototypical

#&, #_assign, #_get, #_set, #accessor_and_writer, #context_event, #ensure_properties_attribute!, #instance_variables_map, #key_refers_to_link?, #method_missing, #prototype, #string?, #|

Methods included from Inform::Events

#active?, active_objects, #add_event, available_processors, #contextualize, #delay, each, #elementally, #event, #events, #immediately, init_java_pool, init_java_pooled_executor, init_java_scheduled_executor, init_java_scheduler, init_pool, init_scheduled_executor, init_scheduler, #interrupt, object_events, pool, pooled_executor, possibilities, #register_callback, scheduled_executor, scheduler

Methods included from Context

definition, each_attribute, get_value, set, setter_method

Methods included from StoryTeller::Daemons

#StartDaemon, #StartTimer, #StopDaemon, #StopTimer, #before_destroy, #daemonic?, #daemonize, daemons, ensure_inform_library, execute, execute_daemon, execute_each_turn, #ghost, #heart, #init_heart, #occasionally, #on?, refresh_or_remove, #rescan, scan_objects, #schedules, #spawn, start, stop

Methods included from StoryTeller::Publisher

#muted?, #publish, #publish_except, #publish_only, #publish_system_message, #subscribers

Methods included from StoryTeller::Subscribers

#explicit_subscribers, #subscribe, #subscribers, #unsubscribe, #unsubscribe_all

Methods included from StoryTeller::IO

#connections, #flush_io, #inform, #many_per_line, #new_line, #noop, #out, #output_stream, #print, #println, #prompt, #read, #reset_prompt, #session, #spaces, #tidy_output

Methods included from StoryTeller::Builtins

#__quit, #__read, #__restart, #__restore, #__save, #__verify, #apply_instance_behavior!, #classification, #compass, #content, #current_word, #deadflag, #deadflag=, #dict_par1, #dictionary, #disrobe, #drop, #emote, #findobject, #getobject, #go, #initial, #language_descriptors, #language_pronouns, #library_method?, #life?, #light_adjusted, #method_source, #nothing, #num_words, #number, #objectloop, #ofclass, #pause, #provides?, #randomly, #react_after?, #react_before?, #remaining_words, #resembles_method?, #say, #score, #score=, #search, #special, #the_time, #the_time=, #things_score, #things_score=, #topic, #visibility_ceiling=, #with, #workflags

Methods included from StoryTeller::Plurals

#apply_inflections, #plural?, #pluralize, #singular?, #singularize

Methods included from StoryTeller::Color

#background_color, #color, #nearest_color, #style, #un_background_color, #un_color, #un_style

Methods included from StoryTeller::Articles

#A, #Cthatorthose, #Ctheyreorthats, #The, #a, #cthatorthose, #ctheyreorthats, #defart, #indefart, #isorare, #itorthem, #thatorthose, #the

Constructor Details

#initialize(textual_name = nil, *args, &block) ⇒ Object

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/story_teller/inform/ephemeral/object.rb', line 58

def initialize(textual_name = nil, *args, &block)
  super(*args)
  @name = textual_name
  @short_name = textual_name || format('(%<short_name>s)', short_name: self.class.name)
  @display_name = textual_name
  @description = nil
  @adjectives = []
  @properties = {}
  @links = []
  @modules = []
  @tags = []
  @children = []
  @visited = []
  @created_at = @modified_at = Time.now
  index_words if respond_to?(:index_words)
  index_obj(self)
  @original_description_method = self.class.instance_method(:description)
  self.with(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Inform::Prototypical

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



50
51
52
# File 'lib/story_teller/inform/ephemeral/object.rb', line 50

def created_at
  @created_at
end

#description(*args) ⇒ Object



170
171
172
173
174
# File 'lib/story_teller/inform/ephemeral/object.rb', line 170

def description(*args)
  return @description if args.nil? || args.empty?
  @description = args.length > 1 ? args.join(' ') : args.first
  @description
end

#display_nameObject

Returns the value of attribute display_name.



50
51
52
# File 'lib/story_teller/inform/ephemeral/object.rb', line 50

def display_name
  @display_name
end

#inflibObject

Returns the value of attribute inflib.



50
51
52
# File 'lib/story_teller/inform/ephemeral/object.rb', line 50

def inflib
  @inflib
end

Returns the value of attribute links.



50
51
52
# File 'lib/story_teller/inform/ephemeral/object.rb', line 50

def links
  @links
end

#modified_atObject

Returns the value of attribute modified_at.



50
51
52
# File 'lib/story_teller/inform/ephemeral/object.rb', line 50

def modified_at
  @modified_at
end

#modulesObject

Returns the value of attribute modules.



50
51
52
# File 'lib/story_teller/inform/ephemeral/object.rb', line 50

def modules
  @modules
end

#original_description_methodObject (readonly)

Returns the value of attribute original_description_method.



54
55
56
# File 'lib/story_teller/inform/ephemeral/object.rb', line 54

def original_description_method
  @original_description_method
end

#propertiesObject

Returns the value of attribute properties.



50
51
52
# File 'lib/story_teller/inform/ephemeral/object.rb', line 50

def properties
  @properties
end

#short_nameObject

Returns the value of attribute short_name.



50
51
52
# File 'lib/story_teller/inform/ephemeral/object.rb', line 50

def short_name
  @short_name
end

#tagsObject

Returns the value of attribute tags.



252
253
254
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 252

def tags
  @tags ||= []
end

#visitedObject

Returns the value of attribute visited.



50
51
52
# File 'lib/story_teller/inform/ephemeral/object.rb', line 50

def visited
  @visited
end

Class Method Details

.allObject



111
112
113
# File 'lib/story_teller/inform/ephemeral/object.rb', line 111

def self.all
  Instances.memo.dup
end

.clearObject



115
116
117
# File 'lib/story_teller/inform/ephemeral/object.rb', line 115

def self.clear
  Instances.memo.clear
end

.create(name = nil, object_type: nil, parent_id: nil, properties: {}, klass: self) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/story_teller/inform/ephemeral/object.rb', line 86

def self.create(name = nil, object_type: nil, parent_id: nil, properties: {}, klass: self)
  klass.new(name).tap do |obj|
    obj.object_type = object_type
    obj.parent_id = parent_id
    obj.properties.merge!(properties)
  end
end

.fetch_or_create_by_name(object_name, klass = self) ⇒ Object

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



80
81
82
83
84
# File 'lib/story_teller/inform/ephemeral/object.rb', line 80

def self.fetch_or_create_by_name(object_name, klass = self)
  Inform::EphemeralObjects.fetch(object_name) do
    Inform::EphemeralObjects[object_name] = klass.create(object_name)
  end
end

Instance Method Details

#<<(o) ⇒ Object



184
185
186
187
188
189
# File 'lib/story_teller/inform/ephemeral/object.rb', line 184

def <<(o)
  return if self == o
  return unless o.object?
  o.parent = self
  @children << o
end

#<=>(other) ⇒ Object



135
136
137
# File 'lib/story_teller/inform/ephemeral/object.rb', line 135

def <=>(other)
  self.name <=> other.name
end

#[](property) ⇒ Object



119
120
121
122
123
124
# File 'lib/story_teller/inform/ephemeral/object.rb', line 119

def [](property)
  return description if property == :description
  return name if property == :name
  return short_name if property == :short_name
  properties[property]
end

#_get_object(link_name) ⇒ Object



247
248
249
# File 'lib/story_teller/inform/ephemeral/object.rb', line 247

def _get_object(link_name)
  find_link(link_name)&.to
end

#_set_object(link_name, obj = nil) ⇒ Object

rubocop: enable Style/TrivialAccessors



243
244
245
# File 'lib/story_teller/inform/ephemeral/object.rb', line 243

def _set_object(link_name, obj = nil)
  link(link_name, obj).to
end

#ancestorsObject



215
216
217
# File 'lib/story_teller/inform/ephemeral/object.rb', line 215

def ancestors
  self.parent.nil? ? [] : self.parent.ancestors + [self.parent]
end

#children(o = nil, prop = :@children) ⇒ Object



200
201
202
203
204
# File 'lib/story_teller/inform/ephemeral/object.rb', line 200

def children(o = nil, prop = :@children)
  return @children if o.nil?
  return o.instance_variable_get(prop).length if o.instance_variable_defined?(prop)
  o.children.length
end

#descendantsObject



219
220
221
# File 'lib/story_teller/inform/ephemeral/object.rb', line 219

def descendants
  @children + @children.map(&:descendants).flatten
end

#empty?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/story_teller/inform/ephemeral/object.rb', line 196

def empty?(*args, &block)
  self.children.empty?(*args, &block)
end


251
252
253
# File 'lib/story_teller/inform/ephemeral/object.rb', line 251

def find_link(link_name)
  links.find { |x| x.name == link_name }
end

#idObject



146
147
148
# File 'lib/story_teller/inform/ephemeral/object.rb', line 146

def id
  self.object_id
end

#index_obj(obj) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/story_teller/inform/ephemeral/object.rb', line 103

def index_obj(obj)
  if Instances.memo.respond_to?(:add)
    Instances.memo.add(obj)
  else
    Instances.memo << obj
  end
end

#initObject Also known as: safe_init



283
284
285
# File 'lib/story_teller/inform/ephemeral/object.rb', line 283

def init
  self
end

#inspectObject



302
303
304
# File 'lib/story_teller/inform/ephemeral/object.rb', line 302

def inspect
  identity
end


255
256
257
258
259
# File 'lib/story_teller/inform/ephemeral/object.rb', line 255

def link(link_name, obj = nil)
  link = Inform::Ephemeral::Link.new(name: link_name, to: obj, from: self)
  links << link
  link
end

#linked?(link_name) ⇒ Boolean Also known as: _key?

Returns:

  • (Boolean)


261
262
263
# File 'lib/story_teller/inform/ephemeral/object.rb', line 261

def linked?(link_name)
  links.count { |x| x.name == link_name } > 0
end

#linksfrom(link_name = nil) ⇒ Object



275
276
277
278
279
280
281
# File 'lib/story_teller/inform/ephemeral/object.rb', line 275

def linksfrom(link_name = nil)
  if link_name.nil?
    links.select { |x| x.to == self }.map(&:from)
  else
    links.select { |x| x.name == link_name && x.to == self }.map(&:from)
  end
end

#linkto(link_name) ⇒ Object



271
272
273
# File 'lib/story_teller/inform/ephemeral/object.rb', line 271

def linkto(link_name)
  _get_object(link_name)
end

#list_togetherObject



211
212
213
# File 'lib/story_teller/inform/ephemeral/object.rb', line 211

def list_together
  linkto :list_together
end

#locationObject

TODO: Remove if possible after unit test implementation



207
208
209
# File 'lib/story_teller/inform/ephemeral/object.rb', line 207

def location
  self.parent
end

#name(*args) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/story_teller/inform/ephemeral/object.rb', line 154

def name(*args)
  return args.first.object_name if args.first.respond_to?(:object_name)
  return @name if args.empty?
  @name = ([@name] | args).flatten.uniq
  index_words if respond_to?(:index_words)
  @name
end

#name=(*args) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/story_teller/inform/ephemeral/object.rb', line 162

def name=(*args)
  return if args.empty?
  @name = args.first.object_name if args.first.respond_to?(:object_name)
  @name = ([@name] | args).flatten.uniq
  index_words if respond_to?(:index_words)
  return
end

#name_wordsObject



180
181
182
# File 'lib/story_teller/inform/ephemeral/object.rb', line 180

def name_words
  @name.split(/[,\s]+/).join(', ')
end

#object_nameObject



176
177
178
# File 'lib/story_teller/inform/ephemeral/object.rb', line 176

def object_name
  @display_name || @short_name || @name
end

#object_typeObject



150
151
152
# File 'lib/story_teller/inform/ephemeral/object.rb', line 150

def object_type
  self.class
end

#parent(o = nil, &block) ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'lib/story_teller/inform/ephemeral/object.rb', line 227

def parent(o = nil, &block)
  return @parent if o.nil?
  o.safe_refresh if o.respond_to? :safe_refresh
  return unless o.respond_to? :parent
  obj_parent = block_given? ? parent_selector(o, &block) : o.parent
  return nil if obj_parent.nil?
  obj_parent.safe_refresh if obj_parent.respond_to? :safe_refresh
  obj_parent
end

#parent=(o) ⇒ Object

rubocop: disable Style/TrivialAccessors



238
239
240
# File 'lib/story_teller/inform/ephemeral/object.rb', line 238

def parent=(o)
  @parent = o
end

#parse(s) ⇒ Object



139
140
141
142
143
144
# File 'lib/story_teller/inform/ephemeral/object.rb', line 139

def parse(s)
  return if inflib.nil?
  inflib.semaphore.synchronize do
    inflib.println(inflib.parse(s))
  end
end

#refreshObject Also known as: safe_refresh



288
289
290
# File 'lib/story_teller/inform/ephemeral/object.rb', line 288

def refresh
  self
end

#removeObject



191
192
193
194
# File 'lib/story_teller/inform/ephemeral/object.rb', line 191

def remove
  @children.clear
  @parent = nil
end

#rootObject



223
224
225
# File 'lib/story_teller/inform/ephemeral/object.rb', line 223

def root
  self.parent.nil? ? self : self.parent.root
end

#save(*_args) ⇒ Object Also known as: safe_save



293
294
295
# File 'lib/story_teller/inform/ephemeral/object.rb', line 293

def save(*_args)
  self
end

#tag(*args) ⇒ Object



236
237
238
239
240
241
242
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 236

def tag(*args)
  workflags.add self.identity if args.delete :workflag
  tag_names(args).each do |tag|
    tags << tag.to_sym
  end
  self
end

#to_hashObject



126
127
128
129
130
131
132
133
# File 'lib/story_teller/inform/ephemeral/object.rb', line 126

def to_hash
  properties.merge(
    id: id,
    name: name,
    short_name: short_name,
    description: description
  )
end

#to_sObject



298
299
300
# File 'lib/story_teller/inform/ephemeral/object.rb', line 298

def to_s
  self.name
end


266
267
268
# File 'lib/story_teller/inform/ephemeral/object.rb', line 266

def unlink(link_name)
  links.delete_at(links.index { |x| x.name == link_name })
end

#untag(*args) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/story_teller/inform/ephemeral/tag.rb', line 244

def untag(*args)
  workflags.delete self.identity if args.delete :workflag
  (nil_safe_tags & tag_names(args)).each do |tag|
    tags.delete tag
  end
  self
end

#valuesObject



94
95
96
97
98
99
100
101
# File 'lib/story_teller/inform/ephemeral/object.rb', line 94

def values
  {
    name: @name,
    short_name: @short_name,
    description: @description,
    properties: @properties
  }
end