module Elevatore

Constants

DIRECTION_DOWN
DIRECTION_UP
LOWER_LEVEL
SPEED
TOP_LEVEL
VERSION

Public Class Methods

calling(opts=[]) click to toggle source
# File lib/elevatore.rb, line 15
def self.calling opts=[]
  return nil if opts == []
  @opts = opts
  enforce_rules! @opts
  @elevator = Elevatore::Elevator.new(people: build_people(@opts))
  @elevator.print_exit_times
end

Private Class Methods

build_people(opts) click to toggle source
# File lib/elevatore.rb, line 24
def self.build_people opts
  (opts.class==String ? parse_string(opts) : parse_array(opts))
end
enforce_rules!(opts) click to toggle source
# File lib/elevatore.rb, line 48
def self.enforce_rules! opts
  begin
    @opts = JSON.parse(opts)
  ensure
    unless @opts.class==String || (@opts.class==Array && !@opts.any?{|hash| hash.class!=Hash })
      raise ArgumentError.new('Input should be either a String or a Array with Hashes of attributes')
    end
    if @opts.class==String
      person_id = @opts.split(" ".freeze)[1].chomp("P")
      person_from = @opts.split(" ".freeze)[1].chomp("P")
      person_to = @opts.split(" ".freeze)[2]
      person_enter_at = @opts.split(" ".freeze)[3]
      if !@opts.match?(/^(([0-9]+(\.[0-9]+)?m\ +P[0-9]+\ [0-9]+\ [0-9]+)\s?)+/)
        raise ArgumentError.new("Error on P#{person_id}: Text lines input should follow the structure: '<MINUTE>m P<ID> <FROM> <TO>'")
      end
    else @opts.class==Array
      @opts.each do |person|
        if person.keys.sort != ["id", "from", "to", "enter_at"].sort
          raise ArgumentError.new('Error on P#{person[:id]}: All hashes should contain [:id, :from, :to, :enter_at] as keys')
        end
        if !person.values.map(&:class).any?{|klass| klass <= Numeric }
          raise ArgumentError.new("#{"Error on P"+person[:id]+": " unless person[:id].nil?}All hashes should contain numbers as values")
        end
      end
    end
  end
end
parse_array(opts) click to toggle source
# File lib/elevatore.rb, line 28
def self.parse_array opts
  people=[]
  opts.map.each do |person|
    people << Elevatore::Person.new(person)
  end
  people
end
parse_string(opts) click to toggle source
# File lib/elevatore.rb, line 36
def self.parse_string opts
  people=[]
  opts.split("\n".freeze).each do |line|
    person = line.split(" ")
    people << Elevatore::Person.new(enter_at: person[0].chomp("m").to_f,
                                    id: person[1][1..-1].to_i,
                                    from: person[2].to_i,
                                    to: person[3].to_i)
  end
  people
end