Class: ValidatesBySchema::ValidationOption
- Inherits:
-
Object
- Object
- ValidatesBySchema::ValidationOption
- Defined in:
- lib/validates_by_schema/validation_option.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
column here must be an ActiveRecord column i.e.
Instance Method Summary collapse
- #decimal_max ⇒ Object
- #inclusion ⇒ Object
- #inclusion? ⇒ Boolean
-
#initialize(column) ⇒ ValidationOption
constructor
A new instance of ValidationOption.
- #integer_max ⇒ Object
- #length ⇒ Object
- #length? ⇒ Boolean
- #numericality ⇒ Object
- #numericality? ⇒ Boolean
- #presence ⇒ Object
- #presence? ⇒ Boolean
- #to_hash ⇒ Object
- #to_s ⇒ Object
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
#column ⇒ Object
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_max ⇒ Object
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 |
#inclusion ⇒ Object
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
46 47 48 |
# File 'lib/validates_by_schema/validation_option.rb', line 46 def inclusion? column.type == :boolean end |
#integer_max ⇒ Object
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 |
#length ⇒ Object
42 43 44 |
# File 'lib/validates_by_schema/validation_option.rb', line 42 def length { maximum: column.limit, allow_nil: true } end |
#length? ⇒ Boolean
38 39 40 |
# File 'lib/validates_by_schema/validation_option.rb', line 38 def length? [:string, :text].include?(column.type) && column.limit end |
#numericality ⇒ Object
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
18 19 20 |
# File 'lib/validates_by_schema/validation_option.rb', line 18 def numericality? [:integer, :decimal, :float].include? column.type end |
#presence ⇒ Object
14 15 16 |
# File 'lib/validates_by_schema/validation_option.rb', line 14 def presence !column.null end |
#presence? ⇒ Boolean
10 11 12 |
# File 'lib/validates_by_schema/validation_option.rb', line 10 def presence? presence && column.type != :boolean end |
#to_hash ⇒ Object
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_s ⇒ Object
68 69 70 |
# File 'lib/validates_by_schema/validation_option.rb', line 68 def to_s to_hash.inspect end |