Class: Gamefic::Props::Output
- Inherits:
-
Object
- Object
- Gamefic::Props::Output
- 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
-
#[prompt]([prompt]) ⇒ String
The input prompt to be displayed to the player.
-
#last_input ⇒ String?
The input received from the player in the previous scene.
-
#last_prompt ⇒ String?
The input prompt from the previous scene.
-
#messages ⇒ String
A text message to be displayed at the start of a scene.
-
#options ⇒ Array<String>
An array of options to be presented to the player, e.g., in a MultipleChoice scene.
-
#queue ⇒ Array<String>
An array of commands waiting to be executed.
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
-
#scene ⇒ Hash
A hash containing the scene’s :name and :type.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #freeze ⇒ Object
-
#initialize(**data) ⇒ Output
constructor
A new instance of Output.
- #merge!(data) ⇒ Object
- #method_missing(method, *args) ⇒ Object
- #replace(data) ⇒ Object
- #respond_to_missing?(method, _with_private = false) ⇒ Boolean
- #to_hash ⇒ Hash
- #to_json(_ = nil) ⇒ Object
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.
|
# File 'lib/gamefic/props/output.rb', line 46
|
#last_input ⇒ String?
The input received from the player in the previous scene.
|
# File 'lib/gamefic/props/output.rb', line 51
|
#last_prompt ⇒ String?
The input prompt from the previous scene.
|
# File 'lib/gamefic/props/output.rb', line 56
|
#messages ⇒ String
A text message to be displayed at the start of a scene.
|
# File 'lib/gamefic/props/output.rb', line 25
|
#options ⇒ Array<String>
An array of options to be presented to the player, e.g., in a MultipleChoice scene.
|
# File 'lib/gamefic/props/output.rb', line 30
|
#queue ⇒ Array<String>
An array of commands waiting to be executed.
|
# File 'lib/gamefic/props/output.rb', line 36
|
#raw_data ⇒ Object (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 |
#scene ⇒ Hash
A hash containing the scene’s :name and :type.
|
# File 'lib/gamefic/props/output.rb', line 41
|
Instance Method Details
#[](key) ⇒ Object
62 63 64 |
# File 'lib/gamefic/props/output.rb', line 62 def [] key raw_data[key] end |
#[]=(key, value) ⇒ Object
68 69 70 |
# File 'lib/gamefic/props/output.rb', line 68 def []= key, value raw_data[key] = value end |
#freeze ⇒ Object
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
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_hash ⇒ 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 |