Class: Gamefic::Rulebook::Calls
- Inherits:
-
Object
- Object
- Gamefic::Rulebook::Calls
- Defined in:
- lib/gamefic/rulebook/calls.rb
Overview
A collection of responses and syntaxes that constitute the actions available to actors.
Instance Method Summary collapse
- #add_response(response) ⇒ Object
- #add_syntax(syntax) ⇒ Syntax
- #empty? ⇒ Boolean
- #freeze ⇒ Object
-
#initialize ⇒ Calls
constructor
A new instance of Calls.
- #responses ⇒ Object
- #responses_for(*verbs) ⇒ Object
- #synonyms ⇒ Object
- #syntaxes ⇒ Object
- #syntaxes_for(*synonyms) ⇒ Object
- #verbs ⇒ Object
Constructor Details
#initialize ⇒ Calls
Returns a new instance of Calls.
9 10 11 12 |
# File 'lib/gamefic/rulebook/calls.rb', line 9 def initialize @verb_response_map = Hash.new { |hash, key| hash[key] = [] } @synonym_syntax_map = Hash.new { |hash, key| hash[key] = [] } end |
Instance Method Details
#add_response(response) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/gamefic/rulebook/calls.rb', line 47 def add_response response verb_response_map[response.verb].unshift response sort_responses_for response.verb add_syntax response.syntax unless response.verb.to_s.start_with?('_') response end |
#add_syntax(syntax) ⇒ Syntax
56 57 58 59 60 61 62 63 64 |
# File 'lib/gamefic/rulebook/calls.rb', line 56 def add_syntax syntax raise "No responses exist for \"#{syntax.verb}\"" unless verb_response_map.key?(syntax.verb) return if synonym_syntax_map[syntax.synonym].include?(syntax) synonym_syntax_map[syntax.synonym].unshift syntax sort_syntaxes_for syntax.synonym syntax end |
#empty? ⇒ Boolean
66 67 68 |
# File 'lib/gamefic/rulebook/calls.rb', line 66 def empty? verb_response_map.empty? && synonym_syntax_map.empty? end |
#freeze ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/gamefic/rulebook/calls.rb', line 14 def freeze super @verb_response_map.freeze @verb_response_map.values.map(&:freeze) @synonym_syntax_map.freeze @synonym_syntax_map.values.map(&:freeze) self end |
#responses ⇒ Object
31 32 33 |
# File 'lib/gamefic/rulebook/calls.rb', line 31 def responses verb_response_map.values.flatten end |
#responses_for(*verbs) ⇒ Object
39 40 41 |
# File 'lib/gamefic/rulebook/calls.rb', line 39 def responses_for *verbs verbs.flat_map { |verb| verb_response_map.fetch(verb, []) } end |
#synonyms ⇒ Object
27 28 29 |
# File 'lib/gamefic/rulebook/calls.rb', line 27 def synonyms synonym_syntax_map.keys.compact.sort end |
#syntaxes ⇒ Object
23 24 25 |
# File 'lib/gamefic/rulebook/calls.rb', line 23 def syntaxes synonym_syntax_map.values.flatten end |
#syntaxes_for(*synonyms) ⇒ Object
43 44 45 |
# File 'lib/gamefic/rulebook/calls.rb', line 43 def syntaxes_for *synonyms synonyms.flat_map { |syn| synonym_syntax_map.fetch(syn, []) } end |
#verbs ⇒ Object
35 36 37 |
# File 'lib/gamefic/rulebook/calls.rb', line 35 def verbs verb_response_map.keys.compact.sort end |