Sha256: 69ec348d0e866933c6555ce6affa7b1275965c9ff602b43ad00511a73ffc6fe4

Contents?: true

Size: 716 Bytes

Versions: 3

Compression:

Stored size: 716 Bytes

Contents

module Yema
  class Rule
    class Length < self

      attr_reader :length

      def initialize(attribute_name, options={})
        super
        extract_options(options)
      end

      def self.required_options
        [:length]
      end

      def matches?(value)
        raise ArgumentError unless value.respond_to?(:length)

        case length
        when Range;  length.include?(value.length)
        when Fixnum; length == value.length
        end
      end

      private

      def extract_options(options)
        @length = options.fetch(:length)
        raise InvalidOptionError, "Length option only accept Fixnum or Range" unless [Fixnum, Range].include?(length.class)
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
yema-0.0.3 lib/yema/rule/length.rb
yema-0.0.2 lib/yema/rule/length.rb
yema-0.0.1 lib/yema/rule/length.rb