Module: Crucigrama::Crossword::Definitions
- Included in:
- Crucigrama::Crossword
- Defined in:
- lib/crucigrama/crossword/definitions.rb
Overview
This module provides the behaviour related to crosswords’ definitions
Instance Method Summary collapse
-
#add_definition(definition, position, direction) ⇒ Object
Adds a crossword definition for the given position and direction.
-
#definition_for(position, direction) ⇒ String?
The definition for the word at the given position and direction.
-
#definitions ⇒ Hash<Symbol,Hash<Integer,Hash<Integer,String>>>
with keys direction (:horizontal or :vertical), the other direction coordinate and the given direction coordinate.
-
#definitions=(defs) ⇒ Object
Sets the definitions for the crossword words.
-
#initialize(options = {}) ⇒ Object
Initializes the definitions of a crossword if given.
-
#remove_definition(position, direction) ⇒ String?
Removes a crossword definition for the given position and direction.
Instance Method Details
#add_definition(definition, position, direction) ⇒ Object
Adds a crossword definition for the given position and direction
33 34 35 36 37 |
# File 'lib/crucigrama/crossword/definitions.rb', line 33 def add_definition(definition, position, direction) definitions[direction]||={} definitions[direction][position[direction_other_than(direction)]]||={} definitions[direction][position[direction_other_than(direction)]][position[direction]] = definition end |
#definition_for(position, direction) ⇒ String?
Returns the definition for the word at the given position and direction.
57 58 59 60 |
# File 'lib/crucigrama/crossword/definitions.rb', line 57 def definition_for(position, direction) return nil unless definitions[direction] and definitions[direction][position[direction_other_than(direction)]] definitions[direction][position[direction_other_than(direction)]][position[direction]] end |
#definitions ⇒ Hash<Symbol,Hash<Integer,Hash<Integer,String>>>
with keys direction (:horizontal or :vertical), the other direction coordinate and the given direction coordinate
14 15 16 |
# File 'lib/crucigrama/crossword/definitions.rb', line 14 def definitions @definitions end |
#definitions=(defs) ⇒ Object
Sets the definitions for the crossword words
20 21 22 23 24 25 26 |
# File 'lib/crucigrama/crossword/definitions.rb', line 20 def definitions=(defs) @definitions = Hash[defs.collect do |key,value| [key.to_sym, Hash[value.collect do |key, val| [key.to_i, Hash[val.collect{|k,v| [k.to_i, v]}]] end]] end] end |
#initialize(options = {}) ⇒ Object
Initializes the definitions of a crossword if given
7 8 9 10 |
# File 'lib/crucigrama/crossword/definitions.rb', line 7 def initialize(={}) super() self.definitions = ([:definitions]||{}) end |
#remove_definition(position, direction) ⇒ String?
Removes a crossword definition for the given position and direction
45 46 47 48 49 50 51 |
# File 'lib/crucigrama/crossword/definitions.rb', line 45 def remove_definition(position, direction) if definitions[direction] and definitions[direction][position[direction_other_than(direction)]] definition = definitions[direction][position[direction_other_than(direction)]].delete(position[direction]) definitions[direction].delete(position[direction_other_than(direction)]) if definitions[direction][position[direction_other_than(direction)]].empty? definition end end |