Sha256: cd0256a35b9877179e240244a6ff1e492219682725736d3de9b51a2dae30697b

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

module DataMapper
  module Types
    class Enum < DataMapper::Type(Integer)
      def self.inherited(target)
        target.instance_variable_set("@primitive", self.primitive)
      end

      def self.flag_map
        @flag_map
      end

      def self.flag_map=(value)
        @flag_map = value
      end

      def self.new(*flags)
        enum = Class.new(Enum)
        enum.flag_map = {}

        flags.each_with_index do |flag, i|
          enum.flag_map[i + 1] = flag
        end

        enum
      end

      def self.[](*flags)
        new(*flags)
      end

      def self.load(value, property)
        self.flag_map[value]
      end

      def self.dump(value, property)
        case value
          when Array then value.collect { |v| self.dump(v, property) }
          else            self.flag_map.invert[value]
        end
      end

      def self.typecast(value, property)
        # Attempt to typecast using the class of the first item in the map.
        return value if value.nil?
        case self.flag_map[1]
          when Symbol then value.to_sym
          when String then value.to_s
          when Fixnum then value.to_i
          else             value
        end
      end
    end # class Enum
  end # module Types

  if defined?(Validate)
    module Validate
      module AutoValidate
        alias :orig_auto_generate_validations :auto_generate_validations
        def auto_generate_validations(property)
          orig_auto_generate_validations(property)
          return unless property.options[:auto_validation]

          if property.type.ancestors.include?(Types::Enum)
            validates_within property.name, options_with_message({:set => property.type.flag_map.values}, property, :within)
          end
        end
      end
    end
  end
end # module DataMapper

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
dm-types-0.9.10 lib/dm-types/enum.rb
dm-types-0.9.11 lib/dm-types/enum.rb
dm-types-0.9.9 lib/dm-types/enum.rb
mack-data_mapper-0.8.3 lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb
mack-data_mapper-0.8.3.1 lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb