Module: Crucigrama::Crossword::Serializable

Included in:
Crucigrama::Crossword
Defined in:
lib/crucigrama/crossword/serializable.rb

Overview

This module provides methods to serialize a crossword

Instance Method Summary collapse

Instance Method Details

#attributesHash

Returns a hash with the dimensions, grid and serialized definitions of a crossword.

Returns:

  • (Hash)

    a hash with the dimensions, grid and serialized definitions of a crossword



7
8
9
10
11
12
13
# File 'lib/crucigrama/crossword/serializable.rb', line 7

def attributes
  {
    :dimensions => dimensions,
    :grid => grid,
    :definitions => serialized_definitions
  }
end

#serialized_definitionsHash

Returns the definitions of a crossword in a JSON-compatible format.

Returns:

  • (Hash)

    the definitions of a crossword in a JSON-compatible format



16
17
18
19
20
21
22
23
24
# File 'lib/crucigrama/crossword/serializable.rb', line 16

def serialized_definitions 
  Hash[definitions.collect do |direction, defs|
    [direction, Hash[defs.collect do |line_number, line_defs|
      [line_number.to_s, Hash[line_defs.collect do |pos_number, definition|
        [pos_number.to_s, definition]
      end]]
    end]]
  end]
end

#to_jsonString

Returns the attributes of the crossword as returned by #attributes in a JSON string.

Returns:

  • (String)

    the attributes of the crossword as returned by #attributes in a JSON string



27
28
29
# File 'lib/crucigrama/crossword/serializable.rb', line 27

def to_json
  MultiJson.encode(attributes)
end