Class: Vestroy::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/vestroy/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Model

Returns a new instance of Model.



7
8
9
10
# File 'lib/vestroy/model.rb', line 7

def initialize(object)
  @object = object
  @collected_errors = []
end

Instance Attribute Details

#collected_errorsObject (readonly)

Returns the value of attribute collected_errors.



5
6
7
# File 'lib/vestroy/model.rb', line 5

def collected_errors
  @collected_errors
end

Class Method Details

.destroys(name) ⇒ Object



12
13
14
15
16
# File 'lib/vestroy/model.rb', line 12

def self.destroys(name)
  define_method(name) do
    @object
  end
end

Instance Method Details

#add_error(message) ⇒ Object



36
37
38
# File 'lib/vestroy/model.rb', line 36

def add_error(message)
  collected_errors << message
end

#collect_errorsObject



31
32
33
34
# File 'lib/vestroy/model.rb', line 31

def collect_errors
  # default error message
  add_error(I18n.t('vestroy.could_not_be_destroyed')) if collected_errors.empty?
end

#destroyObject



18
19
20
21
22
23
24
25
# File 'lib/vestroy/model.rb', line 18

def destroy
  if destroyed_object = (destroyable? && @object.destroy)
    destroyed_object
  else
    collect_errors
    raise Vestroy::Error.new("#{collected_errors.to_sentence}.")
  end
end

#destroyable?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/vestroy/model.rb', line 27

def destroyable?
  true
end