Class: ApplicationService

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/application_service.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplicationService

Returns a new instance of ApplicationService.



9
10
# File 'lib/application_service.rb', line 9

def initialize
end

Class Method Details

.after(callback, *args) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/application_service.rb', line 21

def self.after(callback, *args)
  options = extract_callback_options(args, "!halted && value")

  args.each do |arg|
    set_callback callback, :after, arg, options
  end
end

.before(callback, *args) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/application_service.rb', line 13

def self.before(callback, *args)
  options = extract_callback_options(args, '!halted')

  args.each do |arg|
    set_callback callback, :before, arg, options
  end
end

Instance Method Details

#current_objectObject



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

def current_object
  @obj
end

#current_object=(obj) ⇒ Object



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

def current_object=(obj)
  @obj = obj
end

#destroy(obj) ⇒ Object



62
63
64
65
66
67
# File 'lib/application_service.rb', line 62

def destroy(obj)
  @obj = obj
  run_callbacks :destroy do
    @obj.destroy
  end
end

#save(obj) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/application_service.rb', line 37

def save(obj)
  @obj = obj
  run_callbacks :save do
    if @obj.new_record?
      result = create
    else
      result = update
    end
    result
  end
end

#update_attributes(obj, params) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/application_service.rb', line 49

def update_attributes(obj, params)
  @obj = obj
  @obj.assign_attributes(params)

  yield if block_given?

  run_callbacks :save do
    run_callbacks :update do
       @obj.save
    end
  end
end