Class: EasyJSONMatcher::DateValidator
- Defined in:
- lib/easy_json_matcher/date_validator.rb
Constant Summary collapse
- DEFAULT_DATE_FORMAT =
"%Y-%m-%d"
Instance Attribute Summary collapse
-
#date_format ⇒ Object
readonly
Returns the value of attribute date_format.
-
#string_validator ⇒ Object
readonly
Returns the value of attribute string_validator.
Attributes inherited from Validator
#content, #errors, #key, #required
Instance Method Summary collapse
- #_content_is_a_date? ⇒ Boolean
- #_content_is_a_string? ⇒ Boolean
- #_validate ⇒ Object
- #_validate_date ⇒ Object
- #_validate_string ⇒ Object
-
#initialize(opts = {}) ⇒ DateValidator
constructor
A new instance of DateValidator.
Methods inherited from Validator
#_check_content_type, #_check_required?, #_create_validator, #_no_errors?, #_post_initialise, #_set_content, #get_errors, #reset!, #valid?
Constructor Details
#initialize(opts = {}) ⇒ DateValidator
Returns a new instance of DateValidator.
8 9 10 11 12 |
# File 'lib/easy_json_matcher/date_validator.rb', line 8 def initialize(opts = {}) super(opts) @date_format = opts[:format] @string_validator = _create_validator(type: :string) end |
Instance Attribute Details
#date_format ⇒ Object (readonly)
Returns the value of attribute date_format.
5 6 7 |
# File 'lib/easy_json_matcher/date_validator.rb', line 5 def date_format @date_format end |
#string_validator ⇒ Object (readonly)
Returns the value of attribute string_validator.
5 6 7 |
# File 'lib/easy_json_matcher/date_validator.rb', line 5 def string_validator @string_validator end |
Instance Method Details
#_content_is_a_date? ⇒ Boolean
23 24 25 26 27 28 29 30 |
# File 'lib/easy_json_matcher/date_validator.rb', line 23 def _content_is_a_date? require 'date' begin Date.strptime(content, date_format) rescue ArgumentError false end end |
#_content_is_a_string? ⇒ Boolean
19 20 21 |
# File 'lib/easy_json_matcher/date_validator.rb', line 19 def _content_is_a_string? string_validator.valid? content end |
#_validate ⇒ Object
14 15 16 17 |
# File 'lib/easy_json_matcher/date_validator.rb', line 14 def _validate _validate_string _validate_date end |
#_validate_date ⇒ Object
40 41 42 |
# File 'lib/easy_json_matcher/date_validator.rb', line 40 def _validate_date errors << "#{content} is not a Date" unless _content_is_a_date? end |
#_validate_string ⇒ Object
36 37 38 |
# File 'lib/easy_json_matcher/date_validator.rb', line 36 def _validate_string errors << "#{content} must be provided as a String for Date validation" unless _content_is_a_string? end |