Class: ServiCraft::Update

Inherits:
Object
  • Object
show all
Defined in:
lib/servi_craft/update.rb

Overview

UpdateRecordService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, params) ⇒ Update

Returns a new instance of Update.



9
10
11
12
13
14
# File 'lib/servi_craft/update.rb', line 9

def initialize(record, params)
  @record = record
  @model = @record.class
  @params = params
  @response = ::EchoCraft::ServiceObjects::Update.new(@record, @params)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/servi_craft/update.rb', line 7

def model
  @model
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/servi_craft/update.rb', line 7

def params
  @params
end

#recordObject (readonly)

Returns the value of attribute record.



7
8
9
# File 'lib/servi_craft/update.rb', line 7

def record
  @record
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/servi_craft/update.rb', line 7

def response
  @response
end

Instance Method Details

#call(before_assign_attributes: proc {}, before_update: proc {}, after_successful_update: proc {}, after_failed_update: proc {}, finally: proc {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/servi_craft/update.rb', line 16

def call(before_assign_attributes: proc {},
         before_update: proc {},
         after_successful_update: proc {},
         after_failed_update: proc {},
         finally: proc {})

  begin
    before_assign_attributes.call

    @record.assign_attributes(@params)

    before_update.call

    process_record_update(@record.save, after_successful_update: after_successful_update,
                                        after_failed_update: after_failed_update)
  rescue StandardError => e
    puts "#{e.class}: #{e.message}"
    puts e.backtrace.join("\n")
    fail_update(after_failed_update: after_failed_update)
  end
  finally.call
  @response
end