Class: Acrosslite

Inherits:
Object
  • Object
show all
Defined in:
lib/entry.rb,
lib/acrosslite.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

VERSION =
'0.2.1'
ACROSSLITE =
2
ROWS =
44
COLUMNS =
45
SOLUTION =
52
DEFAULT_OPTIONS =
{
  :filepath => nil,
  :content  => nil,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Acrosslite

Returns a new instance of Acrosslite.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/acrosslite.rb', line 21

def initialize(*args)
  opts = {}

  case
  when args.length == 0 then
  when args.length == 1 && args[0].class == Hash then
    arg = args.shift

    if arg.class == Hash
      opts = arg
    end
  else
    raise ArgumentError, "new() expects hash or hashref as argument"
  end

  opts = DEFAULT_OPTIONS.merge opts

  @filepath   = opts[:filepath]
  @content    = opts[:content] || content

  @content_io = StringIO.new @content

  @across     = Array.new
  @down       = Array.new
  @layout     = Array.new
  @solution   = Array.new
  @diagram    = Array.new
end

Instance Attribute Details

#acrossObject (readonly)

Returns the value of attribute across.



7
8
9
# File 'lib/acrosslite.rb', line 7

def across
  @across
end

#authorObject

Returns the value of attribute author.



6
7
8
# File 'lib/acrosslite.rb', line 6

def author
  @author
end

Returns the value of attribute copyright.



6
7
8
# File 'lib/acrosslite.rb', line 6

def copyright
  @copyright
end

#diagramObject (readonly)

Returns the value of attribute diagram.



7
8
9
# File 'lib/acrosslite.rb', line 7

def diagram
  @diagram
end

#downObject (readonly)

Returns the value of attribute down.



7
8
9
# File 'lib/acrosslite.rb', line 7

def down
  @down
end

#filepathObject (readonly)

Returns the value of attribute filepath.



7
8
9
# File 'lib/acrosslite.rb', line 7

def filepath
  @filepath
end

#solutionObject (readonly)

Returns the value of attribute solution.



7
8
9
# File 'lib/acrosslite.rb', line 7

def solution
  @solution
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'lib/acrosslite.rb', line 6

def title
  @title
end

Instance Method Details

#areaObject



97
98
99
# File 'lib/acrosslite.rb', line 97

def area
  rows * columns
end

#columnsObject



62
63
64
65
66
67
68
# File 'lib/acrosslite.rb', line 62

def columns
  unless @columns 
    @content_io.seek(COLUMNS)
    @columns = @content_io.read(1).unpack('C').first
  end
  @columns
end

#contentObject



50
51
52
# File 'lib/acrosslite.rb', line 50

def content
  @content ||= read_puzzle
end

#read_puzzle(filepath = nil) ⇒ Object

If a filehandle or filepath were provided, open reads in the file’s contents into the content attribute which is then used for parsing.

open must be called prior to parsing if content has not already been provided.



148
149
150
151
152
# File 'lib/acrosslite.rb', line 148

def read_puzzle(filepath=nil)
  filepath ||= @filepath
  raise unless filepath
	@content = open(@filepath).read
end

#rowsObject



54
55
56
57
58
59
60
# File 'lib/acrosslite.rb', line 54

def rows
  unless @rows 
    @content_io.seek(ROWS)
    @rows = @content_io.read(1).unpack('C').first
  end
  @rows
end