Class: DhEasy::Router::Parser

Inherits:
Object
  • Object
show all
Includes:
DhEasy::Router::Plugin::Router
Defined in:
lib/dh_easy/router/parser.rb

Overview

Parser router designed to look over page_type for route to the right parser class.

Instance Attribute Summary

Attributes included from DhEasy::Router::Plugin::Router

#local_config

Instance Method Summary collapse

Methods included from DhEasy::Router::Plugin::Router

#class_defined?, #config, #get_class, #initialize, #initialize_hook_router_plugin_router

Instance Method Details

#route(opts = {}) ⇒ Object

Note:

Requires the route class to implement parse instance method.

Execute the parser class with options as described by router configuration filtering by page_type and calling class's instance parse method.

Parameters:

  • opts (Hash) (defaults to: {})

    ({}) Parser initializer options (see DhEasy::Core::Plugin::Parser).

Raises:

  • (ArgumentError)

    opts[:context] is nil.

  • (ArgumentError)

    page_type doesn't exists within routes.

  • (NameError)

    A class with name equal to route's class attribute doesn't exists.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dh_easy/router/parser.rb', line 21

def route opts = {}
  context = opts[:context]
  if context.nil?
    raise ArgumentError.new('Must send a context to the parser.')
  end

  page_type = context.page['page_type'].strip.downcase
  class_name = nil
  config['parser']['routes'].each do |item|
    # Look for page type
    next if item['page_type'].strip.downcase != page_type

    # Validate class name
    executor_class = get_class item['class']
    if executor_class.nil?
      raise NameError.new("Class \"#{item['class']}\" doesn't exists, check your dh_easy config file.")
    end

    executor_class.new(opts).parse
    return
  end

  # Page type is not routed, raise an error.
  raise ArgumentError.new("Page type \"#{page_type}\" is not routed, check your dh_easy config file.")
end