Class: Dibber::Seeder

Inherits:
Object
  • Object
show all
Defined in:
lib/dibber/seeder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_methodObject

Returns the value of attribute attribute_method



7
8
9
# File 'lib/dibber/seeder.rb', line 7

def attribute_method
  @attribute_method
end

#fileObject

Returns the value of attribute file



7
8
9
# File 'lib/dibber/seeder.rb', line 7

def file
  @file
end

#klassObject

Returns the value of attribute klass



7
8
9
# File 'lib/dibber/seeder.rb', line 7

def klass
  @klass
end

#name_methodObject

Returns the value of attribute name_method



7
8
9
# File 'lib/dibber/seeder.rb', line 7

def name_method
  @name_method
end

#overwriteObject

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_logObject



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_reportObject



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

.errorsObject



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_logObject



23
24
25
# File 'lib/dibber/seeder.rb', line 23

def process_log
  @process_log ||= start_process_log
end

.reportObject



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_pathObject



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

#buildObject



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

#objectsObject



90
91
92
# File 'lib/dibber/seeder.rb', line 90

def objects
  @objects ||= self.class.objects_from(file)
end

#start_logObject



86
87
88
# File 'lib/dibber/seeder.rb', line 86

def start_log
  self.class.monitor(klass)
end