Sha256: 3da94d8d64c3f187af6f613757f406f11ef3634ef49be194369887b4bbd509ee

Contents?: true

Size: 680 Bytes

Versions: 26

Compression:

Stored size: 680 Bytes

Contents

module ApiResource

  module Typecast

    ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/

    module DateTypecaster

      def self.from_api(value)
        return value if value.is_a?(Date)

        value = value.to_s
        if value =~ ApiResource::Typecast::ISO_DATE
          return self.new_date($1.to_i, $2.to_i, $3.to_i)
        end

        self.new_date(*::Date._parse(value, false).values_at(:year, :mon, :mday))
      end

      def self.to_api(value)
        return value.to_s
      end

      protected

      def self.new_date(year, month, day)
        return nil unless year && year != 0
        return Date.new(year, month, day) rescue nil
      end

    end

  end

end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
api_resource-0.6.5 lib/api_resource/typecasters/date_typecaster.rb
api_resource-0.6.4 lib/api_resource/typecasters/date_typecaster.rb
api_resource-0.6.3 lib/api_resource/typecasters/date_typecaster.rb
api_resource-0.6.2 lib/api_resource/typecasters/date_typecaster.rb
api_resource-0.6.1 lib/api_resource/typecasters/date_typecaster.rb
api_resource-0.6.0 lib/api_resource/typecasters/date_typecaster.rb