Class: ValidatesBySchema::ValidationOption

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_by_schema/validation_option.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column) ⇒ ValidationOption

Returns a new instance of ValidationOption.



6
7
8
# File 'lib/validates_by_schema/validation_option.rb', line 6

def initialize(column)
  @column = column
end

Instance Attribute Details

#columnObject

column here must be an ActiveRecord column i.e. MyARModel.columns.first



4
5
6
# File 'lib/validates_by_schema/validation_option.rb', line 4

def column
  @column
end

Instance Method Details

#decimal_maxObject



58
59
60
# File 'lib/validates_by_schema/validation_option.rb', line 58

def decimal_max
  10.0**(column.precision - column.scale) - 10.0**(-column.scale)
end

#inclusionObject



50
51
52
# File 'lib/validates_by_schema/validation_option.rb', line 50

def inclusion
  { in: [true, false], allow_nil: column.null }
end

#inclusion?Boolean

Returns:



46
47
48
# File 'lib/validates_by_schema/validation_option.rb', line 46

def inclusion?
  column.type == :boolean
end

#integer_maxObject



54
55
56
# File 'lib/validates_by_schema/validation_option.rb', line 54

def integer_max
  (2**(8 * column.limit)) / 2 if column.limit
end

#lengthObject



42
43
44
# File 'lib/validates_by_schema/validation_option.rb', line 42

def length
  { maximum: column.limit, allow_nil: true }
end

#length?Boolean

Returns:



38
39
40
# File 'lib/validates_by_schema/validation_option.rb', line 38

def length?
  [:string, :text].include?(column.type) && column.limit
end

#numericalityObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/validates_by_schema/validation_option.rb', line 22

def numericality
  numericality = {}
  if column.type == :integer
    numericality[:only_integer] = true
    if integer_max
      numericality[:less_than] = integer_max
      numericality[:greater_than] = -integer_max
    end
  elsif column.type == :decimal
    numericality[:less_than_or_equal_to] = decimal_max
    numericality[:greater_than_or_equal_to] = -decimal_max
  end
  numericality[:allow_nil] = true
  numericality
end

#numericality?Boolean

Returns:



18
19
20
# File 'lib/validates_by_schema/validation_option.rb', line 18

def numericality?
  [:integer, :decimal, :float].include? column.type
end

#presenceObject



14
15
16
# File 'lib/validates_by_schema/validation_option.rb', line 14

def presence
  !column.null
end

#presence?Boolean

Returns:



10
11
12
# File 'lib/validates_by_schema/validation_option.rb', line 10

def presence?
  presence && column.type != :boolean
end

#to_hashObject



62
63
64
65
66
# File 'lib/validates_by_schema/validation_option.rb', line 62

def to_hash
  [:presence, :numericality, :length, :inclusion].inject({}) do |h, k|
    send(:"#{k}?") ? h.merge(k => send(k)) : h
  end
end

#to_sObject



68
69
70
# File 'lib/validates_by_schema/validation_option.rb', line 68

def to_s
  to_hash.inspect
end