Sha256: 4154a389782038030f31735f90ce018765e9c6f7d068c68fa2a7ed8f9def9ea3

Contents?: true

Size: 1.02 KB

Versions: 9

Compression:

Stored size: 1.02 KB

Contents

require 'selectable_attr_rails/validatable'

module SelectableAttrRails
  module Validatable
    module Base
      def self.included(mod)
        mod.extend(ClassMethods)
        mod.instance_eval do
          alias :define_enum_without_validatable :define_enum
          alias :define_enum :define_enum_with_validatable
        end
      end

      module ClassMethods
        def define_enum_with_validatable(context)
          enum = context[:enum]
          if options = enum.validates_format_options
            options[:with] = Regexp.union(*enum.entries.map{|entry| /#{Regexp.escape(entry.id)}/})
            entry_format = options.delete(:entry_format) || '#{entry.name}'
            entries = enum.entries.map{|entry| instance_eval("\"#{entry_format}\"")}.join(', ')
            message = options.delete(:message) || 'is invalid, must be one of #{entries}'
            options[:message] = instance_eval("\"#{message}\"")
            validates_format_of(context[:attr], options)
          end
        end
      end


    end
  end
end


Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
selectable_attr_rails-0.3.15 lib/selectable_attr_rails/validatable/base.rb
selectable_attr_rails-0.3.14 lib/selectable_attr_rails/validatable/base.rb
selectable_attr_rails-0.3.13 lib/selectable_attr_rails/validatable/base.rb
selectable_attr_rails-0.3.12 lib/selectable_attr_rails/validatable/base.rb
selectable_attr_rails-0.3.11 lib/selectable_attr_rails/validatable/base.rb
selectable_attr_rails-0.3.10 lib/selectable_attr_rails/validatable/base.rb
selectable_attr_rails-0.3.9 lib/selectable_attr_rails/validatable/base.rb
selectable_attr_rails-0.3.8 lib/selectable_attr_rails/validatable/base.rb
selectable_attr_rails-0.3.7 lib/selectable_attr_rails/validatable/base.rb