Class: GridGenerator::FaceParser

Inherits:
Object
  • Object
show all
Defined in:
lib/grid_generator/face_parser.rb

Constant Summary collapse

COLOURS =
{ 
  'w' => '#ffffff',
  'y' => '#ffff00',
  'b' => '#5050ff',
  'g' => '#00b000',
  'r' => '#ff0000',
  'o' => '#ffb000',
  'gr' => '#808080',
  'br' => '#804000',
  's' => '#c2b280', 
  'c' => '#00ffff',
  'l' => '#80ff80',
  'p' => '#800080', 
  'pi' => '#ff8080',
  'bl' => '#404040',
  'fu' => "#f0f0f0", # face up
  'ff' => "#d0d0d0", # face front
  'fr' => "#b0b0b0"  # face right
}
OPACITY =
{
  full: 1,
  faded: 0.4
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ FaceParser

Returns a new instance of FaceParser.


28
29
30
# File 'lib/grid_generator/face_parser.rb', line 28

def initialize(string)
  @string = string
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.


36
37
38
# File 'lib/grid_generator/face_parser.rb', line 36

def string
  @string
end

Instance Method Details

#parseObject


38
39
40
41
42
43
44
# File 'lib/grid_generator/face_parser.rb', line 38

def parse
  if single?
    parse_char(string)
  else
    parse_array(string)
  end
end

#parse_array(str) ⇒ Object


56
57
58
59
60
61
62
# File 'lib/grid_generator/face_parser.rb', line 56

def parse_array(str)
  str.split(/\\n/).map do |line|
    line.split(',').map(&:strip).map do |col|
      parse_char(col)
    end
  end 
end

#parse_char(char) ⇒ Object


46
47
48
49
50
51
52
53
54
# File 'lib/grid_generator/face_parser.rb', line 46

def parse_char(char)
  if char == '-'
    nil
  else
    colour = COLOURS[char.downcase]
    opacity = OPACITY[(/[[:upper:]]/.match(char) ? :full : :faded)]
    { colour: colour, opacity: opacity }
  end
end

#single?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'lib/grid_generator/face_parser.rb', line 32

def single?
  !@string.include?("\\n")
end