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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.set(name, value_or_proc) ⇒ Object

defines a method with the specified proc or a proc returning the value of the second attribute



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

def self.set(name, value_or_proc)
  procedure = value_or_proc.is_a?(Proc) ? value_or_proc : -> { value_or_proc }
  define_method name, procedure
end

Instance Method Details

#ancestorsObject



32
33
34
# File 'lib/csv_step_importer/base.rb', line 32

def ancestors
  @ancestors ||= [parent] + (parent ? parent.ancestors : [])
end

#assign_attributes(attributes) ⇒ Object



26
27
28
29
30
# File 'lib/csv_step_importer/base.rb', line 26

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

#create_or_updateObject



40
41
42
# File 'lib/csv_step_importer/base.rb', line 40

def create_or_update
  raise "please extend and implement"
end

#inspectObject



70
71
72
# File 'lib/csv_step_importer/base.rb', line 70

def inspect
  to_s
end

#persisted?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/csv_step_importer/base.rb', line 36

def persisted?
  false
end

#saveObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/csv_step_importer/base.rb', line 44

def save
  run_callbacks :save do
    return false unless valid?

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

    status
  end
end

#save!Object



61
62
63
# File 'lib/csv_step_importer/base.rb', line 61

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

#to_sObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/csv_step_importer/base.rb', line 74

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



65
66
67
68
# File 'lib/csv_step_importer/base.rb', line 65

def update(attributes)
  assign_attributes(attributes)
  save
end