有没有办法使DataMapper枚举类型为零?

时间:2014-07-11 13:51:13

标签: ruby-datamapper

第一次使用DataMapper Enum类型,我注意到枚举中的第一个值转换为1.我需要将其设置为零,以便与另一个也读取此数据库的应用程序中的另一个ORM层向后兼容。

1 个答案:

答案 0 :(得分:1)

你应该能够在dm-types中修补enum.rb,以支持这一点。您需要将initialize方法替换为稍加修改的副本,其中@flag_map[i+1]替换为@flag_map[i]

module DataMapper
  class Property
    class Enum < Object

      def initialize(model, name, options = {})
        @flag_map = {}

        flags = options.fetch(:flags, self.class.flags)
        flags.each_with_index do |flag, i|
          @flag_map[i] = flag
        end

        if self.class.accepted_options.include?(:set) && !options.include?(:set)
          options[:set] = @flag_map.values_at(*@flag_map.keys.sort)
        end

        super
      end # end initialize

    end # end class Enum
  end # end class Property
end # end module DataMapper
相关问题