Class: CsvRowModel::Import::Attribute

Inherits:
AttributeBase show all
Defined in:
lib/csv_row_model/internal/import/attribute.rb

Instance Attribute Summary collapse

Attributes inherited from AttributeBase

#column_name, #row_model

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AttributeBase

#formatted_value, #row_model_class

Methods included from ColumnShared

#context, #options

Constructor Details

#initialize(column_name, source_value, parsed_model_errors, row_model) ⇒ Attribute

Returns a new instance of Attribute.


8
9
10
11
12
# File 'lib/csv_row_model/internal/import/attribute.rb', line 8

def initialize(column_name, source_value, parsed_model_errors, row_model)
  @source_value = source_value
  @parsed_model_errors = parsed_model_errors
  super(column_name, row_model)
end

Instance Attribute Details

#parsed_model_errorsObject (readonly)

Returns the value of attribute parsed_model_errors


6
7
8
# File 'lib/csv_row_model/internal/import/attribute.rb', line 6

def parsed_model_errors
  @parsed_model_errors
end

#source_valueObject (readonly)

Returns the value of attribute source_value


6
7
8
# File 'lib/csv_row_model/internal/import/attribute.rb', line 6

def source_value
  @source_value
end

Class Method Details

.custom_check_options(options) ⇒ Object

Raises:

  • (ArgumentError)

53
54
55
# File 'lib/csv_row_model/internal/import/attribute.rb', line 53

def custom_check_options(options)
  raise ArgumentError.new("Use :parse OR :type option, but not both") if options[:parse] && options[:type]
end

.valid_optionsObject


57
58
59
# File 'lib/csv_row_model/internal/import/attribute.rb', line 57

def valid_options
  %i[type parse default]
end

Instance Method Details

#default?Boolean

Returns:


37
38
39
# File 'lib/csv_row_model/internal/import/attribute.rb', line 37

def default?
  options.has_key?(:default) && parsed_value.blank?
end

#default_changeObject


41
42
43
# File 'lib/csv_row_model/internal/import/attribute.rb', line 41

def default_change
  [formatted_value, default_value] if default?
end

#default_valueObject


30
31
32
33
34
35
# File 'lib/csv_row_model/internal/import/attribute.rb', line 30

def default_value
  @default_value ||= begin
    default = options[:default]
    default.is_a?(Proc) ? row_model.instance_exec(&default) : default
  end
end

#parse_lambdaLambda, Proc (protected)

Returns the Lambda/Proc given in the parse option or the one given by row_model_class.class_to_parse_lambda

Returns:

  • (Lambda, Proc)

    returns the Lambda/Proc given in the parse option or the one given by row_model_class.class_to_parse_lambda


48
49
50
# File 'lib/csv_row_model/internal/import/attribute.rb', line 48

def parse_lambda
  options[:parse] || row_model_class.class_to_parse_lambda[options[:type]]
end

#parsed_valueObject


21
22
23
24
25
26
27
28
# File 'lib/csv_row_model/internal/import/attribute.rb', line 21

def parsed_value
  @parsed_value ||= begin
    value = formatted_value
    value.present? ? row_model.instance_exec(formatted_value, &parse_lambda) : value
  end
rescue
  nil
end

#valueObject


14
15
16
17
18
19
# File 'lib/csv_row_model/internal/import/attribute.rb', line 14

def value
  @value ||= begin
    return unless parsed_model_errors.blank?
    default? ? default_value : parsed_value
  end
end