Sha256: c6656b05a4b92cef424c956c125726adbb98561a4c6e63342a16429b50369a3b

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'bigdecimal'
require 'time'
require 'date'

module Ohm
  module Types
    class String < ::String
      def self.[](value)
        value
      end
    end

    class Time < ::Time
      def self.[](value)
        return value if value.to_s.empty?

        ret = parse(value)
        ret.to_s == value ? ret : value
      end
    end

    class Date < ::Date
      def self.[](value)
        return value if value.to_s.empty?

        parse(value)
      rescue ArgumentError
        value
      end
    end

    class Decimal
      CANONICAL = /^(\d+)?(\.\d+)?(E[+\-]\d+)?$/

      def self.[](value)
        return value if value.to_s.empty?

        if value.to_s =~ CANONICAL
          BigDecimal(value)
        else
          value
        end
      end
    end

    class Float < ::Float
      def self.[](value)
        return value if value.to_s.empty?

        Float(value)
      rescue ArgumentError
        value
      end
    end

    class Integer < ::Integer
      def self.[](value)
        return value if value.to_s.empty?

        Integer(value)
      rescue ArgumentError
        value
      end
    end
  end

  module Typecast
    include Types

    def self.included(base)
      base.extend Macros
    end

    module Macros
      def attribute(name, type = String)
        define_method(name) do
          type[read_local(name)]
        end

        define_method(:"#{name}=") do |value|
          write_local(name, value && value.to_s)
        end

        attributes << name unless attributes.include?(name)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ohm-contrib-0.0.5 lib/ohm/contrib/typecast.rb