Sha256: d26c1e868adce52350bf5e341757f5fa52609148be1171bb5fb9cd8310d0797a

Contents?: true

Size: 755 Bytes

Versions: 4

Compression:

Stored size: 755 Bytes

Contents

module Restspec::Schema::Types
  class DecimalStringType < DecimalType
    def example_for(attribute)
      super(attribute).to_s
    end

    def valid?(attribute, value)
      return false unless value.is_a?(String)
      decimal_regex.match(value).present?
    end

    private

    def decimal_regex
      @decimal_regex ||= build_regex
    end

    def build_regex
      integer_part_limit = to_regexp_limit(schema_options.fetch(:integer_part, nil))
      decimal_part_limit = to_regexp_limit(schema_options.fetch(:decimal_part, nil))
      /^\d#{integer_part_limit}([.,]\d#{decimal_part_limit})?$/
    end

    def to_regexp_limit(limit, default = '+')
      if limit.nil?
        default
      else
        "{#{limit}}"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
restspec-0.0.4 lib/restspec/schema/types/decimal_string_type.rb
restspec-0.0.3 lib/restspec/schema/types/decimal_string_type.rb
restspec-0.0.2 lib/restspec/schema/types/decimal_string_type.rb
restspec-0.0.1 lib/restspec/schema/types/decimal_string_type.rb