Sha256: e497c78c1af42dddd8edc242a6f43a7fb8923406e9f966e6c2724f51d5a2b171

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module Structural
  module Model
    module TypeCasts
      def self.cast(type, value)
        casts.fetch(type, Identity).new(value).cast
      end

      protected

      def self.register(cast)
        casts[cast.type] = cast
      end

      private

      def self.casts
        @casts ||= {}
      end

      class Cast
        def initialize(value)
          @value = value
        end

        def cast
          value.is_a?(type) ? value : conversion
        end

        def type
          self.class.type
        end

        def conversion
          raise NotImplementedError
        end

        private

        def self.type
          raise NotImplementedError
        end

        attr_reader :value
      end

      class Identity < Cast
        def cast
          value
        end
      end

      class Date < Cast
        def self.type
          ::Date
        end

        def conversion
          ::Date.parse value
        end

        TypeCasts.register(self)
      end

      class Time < Cast
        def self.type
          ::Time
        end

        def conversion
          ::Time.parse value
        end

        TypeCasts.register(self)
      end

      class Money < Cast
        def self.type
          ::Money
        end

        def conversion
          ::Money.new value.to_i
        end

        TypeCasts.register(self)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
structural-0.0.1 lib/structural/model/type_casts.rb