class Inflect::AbstractService
Acts as an specification or standard required for a Service Class to be consumed by the application. A Service Class is just a wrapper for any possible service you'd like to give support to, just by having four prerequisites.
Attributes
In case there are modules that provide similar contents the one with most priority
is picked.
A words
Array constant with the key words
of the Service. @example Array for New York Times service
words = %W[ NEWS TODAY NEW\ YORK\ TIMES]
Public Class Methods
# File lib/inflect/abstract_service.rb, line 25 def initialize @priority = 0 @words = [] end
Public Instance Methods
Implement Comparable in order to be sortable.
# File lib/inflect/abstract_service.rb, line 31 def <=>(other_service) priority <=> other_service.priority end
Virtual attributes for the service actions. Every action must have its matching method.
# File lib/inflect/abstract_service.rb, line 67 def actions words[1..-1] end
Returns a Hash with retrieved data by routing from the request to the method specified by the request's action attribute. @param Inflect::Request
@return Inflect::Response
# File lib/inflect/abstract_service.rb, line 48 def handle(request) if action_defined(request.action) && !action_implemented(request.action) no_method_error request else if request.arguments.empty? send request.action else send request.action, request.arguments end end end
Virtual attribute for the services keyword.
# File lib/inflect/abstract_service.rb, line 61 def keyword words.first end
Receives a Request
and returns true if the service can handle the request given. @param Inflect::Request
@return [Boolean]
# File lib/inflect/abstract_service.rb, line 39 def valid?(request) (self.keyword.eql? request.keyword) && action_defined(request.action) end
Private Instance Methods
Check if user defined the given action besides from the default_options action that is always present.
# File lib/inflect/abstract_service.rb, line 75 def action_defined(action) (action.eql? :default) || (actions.include? action.to_s.upcase) end
Check if user really implemented the method corresponding to the action.
# File lib/inflect/abstract_service.rb, line 81 def action_implemented(action) respond_to? action end
Method fired when users don't implement the method corresponding to the actions they define in the words
array.
# File lib/inflect/abstract_service.rb, line 87 def no_method_error(request) if request.action.eql? :default message = "No default action declared for #{self.class}, the default method must be implemented if you want #{self.class} to respond to #{self.words}." else message = "#{self.class} declared the action #{request.action} in #{self.words} but the method is missing, for more information see Inflect::AbstractService class." end raise NoMethodError.new message end