Class: Summoner::Feature

Inherits:
ApplicationRecord show all
Defined in:
app/models/summoner/feature.rb

Instance Method Summary collapse

Instance Method Details

#cast_value(raw_value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/summoner/feature.rb', line 12

def cast_value(raw_value)
  case value_type
  when 'boolean'
    raw_value.to_s.strip.downcase == 'true'
  when 'integer'
    raw_value.to_i
  when 'float'
    raw_value.to_f
  when 'json'
    raw_value.is_a?(String) ? JSON.parse(raw_value) : raw_value
  else
    raw_value.to_s
  end
rescue
  raw_value
end

#valid_value?(raw_value) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/summoner/feature.rb', line 29

def valid_value?(raw_value)
  str_val = raw_value.to_s.strip
  
  case value_type
  when 'boolean'
    str_val.downcase.in?(['true', 'false'])
  when 'integer'
    str_val.match?(/\A-?\d+\z/)
  when 'float'
    str_val.match?(/\A-?\d+(\.\d+)?\z/)
  when 'json'
    begin
      JSON.parse(str_val)
      true
    rescue JSON::ParserError
      false
    end
  else
    true
  end
end