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
#file_config ⇒ Object
Returns the value of attribute file_config.
5
6
7
|
# File 'lib/data_seeder/loader.rb', line 5
def file_config
@file_config
end
|
#key_attribute ⇒ Object
Returns the value of attribute key_attribute.
5
6
7
|
# File 'lib/data_seeder/loader.rb', line 5
def key_attribute
@key_attribute
end
|
#path ⇒ Object
Returns the value of attribute path.
6
7
8
|
# File 'lib/data_seeder/loader.rb', line 6
def path
@path
end
|
#path_minus_ext ⇒ Object
Returns the value of attribute path_minus_ext.
6
7
8
|
# File 'lib/data_seeder/loader.rb', line 6
def path_minus_ext
@path_minus_ext
end
|
Instance Method Details
#call_file_method(name, *args) ⇒ Object
120
121
122
123
124
125
126
127
128
|
# File 'lib/data_seeder/loader.rb', line 120
def call_file_method(name, *args)
if method = @file_config[name]
return method.call(*args)
else
class_method = "data_seeder_#{name}"
return self.klass.send(class_method, *args) if @klass.respond_to?(class_method)
end
return nil
end
|
#config ⇒ Object
19
20
21
|
# File 'lib/data_seeder/loader.rb', line 19
def config
DataSeeder.config
end
|
#initialize(options = {}) ⇒ Object
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/data_seeder/loader.rb', line 8
def initialize(options={})
@only = options[:only]
@except = options[:except]
if options.has_key?(:purge)
@purge = options[:purge]
else
@purge = true
end
@old_keys = []
end
|
#klass ⇒ Object
27
28
29
30
|
# File 'lib/data_seeder/loader.rb', line 27
def klass
@path_minus_ext.classify.constantize rescue nil
end
|
#line_number ⇒ Object
92
93
94
|
# File 'lib/data_seeder/loader.rb', line 92
def line_number
$INPUT_LINE_NUMBER
end
|
#load(fin) ⇒ Object
88
89
90
|
# File 'lib/data_seeder/loader.rb', line 88
def load(fin)
throw 'Must override load'
end
|
#load_file_config(fin) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/data_seeder/loader.rb', line 76
def load_file_config(fin)
config_line = fin.readline
if match = config_line.match(/^\s*#\s*config:(.*)/)
@file_config = eval(match[1])
else
fin.seek(0)
if self.klass && self.klass.respond_to?(:data_seeder_config)
@file_config = self.klass.data_seeder_config
end
end
end
|
#logger ⇒ Object
23
24
25
|
# File 'lib/data_seeder/loader.rb', line 23
def logger
DataSeeder.logger
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.
67
68
69
70
71
72
73
74
|
# File 'lib/data_seeder/loader.rb', line 67
def model_info(model, changes=nil)
if changes
attr = @file_config[:update_display_method] || @key_attribute
"#{model.send(attr)}: #{changes.inspect}"
else
model.inspect
end
end
|
#process(path) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/data_seeder/loader.rb', line 32
def process(path)
@path = path
dot_index = @path.rindex('.')
@path_minus_ext = @path[0, dot_index]
@file_config = {}
cfg_file = "#{@path_minus_ext}.cfg"
@file_config = eval(File.read(cfg_file)) if File.exist?(cfg_file)
File.open(@path, 'r') do |fin|
load_file_config(fin) if @file_config.empty?
@file_config = ActiveSupport::HashWithIndifferentAccess.new(@file_config)
setup
load(fin)
teardown
end
call_file_method(:teardown)
end
|
#save(attr) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/data_seeder/loader.rb', line 96
def save(attr)
if @file_config[:use_line_number_as_id]
key = self.line_number
else
key = attr[@key_attribute.to_s] || attr[@key_attribute.to_sym]
raise "No #{@key_attribute} in #{attr.inspect}" unless key
end
@old_keys.delete(key.to_s)
model = self.klass.find_or_initialize_by(@key_attribute => key)
model.attributes = attr
save_model(model)
end
|
#save_model(model) ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/data_seeder/loader.rb', line 109
def save_model(model)
if model.new_record?
logger.info { " Saving #{model_info(model)}" }
else
changes = model.changes
return if changes.empty?
logger.info { " Updating #{model_info(model, changes)}" }
end
model.save!
end
|
#setup ⇒ Object
49
50
51
52
53
54
|
# File 'lib/data_seeder/loader.rb', line 49
def setup
@key_attribute = self.file_config[:key_attribute] || :id
@old_keys = self.klass.all.pluck(@key_attribute).map(&:to_s) if @purge
logger.info { "Loading #{@path}" }
call_file_method(:setup)
end
|
#teardown ⇒ Object
56
57
58
59
60
61
62
63
|
# File 'lib/data_seeder/loader.rb', line 56
def teardown
@old_keys.each do |key|
if model = self.klass.find_by(@key_attribute => key)
logger.info { " Destroying #{model_info(model)}"}
model.destroy
end
end
end
|