class Dreck::Parser

Type and other constraint testing methods for {Dreck}.

Constants

SCALAR_TYPES

@return [Array<Symbol>] the scalar types recognized by {Dreck}. @api private

Public Class Methods

parse_directory(dir) click to toggle source

@param dir [String] the string to check for directory-ness @return [String] the string itself, if it is a directory @raise [ParserError] if the string is not a valid directory on disk

# 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) click to toggle source

@param file [String] the string to check for file-ness @return [String] the string itself, if it is a filename @raise [ParserError] if the string is not a valid regular file on disk

# 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) click to toggle source

@param float [String] the string to coerce into a float @return [Float] the floating-point value of the string @raise [ParserError] if the string cannot be converted

# File lib/dreck/parser.rb, line 21
def parse_float(float)
  Float float rescue raise ParserError, "#{float}: not a float"
end
parse_int(int) click to toggle source

@param int [String] the string to coerce into an integer @return [Integer] the integer value of the string @raise [ParserError] if the string cannot be converted

# 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) click to toggle source

@param type [Symbol] the type of each member of the list @param list [Array<String>] the value of each member @return [Array<Object>] the coerced results

# File lib/dreck/parser.rb, line 68
def parse_list(type, list)
  list.map { |arg| send "parse_#{type}", arg }
end
parse_path(path) click to toggle source

@param path [String] the string to check for path-ness @return [String] the string itself, if it is a path @raise [ParserError] if the string is not a valid path on disk

# 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) click to toggle source

@param str [String] the string to coerce into a string @return [String] the coerced string @note This does nothing.

# File lib/dreck/parser.rb, line 61
def parse_string(str)
  str.to_s
end
parse_symbol(sym) click to toggle source

@param sym [String] the string to coerce into a symbol @return [Symbol] the coerced symbol

# File lib/dreck/parser.rb, line 54
def parse_symbol(sym)
  sym.to_sym
end