csv_row_model 1.0.0.beta1 → 1.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -1
- data/README.md +22 -9
- data/lib/csv_row_model/concerns/attributes_base.rb +86 -0
- data/lib/csv_row_model/concerns/check_options.rb +7 -9
- data/lib/csv_row_model/concerns/dynamic_columns_base.rb +47 -0
- data/lib/csv_row_model/concerns/export/attributes.rb +26 -0
- data/lib/csv_row_model/{export → concerns/export}/base.rb +1 -1
- data/lib/csv_row_model/concerns/export/dynamic_columns.rb +33 -0
- data/lib/csv_row_model/concerns/hidden_module.rb +15 -0
- data/lib/csv_row_model/concerns/import/attributes.rb +52 -0
- data/lib/csv_row_model/{import → concerns/import}/base.rb +10 -18
- data/lib/csv_row_model/{import → concerns/import}/csv_string_model.rb +2 -3
- data/lib/csv_row_model/concerns/import/dynamic_columns.rb +50 -0
- data/lib/csv_row_model/{import → concerns/import}/represents.rb +2 -2
- data/lib/csv_row_model/concerns/inspect.rb +5 -6
- data/lib/csv_row_model/{model/columns.rb → concerns/model/attributes.rb} +8 -31
- data/lib/csv_row_model/{model → concerns/model}/base.rb +1 -11
- data/lib/csv_row_model/{model → concerns/model}/children.rb +7 -1
- data/lib/csv_row_model/{model → concerns/model}/dynamic_columns.rb +23 -42
- data/lib/csv_row_model/export/file_model.rb +2 -15
- data/lib/csv_row_model/export.rb +3 -3
- data/lib/csv_row_model/import/file.rb +62 -22
- data/lib/csv_row_model/import/file_model.rb +3 -4
- data/lib/csv_row_model/import.rb +5 -9
- data/lib/csv_row_model/internal/attribute_base.rb +22 -0
- data/lib/csv_row_model/internal/concerns/column_shared.rb +21 -0
- data/lib/csv_row_model/internal/concerns/dynamic_column_shared.rb +28 -0
- data/lib/csv_row_model/internal/dynamic_column_attribute_base.rb +42 -0
- data/lib/csv_row_model/internal/export/attribute.rb +15 -0
- data/lib/csv_row_model/internal/export/dynamic_column_attribute.rb +21 -0
- data/lib/csv_row_model/{import/cell.rb → internal/import/attribute.rb} +4 -13
- data/lib/csv_row_model/{import → internal/import}/csv.rb +11 -13
- data/lib/csv_row_model/internal/import/dynamic_column_attribute.rb +33 -0
- data/lib/csv_row_model/{import → internal/import}/representation.rb +2 -2
- data/lib/csv_row_model/internal/model/dynamic_column_header.rb +22 -0
- data/lib/csv_row_model/internal/model/header.rb +25 -0
- data/lib/csv_row_model/model/file_model.rb +0 -1
- data/lib/csv_row_model/model.rb +5 -12
- data/lib/csv_row_model/validators/{boolean_format.rb → boolean_format_validator.rb} +1 -1
- data/lib/csv_row_model/validators/date_format_validator.rb +7 -0
- data/lib/csv_row_model/validators/date_time_format_validator.rb +7 -0
- data/lib/csv_row_model/validators/{default_change.rb → default_change_validator.rb} +0 -0
- data/lib/csv_row_model/validators/float_format_validator.rb +7 -0
- data/lib/csv_row_model/validators/integer_format_validator.rb +15 -0
- data/lib/csv_row_model/version.rb +1 -1
- data/lib/csv_row_model.rb +16 -37
- metadata +35 -33
- data/lib/csv_row_model/engine.rb +0 -5
- data/lib/csv_row_model/export/attributes.rb +0 -46
- data/lib/csv_row_model/export/cell.rb +0 -24
- data/lib/csv_row_model/export/dynamic_column_cell.rb +0 -29
- data/lib/csv_row_model/export/dynamic_columns.rb +0 -46
- data/lib/csv_row_model/import/attributes.rb +0 -66
- data/lib/csv_row_model/import/dynamic_column_cell.rb +0 -37
- data/lib/csv_row_model/import/dynamic_columns.rb +0 -59
- data/lib/csv_row_model/import/file/callbacks.rb +0 -17
- data/lib/csv_row_model/import/file/validations.rb +0 -43
- data/lib/csv_row_model/model/comparison.rb +0 -15
- data/lib/csv_row_model/model/dynamic_column_cell.rb +0 -44
- data/lib/csv_row_model/validators/date_format.rb +0 -9
- data/lib/csv_row_model/validators/date_time_format.rb +0 -9
- data/lib/csv_row_model/validators/float_format.rb +0 -9
- data/lib/csv_row_model/validators/integer_format.rb +0 -9
- data/lib/csv_row_model/validators/number_validator.rb +0 -16
- data/lib/csv_row_model/validators/validate_attributes.rb +0 -27
@@ -1,43 +0,0 @@
|
|
1
|
-
module CsvRowModel
|
2
|
-
module Import
|
3
|
-
class File
|
4
|
-
module Validations
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
include ActiveWarnings
|
8
|
-
include Validators::ValidateAttributes
|
9
|
-
|
10
|
-
included do
|
11
|
-
validate_attributes :csv
|
12
|
-
|
13
|
-
warnings do
|
14
|
-
validate { errors.add(:csv, "has header with #{csv.header.message}") unless csv.header.class == Array }
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# @return [Boolean] returns true, if the file should abort reading
|
19
|
-
def abort?
|
20
|
-
!valid? || !!current_row_model.try(:abort?)
|
21
|
-
end
|
22
|
-
|
23
|
-
# @return [Boolean] returns true, if the file should skip `current_row_model`
|
24
|
-
def skip?
|
25
|
-
!!current_row_model.try(:skip?)
|
26
|
-
end
|
27
|
-
|
28
|
-
protected
|
29
|
-
def _abort?
|
30
|
-
abort = abort?
|
31
|
-
run_callbacks(:abort) if abort
|
32
|
-
abort
|
33
|
-
end
|
34
|
-
|
35
|
-
def _skip?
|
36
|
-
skip = skip?
|
37
|
-
run_callbacks(:skip) if skip
|
38
|
-
skip
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
module CsvRowModel
|
2
|
-
module Model
|
3
|
-
class DynamicColumnCell
|
4
|
-
attr_reader :column_name, :row_model
|
5
|
-
|
6
|
-
def initialize(column_name, row_model)
|
7
|
-
@column_name = column_name
|
8
|
-
@row_model = row_model
|
9
|
-
end
|
10
|
-
|
11
|
-
def value
|
12
|
-
@value ||= row_model.class.format_dynamic_column_cells(unformatted_value, column_name, dynamic_column_index, row_model.context)
|
13
|
-
end
|
14
|
-
|
15
|
-
def dynamic_column_index
|
16
|
-
@dynamic_column_index ||= row_model.class.dynamic_column_index(column_name)
|
17
|
-
end
|
18
|
-
|
19
|
-
protected
|
20
|
-
|
21
|
-
def process_cell_method_name
|
22
|
-
self.class.process_cell_method_name(column_name)
|
23
|
-
end
|
24
|
-
|
25
|
-
# Calls the process_cell to return the value of a Cell given the args
|
26
|
-
def call_process_cell(*args)
|
27
|
-
row_model.public_send(process_cell_method_name, *args)
|
28
|
-
end
|
29
|
-
|
30
|
-
class << self
|
31
|
-
def process_cell_method_name(column_name)
|
32
|
-
column_name.to_s.singularize.to_sym
|
33
|
-
end
|
34
|
-
|
35
|
-
# define a method to process each cell of the attribute method
|
36
|
-
# process_cell = one cell
|
37
|
-
# attribute_method = many cells = [process_cell(), process_cell()...]
|
38
|
-
def define_process_cell(row_model_class, column_name, &block)
|
39
|
-
row_model_class.send(:define_method, process_cell_method_name(column_name), &block)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,9 +0,0 @@
|
|
1
|
-
class FloatFormatValidator < CsvRowModel::Validators::NumberValidator # :nodoc:
|
2
|
-
def validate_each(record, attribute, value)
|
3
|
-
before, after = before_after_decimal(value)
|
4
|
-
|
5
|
-
return if value.present? && value.to_f.to_s =~ /#{before}\.#{after.present? ? after : 0}/
|
6
|
-
|
7
|
-
record.errors.add(attribute, 'is not a Float format')
|
8
|
-
end
|
9
|
-
end
|
@@ -1,9 +0,0 @@
|
|
1
|
-
class IntegerFormatValidator < CsvRowModel::Validators::NumberValidator # :nodoc:
|
2
|
-
def validate_each(record, attribute, value)
|
3
|
-
before, after = before_after_decimal(value)
|
4
|
-
|
5
|
-
return if value.to_i.to_s == before && after.empty?
|
6
|
-
|
7
|
-
record.errors.add(attribute, 'is not a Integer format')
|
8
|
-
end
|
9
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module CsvRowModel
|
2
|
-
module Validators
|
3
|
-
class NumberValidator < ActiveModel::EachValidator # :nodoc:
|
4
|
-
def before_after_decimal(value)
|
5
|
-
value ||= ""
|
6
|
-
|
7
|
-
# if value == "0003434.1233000"
|
8
|
-
# before = "3434"; after = "1233"
|
9
|
-
before, decimal, after = value.partition(".")
|
10
|
-
before.sub!(/\A0+/, "")
|
11
|
-
after.sub!(/0+\z/, "")
|
12
|
-
[before, after]
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module CsvRowModel
|
2
|
-
module Validators
|
3
|
-
# adds validates_attributes method to validate the attributes of an attributes
|
4
|
-
module ValidateAttributes
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
class AttributeValidator < ActiveModel::EachValidator # :nodoc:
|
8
|
-
def validate_each(record, attribute, value)
|
9
|
-
return unless value && (record.try(:using_warnings?) ? value.unsafe? : value.invalid?)
|
10
|
-
record.errors.add(attribute)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class_methods do
|
15
|
-
protected
|
16
|
-
|
17
|
-
# Adds validation check to add errors any attribute of `attributes` passed is truthy and invalid.
|
18
|
-
# Inspired by: https://github.com/rails/rails/blob/2bb0abbec0e4abe843131f188129a1189b1bf714/activerecord/lib/active_record/validations/associated.rb#L46
|
19
|
-
#
|
20
|
-
# @param [Array<Symbol>] attributes array of attributes to validate their attributes
|
21
|
-
def validate_attributes(*attributes)
|
22
|
-
validates_with AttributeValidator, { attributes: attributes }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|