Class: Shamu::Services::Request
- Inherits:
-
Object
- Object
- Shamu::Services::Request
- Includes:
- Attributes, Attributes::Assignment, Attributes::Validation
- Defined in:
- lib/shamu/services/request.rb
Overview
Define the attributes and validations required to request a change by a Service. You can use the Request in place of an ActiveRecord model in rails forms_helpers.
module Document
module Request
class Change < Shamu::Services::Request
attribute :title, presence: true
attribute :author_id, presence: true
end
class Create < Change
end
class Update < Change
attribute :id, presence: true
end
end
end
Direct Known Subclasses
Constant Summary collapse
- REQUEST_ACTION_PATTERN =
/(Create|Update|New|Change|Delete)?(Request)?$/
Class Method Summary collapse
-
.coerce(params) ⇒ Request
Coerces a hash or params object to a proper Request object.
-
.coerce!(params) ⇒ Request
Coerces the given params object and raises an ArgumentError if any of the parameters are invalid.
-
.model_name ⇒ ActiveModel::Name
Used by url_helpers or form_helpers etc.
Instance Method Summary collapse
-
#apply_to(model) ⇒ model
Applies the attributes of the request to the given model.
-
#complete(success) ⇒ Object
Mark the request as complete and run any #on_success or ##on_fail callbacks.
-
#error(*args) ⇒ self
Adds an error to #errors and returns self.
-
#on_complete(&block) ⇒ Object
Execute block when the service is done processing the request.
-
#on_fail(&block) ⇒ Object
Execute block if the request is not satisfied by the service.
-
#on_success(&block) ⇒ Object
Execute block if the request is satisfied by the service successfully.
-
#persisted? ⇒ Boolean
Entities are always immutable - so they are considered persisted.
Methods included from Attributes::Validation
attribute, #valid?, #validated?
Methods included from Attributes::Assignment
#[]=, #assigned?, #assigned_attributes, attribute, #unassigned_attributes
Methods included from Attributes
#[], #as_json, #assign_attributes, association, associations, attribute, #attribute?, attributes, #initialize, #pretty_print, #set?, #slice, #to_attributes, #to_json
Class Method Details
.coerce(params) ⇒ Request
Coerces a hash or params object to a proper Shamu::Services::Request object.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/shamu/services/request.rb', line 104 def coerce( params ) if params.is_a?( self ) params elsif params.respond_to?( :to_h ) || params.respond_to?( :to_attributes ) new( params ) elsif params.nil? new else raise ArgumentError end end |
.coerce!(params) ⇒ Request
Coerces the given params object and raises an ArgumentError if any of the parameters are invalid.
120 121 122 123 124 |
# File 'lib/shamu/services/request.rb', line 120 def coerce!( params ) coerced = coerce( params ) raise ArgumentError unless coerced.valid? coerced end |
.model_name ⇒ ActiveModel::Name
Returns used by url_helpers or form_helpers etc. when generating model specific names for this request.
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/shamu/services/request.rb', line 130 def model_name @model_name ||= begin base_name = name || "" parts = reduce_model_name_parts( base_name.split( "::" ) ) parts = ["Request"] if parts.empty? base_name = parts.join "::" ::ActiveModel::Name.new( self, nil, base_name ) end end |
Instance Method Details
#apply_to(model) ⇒ model
Applies the attributes of the request to the given model. Only handles scalar attributes. For more complex associations, override in a custom Shamu::Services::Request class.
36 37 38 39 40 41 42 43 |
# File 'lib/shamu/services/request.rb', line 36 def apply_to( model ) self.class.attributes.each do |name, _| method = :"#{ name }=" model.send method, send( name ) if model.respond_to?( method ) && set?( name ) end model end |
#complete(success) ⇒ Object
Mark the request as complete and run any #on_success or ##on_fail callbacks.
successfully.
78 79 80 81 82 83 84 85 86 |
# File 'lib/shamu/services/request.rb', line 78 def complete( success ) if success @on_success_blocks && @on_success_blocks.each( &:call ) else @on_fail_blocks && @on_fail_blocks.each( &:call ) end @on_complete_blocks && @on_complete_blocks.each( &:call ) end |
#error(*args) ⇒ self
Adds an error to #errors and returns self. Used when performing an early return in a service method
95 96 97 98 |
# File 'lib/shamu/services/request.rb', line 95 def error( *args ) errors.add( *args ) self end |
#on_complete(&block) ⇒ Object
Execute block when the service is done processing the request.
68 69 70 71 |
# File 'lib/shamu/services/request.rb', line 68 def on_complete( &block ) @on_complete_blocks ||= [] @on_complete_blocks << block end |
#on_fail(&block) ⇒ Object
Execute block if the request is not satisfied by the service.
62 63 64 65 |
# File 'lib/shamu/services/request.rb', line 62 def on_fail( &block ) @on_fail_blocks ||= [] @on_fail_blocks << block end |
#on_success(&block) ⇒ Object
Execute block if the request is satisfied by the service successfully.
56 57 58 59 |
# File 'lib/shamu/services/request.rb', line 56 def on_success( &block ) @on_success_blocks ||= [] @on_success_blocks << block end |
#persisted? ⇒ Boolean
Entities are always immutable - so they are considered persisted. Use a Services::ChangeRequest to back a form instead.
47 48 49 50 51 52 53 |
# File 'lib/shamu/services/request.rb', line 47 def persisted? if respond_to?( :id ) !!id else fail NotImplementedError, "override persisted? in #{ self.class.name }" end end |