Sha256: 44eb3fa90c633ecf6ef6132e5e60e3fa1214e3b3650789099f8dc688a7d4c028

Contents?: true

Size: 1.97 KB

Versions: 29

Compression:

Stored size: 1.97 KB

Contents

class Eco::API::Common::People::DefaultParsers::DateParser < Eco::API::Common::Loaders::Parser
  attribute :date

  MIN_DATE = '1901-01-02'.freeze
  MAX_DATE = '2099-12-31'.freeze

  def parser(value, deps)
    parse_date(value, attr: deps['attr'])
  end

  def serializer(value, _deps)
    serialize_date(value)
  end

  private

  def parse_date(value, attr:)
    return value.map {|val| parse_date(val, attr: attr)}.compact if value.is_a?(Enumerable)
    return nil if blank?(value)

    value = value.to_s.strip
    return wrong!(value, attr: attr) unless date?(value)

    date = Date.parse(value)
    return date if valid_range?(date)

    wrong!(value, attr: attr, desc: 'Date out of range (1900-2099). Given:')
  rescue TypeError, Date::Error
    nil
  end

  def serialize_date(value)
    return value.map {|val| serialize_date(val)}.compact if value.is_a?(Enumerable)
    return nil   if blank?(value)
    return value if value.is_a?(String)
    return nil   unless [Date, Time].any? {|type| value.is_a?(type)}
    value&.strftime('%Y-%m-%d')
  rescue TypeError, Date::Error
    nil
  end

  def date?(value)
    return true if value.nil?
    return true if value.to_s.strip.empty?
    Date.parse(value)
    true
  rescue TypeError, Date::Error
    false
  end

  def valid_range?(date)
    return true  if date.nil?
    return false unless min_date <= date
    max_date >= date
  end

  def wrong!(value, attr:, desc: "Can't make a date out of")
    return if wrong(attr).key?(value)
    wrong(attr)[value] = value
    log(:warn) { "#{desc} '#{value}' for '#{attr}'" }
    nil
  end

  def wrong(attr = nil)
    @wrong ||= {}
    return @wrong if attr.nil?
    @wrong[attr] ||= {}
  end

  def min_date
    @min_date ||= Date.parse(MIN_DATE)
  end

  def max_date
    @max_date ||= Date.parse(MAX_DATE)
  end

  def blank?(value)
    return true if value.nil?
    return true if value.to_s.strip.empty?
    false
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
eco-helpers-3.0.14 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.13 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.12 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.11 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.10 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.9 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.8 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.7 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.6 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.5 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.4 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.3 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.2 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.1 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-3.0.0 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-2.7.25 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-2.7.24 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-2.7.23 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-2.7.22 lib/eco/api/common/people/default_parsers/date_parser.rb
eco-helpers-2.7.21 lib/eco/api/common/people/default_parsers/date_parser.rb