Class: Lpgrid::InputReader

Inherits:
Object
  • Object
show all
Defined in:
lib/lpgrid/input_reader.rb

Constant Summary collapse

IncorectInputFormat =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ InputReader

Returns a new instance of InputReader.



13
14
15
# File 'lib/lpgrid/input_reader.rb', line 13

def initialize(string)
  @string = string
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



5
6
7
# File 'lib/lpgrid/input_reader.rb', line 5

def string
  @string
end

Class Method Details

.translate(string) ⇒ Object



7
8
9
10
11
# File 'lib/lpgrid/input_reader.rb', line 7

def self.translate(string)
  instance = new(string)
  instance.check_format
  instance.translate
end

Instance Method Details

#check_formatObject



17
18
19
20
21
# File 'lib/lpgrid/input_reader.rb', line 17

def check_format
  if splitter.collect(&:length).select { |leng| leng !=2 } .any?
    raise IncorectInputFormat
  end
end

#splitterObject



29
30
31
# File 'lib/lpgrid/input_reader.rb', line 29

def splitter
  string.split(' ')
end

#translateObject



23
24
25
26
27
# File 'lib/lpgrid/input_reader.rb', line 23

def translate
  splitter.collect do |element|
    Grid.new(element).fetch
  end.join(' ')
end