Class: ServiCraft::Create

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

Overview

CreateRecordService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, params) ⇒ Create

Returns a new instance of Create.



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

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

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/servi_craft/create.rb', line 8

def model
  @model
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/servi_craft/create.rb', line 8

def params
  @params
end

#recordObject (readonly)

Returns the value of attribute record.



8
9
10
# File 'lib/servi_craft/create.rb', line 8

def record
  @record
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/servi_craft/create.rb', line 8

def response
  @response
end

Instance Method Details

#call(before_assign_attributes: proc {}, before_create: proc {}, after_successful_create: proc {}, after_failed_create: proc {}, finally: proc {}) ⇒ Object



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

def call(before_assign_attributes: proc {},
         before_create: proc {},
         after_successful_create: proc {},
         after_failed_create: proc {},
         finally: proc {})
  begin
    before_assign_attributes.call
    @record = @model.new(@params)
    before_create.call
    process_record_create(@record.save, after_successful_create: after_successful_create,
                                        after_failed_create: after_failed_create)
  rescue StandardError => e
    puts "#{e.class}: #{e.message}"
    puts e.backtrace.join("\n")
    fail_create(after_failed_create: after_failed_create)
  end
  finally.call
  @response
end