Class: ConvenientService::Examples::Standard::DateTime::Services::SafeParse
- Inherits:
-
Object
- Object
- ConvenientService::Examples::Standard::DateTime::Services::SafeParse
- Includes:
- Service::Configs::Standard
- Defined in:
- lib/convenient_service/examples/standard/date_time/services/safe_parse.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
-
#initialize(string:, format:) ⇒ SafeParse
constructor
A new instance of SafeParse.
- #result ⇒ Object
Constructor Details
#initialize(string:, format:) ⇒ SafeParse
Returns a new instance of SafeParse.
13 14 15 16 |
# File 'lib/convenient_service/examples/standard/date_time/services/safe_parse.rb', line 13 def initialize(string:, format:) @string = string @format = format end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
11 12 13 |
# File 'lib/convenient_service/examples/standard/date_time/services/safe_parse.rb', line 11 def format @format end |
#string ⇒ Object (readonly)
Returns the value of attribute string.
11 12 13 |
# File 'lib/convenient_service/examples/standard/date_time/services/safe_parse.rb', line 11 def string @string end |
Instance Method Details
#result ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/convenient_service/examples/standard/date_time/services/safe_parse.rb', line 18 def result success(date_time: ::DateTime.strptime(string, format)) rescue ::Date::Error => exception error( data: {exception: exception}, message: "Failed to parse `DateTime` object from `#{string}` with `#{format}`" ) end |