Class: Explicit::Type::DateRange
- Inherits:
-
Explicit::Type
- Object
- Explicit::Type
- Explicit::Type::DateRange
- Defined in:
- lib/explicit/type/date_range.rb
Constant Summary collapse
- FORMAT =
/^(\d{4}-\d{2}-\d{2})\.\.(\d{4}-\d{2}-\d{2})$/.freeze
- Eval =
->(value) do value.respond_to?(:call) ? value.call : value end
Instance Attribute Summary collapse
-
#max_date ⇒ Object
readonly
Returns the value of attribute max_date.
-
#max_range ⇒ Object
readonly
Returns the value of attribute max_range.
-
#min_date ⇒ Object
readonly
Returns the value of attribute min_date.
-
#min_range ⇒ Object
readonly
Returns the value of attribute min_range.
Attributes inherited from Explicit::Type
#auth_type, #default, #description, #nilable, #param_location
Instance Method Summary collapse
-
#initialize(min_range: nil, max_range: nil, min_date: nil, max_date: nil) ⇒ DateRange
constructor
A new instance of DateRange.
- #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_range: nil, max_range: nil, min_date: nil, max_date: nil) ⇒ DateRange
Returns a new instance of DateRange.
12 13 14 15 16 17 |
# File 'lib/explicit/type/date_range.rb', line 12 def initialize(min_range: nil, max_range: nil, min_date: nil, max_date: nil) @min_range = min_range @max_range = max_range @min_date = min_date @max_date = max_date end |
Instance Attribute Details
#max_date ⇒ Object (readonly)
Returns the value of attribute max_date.
4 5 6 |
# File 'lib/explicit/type/date_range.rb', line 4 def max_date @max_date end |
#max_range ⇒ Object (readonly)
Returns the value of attribute max_range.
4 5 6 |
# File 'lib/explicit/type/date_range.rb', line 4 def max_range @max_range end |
#min_date ⇒ Object (readonly)
Returns the value of attribute min_date.
4 5 6 |
# File 'lib/explicit/type/date_range.rb', line 4 def min_date @min_date end |
#min_range ⇒ Object (readonly)
Returns the value of attribute min_range.
4 5 6 |
# File 'lib/explicit/type/date_range.rb', line 4 def min_range @min_range end |
Instance Method Details
#json_schema(flavour) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/explicit/type/date_range.rb', line 81 def json_schema(flavour) { type: "string", pattern: FORMAT.inspect[1..-2], format: "date range", description_topics: [ swagger_i18n("date_range"), min_range&.then { swagger_i18n("date_range_min_range", min_range: _1.inspect) }, max_range&.then { swagger_i18n("date_range_max_range", max_range: _1.inspect) }, ] } 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 57 58 59 60 61 62 63 64 65 |
# File 'lib/explicit/type/date_range.rb', line 19 def validate(value) return error_i18n("string") if !value.is_a?(::String) match = FORMAT.match(value) return error_i18n("date_range_format") if !match date_1, date_2 = match.captures.map(&:to_date) if date_1.after?(date_2) return error_i18n("date_range_inverted") end if min_date min_date_value = Eval[min_date] if date_1 < min_date_value return error_i18n("date_range_min_date", min_date: min_date_value) end end if max_date max_date_value = Eval[max_date] if date_2 > max_date_value return error_i18n("date_range_max_date", max_date: max_date_value) end end if min_range diff_in_days = date_2 - date_1 + 1 if diff_in_days < min_range.in_days return error_i18n("date_range_min_range", min_range: min_range.inspect) end end if max_range diff_in_days = date_2 - date_1 + 1 if diff_in_days > max_range.in_days return error_i18n("date_range_max_range", max_range: max_range.inspect) end end [:ok, Range.new(date_1, date_2)] end |