Class: Fill::Procedure

Inherits:
Object
  • Object
show all
Defined in:
lib/fill/procedure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models, options = {}, &block) ⇒ Procedure

Returns a new instance of Procedure.



7
8
9
10
11
# File 'lib/fill/procedure.rb', line 7

def initialize(models, options = {}, &block)
  @block   = block
  @options = { :delete => true }.merge(options)
  @models  = models
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



5
6
7
# File 'lib/fill/procedure.rb', line 5

def block
  @block
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/fill/procedure.rb', line 5

def options
  @options
end

Instance Method Details

#countObject



41
42
43
# File 'lib/fill/procedure.rb', line 41

def count
  models.map { |model| model.count }
end

#delete_allObject



33
34
35
# File 'lib/fill/procedure.rb', line 33

def delete_all
  models.map { |model| model.delete_all }
end

#human_modelsObject



21
22
23
# File 'lib/fill/procedure.rb', line 21

def human_models
  @human_models ||= (options[:name] || humanize_models)
end

#humanize_modelsObject



25
26
27
# File 'lib/fill/procedure.rb', line 25

def humanize_models
  models.map { |model| i18n_name(model) }.join(', ')
end

#i18n_name(model) ⇒ Object



29
30
31
# File 'lib/fill/procedure.rb', line 29

def i18n_name(model)
  model.respond_to?(:human_name) ? model.human_name : model.to_s
end

#modelsObject



37
38
39
# File 'lib/fill/procedure.rb', line 37

def models
  @models.map { |model| model.to_s.singularize.camelize.constantize }
end

#performObject



45
46
47
48
49
50
51
# File 'lib/fill/procedure.rb', line 45

def perform
  @before = options[:delete] ? delete_all : count
  @time   = Fill.time { block.call }
  @after  = count
  Presenter.present self
  true
end

#perform!Object



13
14
15
# File 'lib/fill/procedure.rb', line 13

def perform!
  @performed ||= perform
end

#to_hashObject



17
18
19
# File 'lib/fill/procedure.rb', line 17

def to_hash
  { "Models" => human_models, "Before" => @before.join(', '), "After" => @after.join(', '), "Time" => @time }
end