Class: Gamefic::Props::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/gamefic/props/output.rb

Overview

A container for output sent to players with a hash interface for custom data.

Constant Summary collapse

READER_METHODS =
%i[messages options queue scene prompt last_prompt last_input].freeze
WRITER_METHODS =
%i[messages= prompt= last_prompt= last_input=].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**data) ⇒ Output

Returns a new instance of Output.



14
15
16
17
18
19
20
21
22
23
# File 'lib/gamefic/props/output.rb', line 14

def initialize **data
  @raw_data = {
    messages: '',
    options: [],
    queue: [],
    scene: {},
    prompt: ''
  }
  merge! data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/gamefic/props/output.rb', line 94

def method_missing method, *args
  return raw_data[method] if READER_METHODS.include?(method)

  return raw_data[method.to_s[0..-2].to_sym] = args.first if WRITER_METHODS.include?(method)

  super
end

Instance Attribute Details

#[prompt]([prompt]) ⇒ String

The input prompt to be displayed to the player.

Returns:



# File 'lib/gamefic/props/output.rb', line 46

#last_inputString?

The input received from the player in the previous scene.

Returns:



# File 'lib/gamefic/props/output.rb', line 51

#last_promptString?

The input prompt from the previous scene.

Returns:



# File 'lib/gamefic/props/output.rb', line 56

#messagesString

A text message to be displayed at the start of a scene.

Returns:



# File 'lib/gamefic/props/output.rb', line 25

#optionsArray<String>

An array of options to be presented to the player, e.g., in a MultipleChoice scene.

Returns:



# File 'lib/gamefic/props/output.rb', line 30

#queueArray<String>

An array of commands waiting to be executed.

Returns:



# File 'lib/gamefic/props/output.rb', line 36

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



12
13
14
# File 'lib/gamefic/props/output.rb', line 12

def raw_data
  @raw_data
end

#sceneHash

A hash containing the scene’s :name and :type.

Returns:

  • (Hash)


# File 'lib/gamefic/props/output.rb', line 41

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (Symbol)


62
63
64
# File 'lib/gamefic/props/output.rb', line 62

def [] key
  raw_data[key]
end

#[]=(key, value) ⇒ Object

Parameters:

  • key (Symbol)
  • value (Object)


68
69
70
# File 'lib/gamefic/props/output.rb', line 68

def []= key, value
  raw_data[key] = value
end

#freezeObject



89
90
91
92
# File 'lib/gamefic/props/output.rb', line 89

def freeze
  raw_data.freeze
  super
end

#merge!(data) ⇒ Object



81
82
83
# File 'lib/gamefic/props/output.rb', line 81

def merge! data
  data.each { |key, val| self[key] = val }
end

#replace(data) ⇒ Object



85
86
87
# File 'lib/gamefic/props/output.rb', line 85

def replace data
  raw_data.replace data
end

#respond_to_missing?(method, _with_private = false) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/gamefic/props/output.rb', line 102

def respond_to_missing?(method, _with_private = false)
  READER_METHODS.include?(method) || WRITER_METHODS.include?(method)
end

#to_hashHash

Returns:

  • (Hash)


73
74
75
# File 'lib/gamefic/props/output.rb', line 73

def to_hash
  raw_data.dup
end

#to_json(_ = nil) ⇒ Object



77
78
79
# File 'lib/gamefic/props/output.rb', line 77

def to_json _ = nil
  raw_data.to_json
end