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

Instance Method Details

#add_definition(definition, position, direction) ⇒ Object

Adds a crossword definition for the given position and direction

Parameters:

  • definition (String)

    the definition to add

  • position (Hash<Symbol, Integer>)

    a hash with the horizontal and vertical coordinates of the cell where the defined word starts

  • direction (Symbol)

    the direction of the defined word, :horizontal or :vertical



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.

Parameters:

  • position (Hash<Symbol, Integer>)

    a hash with the horizontal and vertical coordinates of the cell where the queried word starts

  • direction (Symbol)

    the direction of the queried word, :horizontal or :vertical

Returns:

  • (String, nil)

    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

#definitionsHash<Symbol,Hash<Integer,Hash<Integer,String>>>

with keys direction (:horizontal or :vertical), the other direction coordinate and the given direction coordinate

Returns:

  • (Hash<Symbol,Hash<Integer,Hash<Integer,String>>>)

    the definitions in the crossword, in a hash to be accessed



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

Parameters:

  • defs (Hash<String|Symbol,Hash<String|Integer,Hash<String|Integer,String>>>)

    the definitions



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

Parameters:

  • options (Hash<Symbol,Integer>, optional) (defaults to: {})

    the options of the crossword

Options Hash (options):

  • :definitions (Hash)

    the definitions for the word in the crossword as returned by method #definitions



7
8
9
10
# File 'lib/crucigrama/crossword/definitions.rb', line 7

def initialize(options={})
  super(options)
  self.definitions = (options[:definitions]||{})
end

#remove_definition(position, direction) ⇒ String?

Removes a crossword definition for the given position and direction

Parameters:

  • position (Hash<Symbol, Integer>)

    a hash with the horizontal and vertical coordinates of the cell where the word whose definition is to be removed starts

  • direction (Symbol)

    the direction of the word whose definition is to be removed, :horizontal or :vertical

Returns:

  • (String, nil)

    the removed definition if found, nil otherwise



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