Class: ImpExp::DataValidators::Base
- Inherits:
-
Object
- Object
- ImpExp::DataValidators::Base
- Defined in:
- app/services/imp_exp/data_validators/base.rb
Instance Attribute Summary collapse
-
#active_record_class ⇒ Object
readonly
Returns the value of attribute active_record_class.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#row_offset ⇒ Object
readonly
Returns the value of attribute row_offset.
Instance Method Summary collapse
- #call(attributes = []) ⇒ Object
-
#initialize(model, row_offset = 0) ⇒ Base
constructor
A new instance of Base.
- #valid?(attribute_value, column_type) ⇒ Boolean
Constructor Details
#initialize(model, row_offset = 0) ⇒ Base
Returns a new instance of Base.
8 9 10 11 12 |
# File 'app/services/imp_exp/data_validators/base.rb', line 8 def initialize(model, row_offset = 0) @active_record_class = model.active_record_class @columns = model.active_record_class.columns.index_by(&:name).with_indifferent_access @row_offset = row_offset end |
Instance Attribute Details
#active_record_class ⇒ Object (readonly)
Returns the value of attribute active_record_class.
6 7 8 |
# File 'app/services/imp_exp/data_validators/base.rb', line 6 def active_record_class @active_record_class end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
6 7 8 |
# File 'app/services/imp_exp/data_validators/base.rb', line 6 def columns @columns end |
#row_offset ⇒ Object (readonly)
Returns the value of attribute row_offset.
6 7 8 |
# File 'app/services/imp_exp/data_validators/base.rb', line 6 def row_offset @row_offset end |
Instance Method Details
#call(attributes = []) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/services/imp_exp/data_validators/base.rb', line 14 def call(attributes = []) errors = [] attributes.each_with_index do |attrs, i| attrs.each do |attr_name, attr_value| column = columns[attr_name] next if column&.array? column_type = column&.type || :string enums = active_record_class.defined_enums[column&.name] if enums.present? possible_values = enums.keys + enums.values unless attr_value.in? possible_values errors << error_hash(row_offset + i + 1, attr_name, attr_value, possible_values) end else unless valid?(attr_value, column_type) errors << error_hash(row_offset + i + 1, attr_name, attr_value, column_type) end end end end errors end |
#valid?(attribute_value, column_type) ⇒ Boolean
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/services/imp_exp/data_validators/base.rb', line 42 def valid?(attribute_value, column_type) return true if attribute_value.to_s.blank? case column_type when :string return true when :boolean return false unless valid_boolean?(attribute_value) when :bigint, :integer return false unless valid_integer?(attribute_value) when :decimal, :float return false unless valid_decimal?(attribute_value) when :date, :datetime return false unless valid_datetime?(attribute_value) when :time return false unless valid_time?(attribute_value) end true end |