Class: Dreck::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/dreck/parser.rb

Overview

Type and other constraint testing methods for Dreck.

Constant Summary collapse

SCALAR_TYPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns the scalar types recognized by Dreck.

Returns:

  • (Array<Symbol>)

    the scalar types recognized by Dreck.

%i[int float path file directory symbol string].freeze

Class Method Summary collapse

Class Method Details

.parse_directory(dir) ⇒ String

Returns the string itself, if it is a directory.

Parameters:

  • dir (String)

    the string to check for directory-ness

Returns:

  • (String)

    the string itself, if it is a directory

Raises:

  • (ParserError)

    if the string is not a valid directory on disk


46
47
48
49
50
# File 'lib/dreck/parser.rb', line 46

def parse_directory(dir)
  raise ParserError, "#{dir}: no such directory" unless File.directory?(dir)

  dir
end

.parse_file(file) ⇒ String

Returns the string itself, if it is a filename.

Parameters:

  • file (String)

    the string to check for file-ness

Returns:

  • (String)

    the string itself, if it is a filename

Raises:

  • (ParserError)

    if the string is not a valid regular file on disk


37
38
39
40
41
# File 'lib/dreck/parser.rb', line 37

def parse_file(file)
  raise ParserError, "#{file}: no such file" unless File.file?(file)

  file
end

.parse_float(float) ⇒ Float

Returns the floating-point value of the string.

Parameters:

  • float (String)

    the string to coerce into a float

Returns:

  • (Float)

    the floating-point value of the string

Raises:


21
22
23
# File 'lib/dreck/parser.rb', line 21

def parse_float(float)
  Float float rescue raise ParserError, "#{float}: not a float"
end

.parse_int(int) ⇒ Integer

Returns the integer value of the string.

Parameters:

  • int (String)

    the string to coerce into an integer

Returns:

  • (Integer)

    the integer value of the string

Raises:


14
15
16
# File 'lib/dreck/parser.rb', line 14

def parse_int(int)
  Integer int rescue raise ParserError, "#{int}: not an integer"
end

.parse_list(type, list) ⇒ Array<Object>

Returns the coerced results.

Parameters:

  • type (Symbol)

    the type of each member of the list

  • list (Array<String>)

    the value of each member

Returns:

  • (Array<Object>)

    the coerced results


68
69
70
# File 'lib/dreck/parser.rb', line 68

def parse_list(type, list)
  list.map { |arg| send "parse_#{type}", arg }
end

.parse_path(path) ⇒ String

Returns the string itself, if it is a path.

Parameters:

  • path (String)

    the string to check for path-ness

Returns:

  • (String)

    the string itself, if it is a path

Raises:

  • (ParserError)

    if the string is not a valid path on disk


28
29
30
31
32
# File 'lib/dreck/parser.rb', line 28

def parse_path(path)
  raise ParserError, "#{path}: no such path" unless File.exist?(path)

  path
end

.parse_string(str) ⇒ String

Note:

This does nothing.

Returns the coerced string.

Parameters:

  • str (String)

    the string to coerce into a string

Returns:

  • (String)

    the coerced string


61
62
63
# File 'lib/dreck/parser.rb', line 61

def parse_string(str)
  str.to_s
end

.parse_symbol(sym) ⇒ Symbol

Returns the coerced symbol.

Parameters:

  • sym (String)

    the string to coerce into a symbol

Returns:

  • (Symbol)

    the coerced symbol


54
55
56
# File 'lib/dreck/parser.rb', line 54

def parse_symbol(sym)
  sym.to_sym
end