Sha256: bec273ef28cb07860fd10fcc371534c66820e2f8ed6b37d23434cd31f41b0151

Contents?: true

Size: 654 Bytes

Versions: 2

Compression:

Stored size: 654 Bytes

Contents

class Typero::StringType < Typero::Type
  opts :min, 'Minimun string length'
  opts :max, 'Maximun string length'

  def set
    @value = @value.to_s unless @value.is_a?(String)
    @value = @value.downcase if @opts[:downcase]
  end

  def validate
    error_for(:min_length_error, @opts[:min], @value.length) if @opts[:min] && @value.length < @opts[:min]
    error_for(:max_length_error, @opts[:max], @value.length) if @opts[:max] && @value.length > @opts[:max]
  end

  def db_field
    opts = {}
    opts[:limit]   = @opts[:max] || 255
    opts[:null]    = false if @opts[:required]
    opts[:default] = @opts[:default]
    [:string, opts]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
typero-0.8.1 ./lib/typero/type/types/string.rb
typero-0.8.0 ./lib/typero/type/types/string.rb