/
base.rb
54 lines (41 loc) · 1.06 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true
module Smuggle
module Importer
class Base
class << self
attr_writer :attributes
def inherited(subclass)
subclass.attributes = []
end
def attributes(*names)
@attributes.concat names
end
def attributes?
@attributes.any?
end
def csv_converters
Hash(header_converters: :symbol, converters: %i[all])
end
end
attr_reader :row, :model
def initialize(row, model)
@model = model
@row = row
end
def persist
raise NotImplementedError
end
def to_h
defined_attributes.each_with_object({}) do |attribute, hash|
hash[attribute.to_sym] = respond_to?(attribute) ? public_send(attribute) : row[attribute.to_sym]
end
end
private
def defined_attributes
return self.class.attributes if self.class.attributes?
return model.attribute_names if model.respond_to?(:attribute_names)
[]
end
end
end
end