Method: Helper.string_cast_to_type

Defined in:
lib/helper.rb

.string_cast_to_type(value, type) ⇒ Object

文字列データを指定された型にキャストする



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/helper.rb', line 198

def string_cast_to_type(value, type)
  result = nil
  case type
  when :boolean
    case value.strip.downcase
    when "true"
      result = true
    when "false"
      result = false
    else
      raise InvalidVariableType, type
    end
  when :integer
    begin
      result = Integer(value)
    rescue
      raise InvalidVariableType, type
    end
  when :float
    begin
      result = Float(value)
    rescue
      raise InvalidVariableType, type
    end
  when :directory, :file
    if File.method("#{type}?").call(value)
      result = File.expand_path(value)
    else
      raise InvalidVariableType, type
    end
  when :string, :select, :multiple
    result = value
  else
    raise UnknownVariableType, type
  end
  result
end