Class: OtbJobQueue::JobsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/otb_job_queue/jobs_parser.rb

Overview

Parses a string of jobs into an hash

Defined Under Namespace

Classes: InputError

Constant Summary collapse

JOBS_QUEUE_FORMAT =
/(\w) => (\w|)/.freeze
Schema =

JobsParser’s Validation schema Successful validation if input is an empty String or a String of valid format

Dry::Validation.Schema do
  required(:jobs_input) { type?(String) & (empty? | format?(JOBS_QUEUE_FORMAT)) }
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jobs_input) ⇒ JobsParser

Creates a new instance of the JobsParser with the valid jobs_input param

Parameters:

  • jobs_input (String)

    jobs string



16
17
18
# File 'lib/otb_job_queue/jobs_parser.rb', line 16

def initialize(jobs_input)
  @jobs_input = jobs_input
end

Class Method Details

.call(jobs_input) ⇒ JobsParser.new(jobs_input).call, InputError

Creates a new JobsParser instance and calls the .call method on it

Examples:

OtbJobQueue::JobsParser.call('a => \nb => \nc => b')
  => { "a"=> [], "b"=> [], "c" => ["b"] }
OtbJobQueue::JobsParser.call('Hello World!')
  => InputError ({ :jobs_input => ["must be empty or is in invalid format"] })

Returns:



34
35
36
37
38
39
40
41
# File 'lib/otb_job_queue/jobs_parser.rb', line 34

def self.call(jobs_input)
  validation = self::Schema.call(jobs_input: jobs_input)
  if validation.success?
    new(jobs_input).call
  else
    raise InputError, validation.messages
  end
end

Instance Method Details

#callObject



43
44
45
# File 'lib/otb_job_queue/jobs_parser.rb', line 43

def call
  parse
end