Class: Choosy::Converter
- Inherits:
-
Object
- Object
- Choosy::Converter
- Defined in:
- lib/choosy/converter.rb
Constant Summary collapse
- CONVERSIONS =
{ :integer => nil, :int => :integer, :float => nil, :symbol => nil, :file => nil, # Succeeds only if a file is present :yaml => nil, # Loads a YAML file, if present :date => nil, :time => nil, :datetime => nil, :string => nil, :boolean => nil, :bool => :boolean }
Class Method Summary collapse
- .boolean(value) ⇒ Object
- .convert(ty, value) ⇒ Object
- .date(value) ⇒ Object
- .datetime(value) ⇒ Object
- .file(value) ⇒ Object
- .float(value) ⇒ Object
- .for(ty) ⇒ Object
- .integer(value) ⇒ Object
- .string(value) ⇒ Object
- .symbol(value) ⇒ Object
- .time(value) ⇒ Object
- .yaml(value) ⇒ Object
Class Method Details
.boolean(value) ⇒ Object
45 46 47 |
# File 'lib/choosy/converter.rb', line 45 def self.boolean(value) value # already set end |
.convert(ty, value) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/choosy/converter.rb', line 33 def self.convert(ty, value) if ty.nil? raise ArgumentError.new("Can't convert nil to a type.") end if CONVERSIONS.has_key?(ty) send(ty, value) else ty.convert(value) end end |
.date(value) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/choosy/converter.rb', line 90 def self.date(value) begin return Date.parse(value) rescue ArgumentError raise Choosy::ConversionError.new("Unable to convert '#{value}' into a date") end end |
.datetime(value) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/choosy/converter.rb', line 106 def self.datetime(value) begin return DateTime.parse(value) rescue ArgumentError raise Choosy::ConversionError.new("Unable to convert '#{value}' into datetime") end end |
.file(value) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/choosy/converter.rb', line 73 def self.file(value) if File.file?(value) File.new(value) else raise Choosy::ValidationError.new("Unable to locate file: '#{value}'") end end |
.float(value) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/choosy/converter.rb', line 61 def self.float(value) begin return Float(value) rescue ArgumentError raise Choosy::ConversionError.new("Unable to convert '#{value}' into a float") end end |
.for(ty) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/choosy/converter.rb', line 18 def self.for(ty) if ty.is_a?(Symbol) && CONVERSIONS.has_key?(ty) val = CONVERSIONS[ty] if val return val else return ty end elsif ty.respond_to?(:convert) return ty end nil end |
.integer(value) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/choosy/converter.rb', line 53 def self.integer(value) begin return Integer(value) rescue ArgumentError raise Choosy::ConversionError.new("Unable to convert '#{value}' into an integer") end end |
.string(value) ⇒ Object
49 50 51 |
# File 'lib/choosy/converter.rb', line 49 def self.string(value) value # already set end |
.symbol(value) ⇒ Object
69 70 71 |
# File 'lib/choosy/converter.rb', line 69 def self.symbol(value) value.to_sym end |
.time(value) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/choosy/converter.rb', line 98 def self.time(value) begin return Time.parse(value) rescue ArgumentError raise Choosy::ConversionError.new("Unable to convert '#{value}' into a time") end end |
.yaml(value) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/choosy/converter.rb', line 81 def self.yaml(value) fd = file(value) begin return YAML::load_file(fd.path) rescue Error raise Choosy::ConversionError.new("Unable to load YAML from file: '#{value}'") end end |