Sha256: d71ebe407eee265bb1a503039e7cebb5cced0af617e3d64701afc420c707d07c

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

module Mongoid
  module Ids
    class Options
      def initialize(options = {})
        @options = merge_defaults validate_options(options)
      end

      def length
        @options[:length]
      end

      def retry_count
        @options[:retry_count]
      end

      def contains
        @options[:contains]
      end

      def field_name
        @options[:field_name]
      end

      def field_name=(param)
        return if param.nil? || param.empty?
        @options[:field_name] = param
      end

      def skip_finders?
        @options[:skip_finders]
      end

      def pattern
        @options[:pattern] ||= \
        case @options[:contains].to_sym
        when :alphanumeric
          "%s#{@options[:length]}"
        when :alpha
          "%w#{@options[:length]}"
        when :alpha_upper
          "%C#{@options[:length]}"
        when :alpha_lower
          "%c#{@options[:length]}"
        when :numeric
          "%d1,#{@options[:length]}"
        when :fixed_numeric
          "%d#{@options[:length]}"
        when :fixed_numeric_no_leading_zeros
          "%D#{@options[:length]}"
        end
      end

      private
      def validate_options(options)
        if options.has_key?(:retry)
          STDERR.puts "Mongoid::Ids Deprecation Warning: option `retry` has been renamed to `retry_count`. `:retry` will be removed in v2.1"
          options[:retry_count] = options[:retry]
        end
        options
      end

      def merge_defaults(options)
        {
          :length => 4,
          :retry_count => 3,
          :contains => :alphanumeric,
          :field_name => :_id,
          :skip_finders => false
        }.merge(options)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-ids-0.1.8 lib/mongoid/ids/options.rb
mongoid-ids-0.1.7 lib/mongoid/ids/options.rb