Sha256: 74a8a25ab9b45cd305e83b030f5fbcd52fb997f0fad9b60f3a45d3178e787cbd

Contents?: true

Size: 617 Bytes

Versions: 5

Compression:

Stored size: 617 Bytes

Contents

# frozen_string_literal: true

require "time"

module InferModel
  class Parsers::Time
    extend Callable
    extend Dry::Initializer

    ACCEPTABLE_TIME_FORMATS = %w[%T %R].freeze

    param :value
    option :allow_blank, default: -> { true }

    def call
      raise Parsers::Error, "value was blank which is not allowed" if value.nil? && !allow_blank
      return if value.nil? || value.empty?

      ACCEPTABLE_TIME_FORMATS.each do |format|
        return Time.strptime(value, format)
      rescue ArgumentError
        next
      end

      raise Parsers::Error, "'#{value}' is not a Time"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
infer_model-0.1.5 lib/infer_model/parsers/time.rb
infer_model-0.1.4 lib/infer_model/parsers/time.rb
infer_model-0.1.2 lib/infer_model/parsers/time.rb
infer_model-0.1.1 lib/infer_model/parsers/time.rb
infer_model-0.1.0 lib/infer_model/parsers/time.rb