Class: Dreck::Parser
- Inherits:
-
Object
- Object
- Dreck::Parser
- 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.
%i[int float path file directory symbol string].freeze
Class Method Summary collapse
-
.parse_directory(dir) ⇒ String
The string itself, if it is a directory.
-
.parse_file(file) ⇒ String
The string itself, if it is a filename.
-
.parse_float(float) ⇒ Float
The floating-point value of the string.
-
.parse_int(int) ⇒ Integer
The integer value of the string.
-
.parse_list(type, list) ⇒ Array<Object>
The coerced results.
-
.parse_path(path) ⇒ String
The string itself, if it is a path.
-
.parse_string(str) ⇒ String
The coerced string.
-
.parse_symbol(sym) ⇒ Symbol
The coerced symbol.
Class Method Details
.parse_directory(dir) ⇒ String
Returns the string itself, if it is a directory.
44 45 46 47 |
# File 'lib/dreck/parser.rb', line 44 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.
36 37 38 39 |
# File 'lib/dreck/parser.rb', line 36 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.
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.
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.
65 66 67 |
# File 'lib/dreck/parser.rb', line 65 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.
28 29 30 31 |
# 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
This does nothing.
Returns the coerced string.
58 59 60 |
# File 'lib/dreck/parser.rb', line 58 def parse_string(str) str.to_s end |
.parse_symbol(sym) ⇒ Symbol
Returns the coerced symbol.
51 52 53 |
# File 'lib/dreck/parser.rb', line 51 def parse_symbol(sym) sym.to_sym end |