Module: DataSeeder::Loader

Included in:
CSV, JSON, Txt, YAML
Defined in:
lib/data_seeder/loader.rb,
lib/data_seeder/loader/csv.rb,
lib/data_seeder/loader/txt.rb,
lib/data_seeder/loader/json.rb,
lib/data_seeder/loader/yaml.rb

Defined Under Namespace

Classes: CSV, JSON, Txt, YAML

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/data_seeder/loader.rb', line 3

def config
  @config
end

#key_attributeObject (readonly)

Returns the value of attribute key_attribute.



3
4
5
# File 'lib/data_seeder/loader.rb', line 3

def key_attribute
  @key_attribute
end

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/data_seeder/loader.rb', line 3

def klass
  @klass
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/data_seeder/loader.rb', line 3

def logger
  @logger
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/data_seeder/loader.rb', line 3

def path
  @path
end

#path_minus_extObject (readonly)

Returns the value of attribute path_minus_ext.



3
4
5
# File 'lib/data_seeder/loader.rb', line 3

def path_minus_ext
  @path_minus_ext
end

Instance Method Details

#call_config(name, *args) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/data_seeder/loader.rb', line 141

def call_config(name, *args)
  if val = config[name]
    if val.kind_of?(Proc)
      return val.call(*args)
    else
      return val
    end
  end
end

#call_method(name, *args) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/data_seeder/loader.rb', line 128

def call_method(name, *args)
  if self.respond_to?(name)
    return send(name, *args)
  elsif val = config[name]
    if val.kind_of?(Proc)
      return val.call(*args)
    else
      return val
    end
  end
  return nil
end

#default_configObject

Override with config defaults



16
17
18
# File 'lib/data_seeder/loader.rb', line 16

def default_config
  { purge: true }
end

#destroy_model(model) ⇒ Object

Allow override for potential soft-delete



102
103
104
105
# File 'lib/data_seeder/loader.rb', line 102

def destroy_model(model)
  log_destroy(model)
  model.destroy
end

#destroy_models(klass, ids) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/data_seeder/loader.rb', line 36

def destroy_models(klass, ids)
  ids.each do |id|
    if model = klass.find_by(id: id)
      destroy_model(model)
    end
  end
end

#initialize(config) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/data_seeder/loader.rb', line 5

def initialize(config)
  @config         = default_config.merge(config)
  @logger         = @config[:logger] || DataSeeder.config.logger
  @key_attribute  = @config[:key_attribute] || :id
  @klass          = @config[:klass]
  @path           = @config[:path]
  @path_minus_ext = @config[:path_minus_ext]
  @old_ids        = Set.new
end

#line_numberObject

Override for applicable loaders



66
67
68
# File 'lib/data_seeder/loader.rb', line 66

def line_number
  raise "This loader doesn't support line_number"
end

#load(io) ⇒ Object



61
62
63
# File 'lib/data_seeder/loader.rb', line 61

def load(io)
  throw 'Must override load'
end

#log_destroy(model) ⇒ Object



115
116
117
# File 'lib/data_seeder/loader.rb', line 115

def log_destroy(model)
  logger.info { "Destroying #{model_info(model)}" }
end

#log_indent(&block) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/data_seeder/loader.rb', line 119

def log_indent(&block)
  # If we used the default logger, then indent, else no-op
  if @logger == DataSeeder.config.logger
    DataSeeder.config.log_indent(&block)
  else
    yield
  end
end

#log_save(model) ⇒ Object



107
108
109
# File 'lib/data_seeder/loader.rb', line 107

def log_save(model)
  logger.info { "Saving #{model_info(model)}" }
end

#log_update(model) ⇒ Object



111
112
113
# File 'lib/data_seeder/loader.rb', line 111

def log_update(model)
  logger.info { "Updating #{model_info(model, model.changes)}" }
end

#model_info(model, changes = nil) ⇒ Object

The information displayed when creating, updating, or destroying a model. The changes argument will be the model.changes on an update.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/data_seeder/loader.rb', line 46

def model_info(model, changes=nil)
  if changes
    if attr = config[:update_display_method]
      "#{model.send(attr)}: #{changes.inspect}"
    elsif @key_attribute.kind_of?(Enumerable)
      label = @key_attribute.map {|k| "#{k}=#{model.send(k)}"}.join(' ')
      "#{label}: #{changes.inspect}"
    else
      "#{model.send(@key_attribute)}: #{changes.inspect}"
    end
  else
    model.inspect
  end
end

#process(io) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/data_seeder/loader.rb', line 20

def process(io)
  call_config(:setup)
  setup
  load(io)
  teardown
  call_config(:teardown)
end

#save(attr) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/data_seeder/loader.rb', line 70

def save(attr)
  attr = call_method(:postprocess, attr) || attr
  if config[:use_line_number_as_id]
    find_hash = { @key_attribute => self.line_number }
  elsif @key_attribute.kind_of?(Enumerable)
    find_hash = {}
    @key_attribute.each do |k|
      find_hash[k] = attr[k.to_s] || attr[k.to_sym]
    end
  else
    key = attr[@key_attribute.to_s] || attr[@key_attribute.to_sym]
    raise "No #{@key_attribute} in #{attr.inspect}" unless key
    find_hash = { @key_attribute => key }
  end
  model = self.klass.find_or_initialize_by(find_hash)
  model.attributes = attr
  save_model(model)
  return model
end

#save_model(model) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/data_seeder/loader.rb', line 90

def save_model(model)
  if model.new_record?
    log_save(model)
  else
    @old_ids.delete(model.id)
    return unless model.changed?
    log_update(model)
  end
  model.save!
end

#setupObject



28
29
30
# File 'lib/data_seeder/loader.rb', line 28

def setup
  @old_ids = klass.all.pluck(:id).to_set if config[:purge]
end

#teardownObject



32
33
34
# File 'lib/data_seeder/loader.rb', line 32

def teardown
  destroy_models(klass, @old_ids)
end