Module: CSpec::DataType

Defined in:
lib/cspec/data_type.rb

Constant Summary collapse

MATCHERS =
[
  { condition: /^-?\d+\.\d+$/, proc: proc { |input| input.to_f } },
  { condition: /^-?\d+$/, proc: proc { |input| input.to_i } },
  { condition: /true/, proc: proc { true } },
  { condition: /^\[.*\]$/, proc: proc { |input| eval(input) } },
  { condition: /false/, proc: proc { false } },
  { condition: /nil/, proc: proc { nil } },
  { condition: /^$/, proc: proc { nil } }
].freeze

Class Method Summary collapse

Class Method Details

.convert(input) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/cspec/data_type.rb', line 21

def self.convert(input)
  return input unless input.instance_of?(String)

  input = input.strip
  MATCHERS.each do |matcher|
    return matcher[:proc].call(input) if input.match?(matcher[:condition])
  end

  input.to_s
end

.convert_all(inputs) ⇒ Object



15
16
17
18
19
# File 'lib/cspec/data_type.rb', line 15

def self.convert_all(inputs)
  return nil unless inputs

  inputs.map { |input| convert(input) }
end