Class: Interphase::Helpers::LayoutParser

Inherits:
Object
  • Object
show all
Defined in:
lib/interphase/helpers/layout_parser.rb

Overview

Provides methods to parse an LDS into a description of a Grid, for use with Layout.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ LayoutParser

Create a new LayoutParser.

input

The input LDS.


27
28
29
# File 'lib/interphase/helpers/layout_parser.rb', line 27

def initialize(input)
  @input = input.lines.map(&:strip).reject { |x| x == '' }
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input


23
24
25
# File 'lib/interphase/helpers/layout_parser.rb', line 23

def input
  @input
end

Instance Method Details

#columnsObject

Gets the number of columns which the LDS contains.


32
33
34
35
36
# File 'lib/interphase/helpers/layout_parser.rb', line 32

def columns
  validate

  input.first.length
end

#create_placement_from_start(start_row, start_col) ⇒ Object

Creates a LayoutPlacement given a widget's starting position.

start_row

The row on which the top-left of the widget occurs.

start_col

The column on which the top-left of the widget occurs.


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/interphase/helpers/layout_parser.rb', line 71

def create_placement_from_start(start_row, start_col)
  name = input[start_row][start_col]

  # Determine the width of the widget
  current_col = start_col
  width = 0
  while name == input[start_row][current_col]
    width += 1
    current_col += 1
  end

  # Determine the height of the widget
  current_row = start_row
  height = 0  # input[current_row] could be nil, which would throw due to #[] on nil

  while !input[current_row].nil? && name == input[current_row][start_col]
    height += 1
    current_row += 1
  end

  puts "#{name} starts at #{start_row}, #{start_col}, width #{width}, height #{height}"

  LayoutPlacement.new(
    *position_and_size_to_placement_args(
      start_row,
      start_col,
      width,
      height
    )
  )
end

#parseObject

Parses the LDS into a LayoutDescription.


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/interphase/helpers/layout_parser.rb', line 53

def parse
  validate

  widgets = {}

  input.each_with_index do |line, row|
    line.chars.each_with_index do |char, col|
      widgets[char] = create_placement_from_start(row, col) \
        if widgets[char].nil?
    end
  end

  LayoutDescription.new(rows, columns, widgets)
end

#position_and_size_to_placement_args(start_row, start_col, width, height) ⇒ Object

Converts a start position, width and height into an array of:

[left, right, top, bottom]

Suitable for use with a Grid or LayoutPlacement.


106
107
108
109
110
111
112
113
# File 'lib/interphase/helpers/layout_parser.rb', line 106

def position_and_size_to_placement_args(start_row, start_col, width, height)
  [
    start_col,                # left
    start_col + width,        # right
    start_row,                # top
    start_row + height        # bottom
  ]
end

#rowsObject

Gets the number of rows which the LDS contains.


39
40
41
42
43
# File 'lib/interphase/helpers/layout_parser.rb', line 39

def rows
  validate

  input.length
end

#validateObject

Ensures that the LDS used to create the LayoutParser is valid.


46
47
48
49
50
# File 'lib/interphase/helpers/layout_parser.rb', line 46

def validate  # Checks if all rows are equal

  raise 'Not all rows of equal length' \
    unless input.map(&:length).uniq.length == 1
end