class Dibber::Seeder
Attributes
attribute_method[RW]
file[RW]
klass[RW]
name_method[RW]
overwrite[RW]
Public Class Methods
clear_process_log()
click to toggle source
# File lib/dibber/seeder.rb, line 27 def clear_process_log @process_log = nil end
erb_processed(content)
click to toggle source
# File lib/dibber/seeder.rb, line 46 def erb_processed(content) ERB.new(content).result end
error_report()
click to toggle source
# File lib/dibber/seeder.rb, line 98 def self.error_report return [] if errors.empty? ["#{errors.length} errors detected:", errors.collect(&:inspect)].flatten end
errors()
click to toggle source
# File lib/dibber/seeder.rb, line 94 def self.errors @errors ||= [] end
file_content(file)
click to toggle source
# File lib/dibber/seeder.rb, line 50 def file_content(file) File.read File.join(seeds_path, file) end
monitor(klass)
click to toggle source
# File lib/dibber/seeder.rb, line 35 def monitor(klass) log_name = klass.to_s.tableize.to_sym unless process_log.exists?(log_name) process_log.start(log_name, "#{klass}.count") end end
new(klass, file, args = {})
click to toggle source
# File lib/dibber/seeder.rb, line 63 def initialize(klass, file, args = {}) @klass = klass @file = file args = {:attributes_method => args} unless args.kind_of?(Hash) @attribute_method = args[:attributes_method] || 'attributes' @name_method = args[:name_method] || 'name' @overwrite = args[:overwrite] end
objects_from(file)
click to toggle source
# File lib/dibber/seeder.rb, line 42 def objects_from(file) YAML.load erb_processed(file_content(file)) end
process_log()
click to toggle source
# File lib/dibber/seeder.rb, line 23 def process_log @process_log ||= start_process_log end
report()
click to toggle source
# File lib/dibber/seeder.rb, line 31 def report process_log.report + error_report end
seed(klass, args = {})
click to toggle source
# File lib/dibber/seeder.rb, line 11 def seed(klass, args = {}) if klass.kind_of?(String) || klass.kind_of?(Symbol) name = klass.to_s.underscore class_name = klass.to_s.strip.classify klass = Kernel.const_get(class_name) else name = klass.to_s.underscore end new_file = "#{name.pluralize}.yml" new(klass, new_file, args).build end
seeds_path()
click to toggle source
# File lib/dibber/seeder.rb, line 54 def seeds_path @seeds_path || try_to_guess_seeds_path || raise_no_seeds_path_error end
seeds_path=(path)
click to toggle source
# File lib/dibber/seeder.rb, line 58 def seeds_path=(path) @seeds_path = add_trailing_slash_to(path) end
Private Class Methods
add_trailing_slash_to(path = nil)
click to toggle source
# File lib/dibber/seeder.rb, line 136 def self.add_trailing_slash_to(path = nil) path = path + '/' if path and path !~ /\/$/ path end
raise_no_seeds_path_error()
click to toggle source
# File lib/dibber/seeder.rb, line 110 def self.raise_no_seeds_path_error raise "You must set the path to your seed files via Seeder.seeds_path = 'path/to/seed/files'" end
start_process_log()
click to toggle source
# File lib/dibber/seeder.rb, line 104 def self.start_process_log process_log = ProcessLog.new process_log.start :time, 'Time.now.strftime("%Y-%b-%d %H:%M:%S.%3N %z")' return process_log end
try_to_guess_seeds_path()
click to toggle source
# File lib/dibber/seeder.rb, line 130 def self.try_to_guess_seeds_path path = File.expand_path('db/seeds', Rails.root) if defined? Rails add_trailing_slash_to(path) end
Public Instance Methods
build()
click to toggle source
# File lib/dibber/seeder.rb, line 72 def build check_objects_exist start_log objects.each do |name, attributes| object = find_or_initialize_by(name) if overwrite or object.new_record? object.send("#{attribute_method}=", attributes) unless object.save self.class.errors << object.errors end end end end
objects()
click to toggle source
# File lib/dibber/seeder.rb, line 90 def objects @objects ||= self.class.objects_from(file) end
start_log()
click to toggle source
# File lib/dibber/seeder.rb, line 86 def start_log self.class.monitor(klass) end
Private Instance Methods
check_objects_exist()
click to toggle source
# File lib/dibber/seeder.rb, line 114 def check_objects_exist raise "No objects returned from file: #{self.class.seeds_path}#{file}" unless objects end
find_or_initialize_by(name)
click to toggle source
# File lib/dibber/seeder.rb, line 118 def find_or_initialize_by(name) if klass.exists?(name_method_sym => name) klass.where(name_method_sym => name).first else klass.new(name_method_sym => name) end end
name_method_sym()
click to toggle source
# File lib/dibber/seeder.rb, line 126 def name_method_sym name_method.to_sym end