Sha256: d4a710a67b561e3f8848084a45f45e48004720e3851f4e4826becce35908a59a

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

module RiceBubble
  class Attributes
    class String < Base
      attr_reader :min, :max, :format

      def initialize(min: nil, max: nil, format: nil, &)
        super(&)
        @min = min
        @max = max
        @format = format
      end

      def valid?(value)
        value.is_a?(::String) &&
          !min&.send(:>, value.length) &&
          !max&.send(:<, value.length) &&
          (!format || format.match?(value))
      end

      def call(value, path: '')
        super(value.to_s, path:)
      end

      def coerce(value)
        value.respond_to?(:to_s) ? value.to_s : value
      end

      def description
        result = super

        if min && max
          result = "#{result} between #{min} and #{max} characters long"
        elsif min
          result = "#{result} of at least #{min} characters"
        elsif max
          result = "#{result} of up to #{max} characters"
        end

        result = "#{result} matching #{format.inspect}" if format

        result
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rice_bubble-0.1.2 lib/rice_bubble/attributes/string.rb
rice_bubble-0.1.1 lib/rice_bubble/attributes/string.rb
rice_bubble-0.1.0 lib/rice_bubble/attributes/string.rb