Class: Boar::Services::Generic
- Inherits:
-
Object
- Object
- Boar::Services::Generic
- Includes:
- Utils::Basic
- Defined in:
- app/models/boar/services/generic.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
- #handle_authentication(path, options = nil) ⇒ Object
- #handle_error(exception) ⇒ Object
- #handler_for(scope, options = nil) ⇒ Object
-
#initialize(controller) ⇒ Generic
constructor
A new instance of Generic.
- #run(method) ⇒ Object
Methods included from Utils::Basic
#ensure_hash, #get_option, #interpolate
Constructor Details
#initialize(controller) ⇒ Generic
Returns a new instance of Generic.
17 18 19 20 21 22 23 24 25 |
# File 'app/models/boar/services/generic.rb', line 17 def initialize(controller) @controller = controller @configuration = Rails.application.config.boar @options = @controller. @params = @controller.params skip_cache_param = get_option(@options, :skip_cache_param, @configuration.skip_cache_param) @skip_cache = skip_cache_param ? @controller.request.params[skip_cache_param].to_boolean : false end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
11 12 13 |
# File 'app/models/boar/services/generic.rb', line 11 def configuration @configuration end |
#controller ⇒ Object
Returns the value of attribute controller.
10 11 12 |
# File 'app/models/boar/services/generic.rb', line 10 def controller @controller end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'app/models/boar/services/generic.rb', line 12 def @options end |
#params ⇒ Object
Returns the value of attribute params.
13 14 15 |
# File 'app/models/boar/services/generic.rb', line 13 def params @params end |
Instance Method Details
#handle_authentication(path, options = nil) ⇒ Object
58 59 60 |
# File 'app/models/boar/services/generic.rb', line 58 def handle_authentication(path, = nil) self.handler_for(:authentication, ).call(path) end |
#handle_error(exception) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/boar/services/generic.rb', line 36 def handle_error(exception) if exception.is_a?(Lazier::Exceptions::Dump) then raise exception else handler = self.handler_for(:views, @options) # Get HTTP code code = exception.is_a?(Boar::Exceptions::Error) ? exception.code : 500 # Basing on the code view = case code when 403 then handler.call(:error, code: 403) when 404 then handler.call(:error, code: 404) else raise exception if !Boar::Exceptions::Error.must_raise?(exception) handler.call(:error, code: 500, exception: exception) end @controller.render(file: view, status: code, formats: [:html]) if !@controller.performed? end end |
#handler_for(scope, options = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/boar/services/generic.rb', line 62 def handler_for(scope, = nil) = ensure_hash() scope = scope.to_sym # Get the handler specified by user = get_option(@options.merge(), :handlers, {}) # Use either the user handler or the system one handlers = @configuration.handlers.merge() # Instantiate the handler @boar_handlers ||= {} @boar_handlers[scope] ||= ::Lazier.find_class(handlers[scope]).new(self, ) end |
#run(method) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'app/models/boar/services/generic.rb', line 27 def run(method) begin self.send(method) rescue => e e = Boar::Exceptions::UnImplemented.new if !self.respond_to?(method) self.handle_error(e) end end |