Class: Dibber::Seeder
- Inherits:
-
Object
- Object
- Dibber::Seeder
- Defined in:
- lib/dibber/seeder.rb
Instance Attribute Summary collapse
-
#attribute_method ⇒ Object
Returns the value of attribute attribute_method.
-
#file ⇒ Object
Returns the value of attribute file.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#name_method ⇒ Object
Returns the value of attribute name_method.
-
#overwrite ⇒ Object
Returns the value of attribute overwrite.
Class Method Summary collapse
- .clear_process_log ⇒ Object
- .erb_processed(content) ⇒ Object
- .error_report ⇒ Object
- .errors ⇒ Object
- .file_content(file) ⇒ Object
- .monitor(klass) ⇒ Object
- .objects_from(file) ⇒ Object
- .process_log ⇒ Object
- .report ⇒ Object
- .seed(klass, args = {}) ⇒ Object
- .seeds_path ⇒ Object
- .seeds_path=(path) ⇒ Object
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(klass, file, args = {}) ⇒ Seeder
constructor
A new instance of Seeder.
- #objects ⇒ Object
- #start_log ⇒ Object
Constructor Details
#initialize(klass, file, args = {}) ⇒ Seeder
Returns a new instance of Seeder.
63 64 65 66 67 68 69 70 |
# 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 |
Instance Attribute Details
#attribute_method ⇒ Object
Returns the value of attribute attribute_method.
7 8 9 |
# File 'lib/dibber/seeder.rb', line 7 def attribute_method @attribute_method end |
#file ⇒ Object
Returns the value of attribute file.
7 8 9 |
# File 'lib/dibber/seeder.rb', line 7 def file @file end |
#klass ⇒ Object
Returns the value of attribute klass.
7 8 9 |
# File 'lib/dibber/seeder.rb', line 7 def klass @klass end |
#name_method ⇒ Object
Returns the value of attribute name_method.
7 8 9 |
# File 'lib/dibber/seeder.rb', line 7 def name_method @name_method end |
#overwrite ⇒ Object
Returns the value of attribute overwrite.
7 8 9 |
# File 'lib/dibber/seeder.rb', line 7 def overwrite @overwrite end |
Class Method Details
.clear_process_log ⇒ Object
27 28 29 |
# File 'lib/dibber/seeder.rb', line 27 def clear_process_log @process_log = nil end |
.erb_processed(content) ⇒ Object
46 47 48 |
# File 'lib/dibber/seeder.rb', line 46 def erb_processed(content) ERB.new(content).result end |
.error_report ⇒ Object
98 99 100 101 |
# 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 ⇒ Object
94 95 96 |
# File 'lib/dibber/seeder.rb', line 94 def self.errors @errors ||= [] end |
.file_content(file) ⇒ Object
50 51 52 |
# File 'lib/dibber/seeder.rb', line 50 def file_content(file) File.read File.join(seeds_path, file) end |
.monitor(klass) ⇒ Object
35 36 37 38 39 40 |
# 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 |
.objects_from(file) ⇒ Object
42 43 44 |
# File 'lib/dibber/seeder.rb', line 42 def objects_from(file) YAML.load erb_processed(file_content(file)) end |
.process_log ⇒ Object
23 24 25 |
# File 'lib/dibber/seeder.rb', line 23 def process_log @process_log ||= start_process_log end |
.report ⇒ Object
31 32 33 |
# File 'lib/dibber/seeder.rb', line 31 def report process_log.report + error_report end |
.seed(klass, args = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# 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 ⇒ Object
54 55 56 |
# 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) ⇒ Object
58 59 60 |
# File 'lib/dibber/seeder.rb', line 58 def seeds_path=(path) @seeds_path = add_trailing_slash_to(path) end |
Instance Method Details
#build ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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 ⇒ Object
90 91 92 |
# File 'lib/dibber/seeder.rb', line 90 def objects @objects ||= self.class.objects_from(file) end |
#start_log ⇒ Object
86 87 88 |
# File 'lib/dibber/seeder.rb', line 86 def start_log self.class.monitor(klass) end |