Class: Explicit::Type::BigDecimal

Inherits:
Explicit::Type show all
Defined in:
lib/explicit/type/big_decimal.rb

Instance Attribute Summary collapse

Attributes inherited from Explicit::Type

#auth_type, #default, #description, #nilable, #param_location

Instance Method Summary collapse

Methods inherited from Explicit::Type

#auth_basic?, #auth_bearer?, build, #error_i18n, #mcp_schema, #merge_base_json_schema, #param_location_body?, #param_location_path?, #param_location_query?, #required?, #swagger_i18n, #swagger_schema

Constructor Details

#initialize(min: nil, max: nil, negative: nil, positive: nil) ⇒ BigDecimal

Returns a new instance of BigDecimal.



6
7
8
9
10
11
# File 'lib/explicit/type/big_decimal.rb', line 6

def initialize(min: nil, max: nil, negative: nil, positive: nil)
  @min = min
  @max = max
  @negative = negative
  @positive = positive
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



4
5
6
# File 'lib/explicit/type/big_decimal.rb', line 4

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



4
5
6
# File 'lib/explicit/type/big_decimal.rb', line 4

def min
  @min
end

#negativeObject (readonly)

Returns the value of attribute negative.



4
5
6
# File 'lib/explicit/type/big_decimal.rb', line 4

def negative
  @negative
end

#positiveObject (readonly)

Returns the value of attribute positive.



4
5
6
# File 'lib/explicit/type/big_decimal.rb', line 4

def positive
  @positive
end

Instance Method Details

#json_schema(flavour) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/explicit/type/big_decimal.rb', line 63

def json_schema(flavour)
  {
    type: "string",
    pattern: /^\d*\.?\d*$/.inspect[1..-2],
    format: "decimal number",
    description_topics: [
      swagger_i18n("big_decimal_format"),
      min&.then { swagger_i18n("big_decimal_min", min: _1) },
      max&.then { swagger_i18n("big_decimal_max", max: _1) },
      positive == false ? swagger_i18n("number_not_positive") : nil,
      positive == true ? swagger_i18n("number_only_positive") : nil,
      negative == false ? swagger_i18n("number_not_negative") : nil,
      negative == true ? swagger_i18n("number_only_negative") : nil
    ].compact
  }
end

#validate(value) ⇒ Object



13
14
15
16
17
18
19
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
# File 'lib/explicit/type/big_decimal.rb', line 13

def validate(value)
  if !value.is_a?(::BigDecimal) && !value.is_a?(::String) && !value.is_a?(::Integer)
    return error_i18n("big_decimal")
  end

  decimal_value = BigDecimal(value)

  if min && decimal_value < min
    return error_i18n("min", min:)
  end

  if max && decimal_value > max
    return error_i18n("max", max:)
  end

  if negative == false && decimal_value < 0
    return error_i18n("not_negative")
  end

  if negative == true && decimal_value >= 0
    return error_i18n("only_negative")
  end

  if positive == false && decimal_value > 0
    return error_i18n("not_positive")
  end

  if positive == true && decimal_value <= 0
    return error_i18n("only_positive")
  end

  [:ok, decimal_value]
rescue ArgumentError
  return error_i18n("big_decimal")
end