Class: ApplicationService
- Inherits:
-
Object
- Object
- ApplicationService
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/application_service.rb
Class Method Summary collapse
- .after(callback, *args) ⇒ Object
- .before(callback, *args) ⇒ Object
- .extract_callback_options(args, default_if_option) ⇒ Object
Instance Method Summary collapse
- #current_object ⇒ Object
- #current_object=(obj) ⇒ Object
- #destroy(obj) ⇒ Object
-
#initialize ⇒ ApplicationService
constructor
A new instance of ApplicationService.
- #save(obj) ⇒ Object
- #save!(obj) ⇒ Object
- #update_attributes(obj, params) ⇒ Object
Constructor Details
#initialize ⇒ ApplicationService
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) = (args, "!halted && value") args.each do |arg| set_callback callback, :after, arg, 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) = (args, '!halted') args.each do |arg| set_callback callback, :before, arg, end end |
.extract_callback_options(args, default_if_option) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/application_service.rb', line 107 def self.(args, default_if_option) = args.last.is_a?(Hash) ? args.pop : {} if && .has_key?(:if) [:if] = [[:if], default_if_option] else [:if] = default_if_option end end |
Instance Method Details
#current_object ⇒ Object
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
74 75 76 77 78 79 |
# File 'lib/application_service.rb', line 74 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 |
#save!(obj) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/application_service.rb', line 49 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
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/application_service.rb', line 61 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 |