Class: ConvenientService::Examples::Standard::V1::DateTime::Services::SafeParse

Inherits:
Object
  • Object
show all
Includes:
Service::Configs::Standard::V1
Defined in:
lib/convenient_service/examples/standard/v1/date_time/services/safe_parse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string:, format:) ⇒ SafeParse

Returns a new instance of SafeParse.



14
15
16
17
# File 'lib/convenient_service/examples/standard/v1/date_time/services/safe_parse.rb', line 14

def initialize(string:, format:)
  @string = string
  @format = format
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



12
13
14
# File 'lib/convenient_service/examples/standard/v1/date_time/services/safe_parse.rb', line 12

def format
  @format
end

#stringObject (readonly)

Returns the value of attribute string.



12
13
14
# File 'lib/convenient_service/examples/standard/v1/date_time/services/safe_parse.rb', line 12

def string
  @string
end

Instance Method Details

#resultObject



19
20
21
22
23
24
25
26
# File 'lib/convenient_service/examples/standard/v1/date_time/services/safe_parse.rb', line 19

def result
  success(date_time: ::DateTime.strptime(string, format))
rescue ::Date::Error => exception
  failure(
    data: {exception: exception},
    message: "Failed to parse `DateTime` object from `#{string}` with `#{format}`"
  )
end