Class: CSVStepImporter::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations, ActiveModel::Validations::Callbacks
Defined in:
lib/csv_step_importer/base.rb

Direct Known Subclasses

Node

Defined Under Namespace

Classes: CSVFileImportError, CSVImportError

Instance Method Summary collapse

Instance Method Details

#assign_attributes(attributes) ⇒ Object



15
16
17
18
19
# File 'lib/csv_step_importer/base.rb', line 15

def assign_attributes(attributes)
  attributes.each do |key, value|
    send("#{key}=", value)
  end
end

#create_or_updateObject



25
26
27
# File 'lib/csv_step_importer/base.rb', line 25

def create_or_update
  raise "please extend and implement"
end

#inspectObject



51
52
53
# File 'lib/csv_step_importer/base.rb', line 51

def inspect
  to_s
end

#persisted?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/csv_step_importer/base.rb', line 21

def persisted?
  false
end

#saveObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/csv_step_importer/base.rb', line 29

def save
  run_callbacks :save do
    return false unless valid?

    status = !!::ActiveRecord::Base.transaction do
      raise ::ActiveRecord::Rollback unless create_or_update
      true
    end

    status
  end
end

#save!Object



42
43
44
# File 'lib/csv_step_importer/base.rb', line 42

def save!
  save || raise(CSVFileImportError.new(errors&.messages))
end

#to_sObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/csv_step_importer/base.rb', line 55

def to_s
  vars = self.instance_variables.map do |key|
    next if key == :@children
    next if key == :@parent
    next if key == :@env
    next if key == :@errors
    next if key == :@cache
    "#{key}=#{instance_variable_get(key).inspect}"
  end.compact.join(", ")
  "<#{self.class}: #{vars}>"
end

#update(attributes) ⇒ Object



46
47
48
49
# File 'lib/csv_step_importer/base.rb', line 46

def update(attributes)
  assign_attributes(attributes)
  save
end