Class: Explicit::Type::Integer

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

Constant Summary collapse

ParseFromString =
->(value) do
  Integer(value)
rescue ::ArgumentError
  nil
end

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) ⇒ Integer

Returns a new instance of Integer.



6
7
8
9
10
11
# File 'lib/explicit/type/integer.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/integer.rb', line 4

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



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

def min
  @min
end

#negativeObject (readonly)

Returns the value of attribute negative.



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

def negative
  @negative
end

#positiveObject (readonly)

Returns the value of attribute positive.



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

def positive
  @positive
end

Instance Method Details

#json_schema(flavour) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/explicit/type/integer.rb', line 72

def json_schema(flavour)
  {
    type: "integer",
    minimum: min,
    maximum: max,
    description_topics: [
      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_blank
  }.compact_blank
end

#validate(value) ⇒ Object



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
48
49
50
51
52
53
54
55
56
# File 'lib/explicit/type/integer.rb', line 19

def validate(value)
  value =
    if value.is_a?(::Integer)
      value
    elsif value.is_a?(::String)
      ParseFromString[value]
    else
      nil
    end

  return error_i18n("integer") if value.nil?

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

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

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

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

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

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

  [ :ok, value ]
end