Class: Explicit::Type::Integer
- Inherits:
-
Explicit::Type
- Object
- Explicit::Type
- Explicit::Type::Integer
- Defined in:
- lib/explicit/type/integer.rb
Constant Summary collapse
- ParseFromString =
->(value) do Integer(value) rescue ::ArgumentError nil end
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#negative ⇒ Object
readonly
Returns the value of attribute negative.
-
#positive ⇒ Object
readonly
Returns the value of attribute positive.
Attributes inherited from Explicit::Type
#auth_type, #default, #description, #nilable, #param_location
Instance Method Summary collapse
-
#initialize(min: nil, max: nil, negative: nil, positive: nil) ⇒ Integer
constructor
A new instance of Integer.
- #json_schema(flavour) ⇒ Object
- #validate(value) ⇒ Object
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
#max ⇒ Object (readonly)
Returns the value of attribute max.
4 5 6 |
# File 'lib/explicit/type/integer.rb', line 4 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
4 5 6 |
# File 'lib/explicit/type/integer.rb', line 4 def min @min end |
#negative ⇒ Object (readonly)
Returns the value of attribute negative.
4 5 6 |
# File 'lib/explicit/type/integer.rb', line 4 def negative @negative end |
#positive ⇒ Object (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 |