Module: JSONAPI::Resources::Coercion::ClassMethods

Defined in:
lib/jsonapi/resources/coercion.rb

Constant Summary collapse

DEFAULT_PRECISION =
14

Instance Method Summary collapse

Instance Method Details

#coerce_as_big_decimal!(val) ⇒ Object Also known as: coerce_as_decimal!

Raises:



74
75
76
77
# File 'lib/jsonapi/resources/coercion.rb', line 74

def coerce_as_big_decimal!(val)
  raise CoercionError unless val.to_s.strip =~ /[0-9.]+/
  BigDecimal(val.to_s.strip, DEFAULT_PRECISION)
end

#coerce_as_boolean!(val) ⇒ Object



70
71
72
# File 'lib/jsonapi/resources/coercion.rb', line 70

def coerce_as_boolean!(val)
  (/^(false|f|no|n|0)$/i === val.to_s ? false : (/^(true|t|yes|y|1)$/i === val.to_s ? true : (raise CoercionError)))
end

#coerce_as_date!(val) ⇒ Object



81
82
83
84
85
# File 'lib/jsonapi/resources/coercion.rb', line 81

def coerce_as_date!(val)
  Date.parse(val)
rescue ArgumentError
  raise CoercionError
end

#coerce_as_date_time!(val) ⇒ Object Also known as: coerce_as_datetime!



87
88
89
90
91
# File 'lib/jsonapi/resources/coercion.rb', line 87

def coerce_as_date_time!(val)
  DateTime.parse(val)
rescue ArgumentError
  raise CoercionError
end

#coerce_as_float!(val) ⇒ Object



64
65
66
67
68
# File 'lib/jsonapi/resources/coercion.rb', line 64

def coerce_as_float!(val)
  Float(val)
rescue ArgumentError
  raise CoercionError
end

#coerce_as_integer!(val) ⇒ Object



52
53
54
55
56
# File 'lib/jsonapi/resources/coercion.rb', line 52

def coerce_as_integer!(val)
  Integer(val)
rescue ArgumentError
  raise CoercionError
end

#coerce_as_string!(val) ⇒ Object



58
59
60
61
62
# File 'lib/jsonapi/resources/coercion.rb', line 58

def coerce_as_string!(val)
  String(val)
rescue ArgumentError
  raise CoercionError
end

#coerce_as_time!(val) ⇒ Object



95
96
97
98
99
# File 'lib/jsonapi/resources/coercion.rb', line 95

def coerce_as_time!(val)
  Time.parse(val)
rescue ArgumentError
  raise CoercionError
end

#verify_filter(filter, raw, context = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jsonapi/resources/coercion.rb', line 20

def verify_filter(filter, raw, context = nil)
  verified_filter = super
  filter_type = _allowed_filters.fetch(filter, Hash.new)[:type]
  filter_name = verified_filter[0]
  if filter_type
    wrapped_filters = if verified_filter[1].empty? # nil or empty array
                        [nil]
                      else
                        Array.wrap(verified_filter[1])
                      end
    coerced_values = wrapped_filters.map do |val|
      begin
        if val.blank? # nil or empty string
          # if _allowed_filters.fetch(filter, Hash.new).fetch(:allow_nil, true)
          #   nil
          # else
          #   raise CoercionError
          # end
          nil
        else
          coerce(val, filter_type)
        end
      rescue CoercionError => _e
        raise JSONAPI::Exceptions::InvalidFilterValue.new(filter_name, val)
      end
    end
    [filter_name, coerced_values]
  else
    verified_filter
  end
end