Class: Hemp::Routing::Base

Inherits:
Object show all
Includes:
Aliases, RouteExtensions
Defined in:
lib/hemp/routing/base.rb

Constant Summary collapse

SUPPORTED_VERB_LIST =
%w(get delete patch post put).freeze

Constants included from Aliases

Aliases::RouteAlias, Aliases::RouteError, Aliases::RouteExtensions, Aliases::RouteSplitter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RouteExtensions

#resources, #subject_helper

Constructor Details

#initializeBase

Returns a new instance of Base.



13
14
15
16
# File 'lib/hemp/routing/base.rb', line 13

def initialize
  @routes = {}
  verb_helper SUPPORTED_VERB_LIST
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



9
10
11
# File 'lib/hemp/routing/base.rb', line 9

def routes
  @routes
end

Instance Method Details

#error_constructor(line, error_message) ⇒ Object



78
79
80
# File 'lib/hemp/routing/base.rb', line 78

def error_constructor(line, error_message)
  raise RouteError.new line, error_message
end

#handle_errors(route_path, controller_action) ⇒ Object



39
40
41
42
# File 'lib/hemp/routing/base.rb', line 39

def handle_errors(route_path, controller_action)
  verify_route_path route_path
  verify_controller_action controller_action
end

#hash_error(controller_hash) ⇒ Object



67
68
69
70
71
72
# File 'lib/hemp/routing/base.rb', line 67

def hash_error(controller_hash)
  error_constructor(
    controller_hash,
    "Controller hash does not contain 'to' key"
  )
end

#hash_valid?(controller_hash) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/hemp/routing/base.rb', line 74

def hash_valid?(controller_hash)
  controller_hash.class == Hash && controller_hash.include?(:to)
end

#parse_route_args(split_route_path) ⇒ Object



56
57
58
59
60
# File 'lib/hemp/routing/base.rb', line 56

def parse_route_args(split_route_path)
  route_split_helper = RouteSplitter.new(split_route_path)

  route_split_helper.parse_regex_and_vars_from_route
end

#prepare(&block) ⇒ Object



18
19
20
# File 'lib/hemp/routing/base.rb', line 18

def prepare(&block)
  instance_eval(&block)
end

#process_route_line(verb, route_path, controller_action) ⇒ Object



32
33
34
35
36
37
# File 'lib/hemp/routing/base.rb', line 32

def process_route_line(verb, route_path, controller_action)
  handle_errors route_path, controller_action
  route_args = parse_route_args(route_path.split("/"))
  route_object = RouteAlias.new(controller_action, *route_args)
  save_to_routes verb, route_object
end

#save_to_routes(verb, route_object) ⇒ Object



62
63
64
65
# File 'lib/hemp/routing/base.rb', line 62

def save_to_routes(verb, route_object)
  routes[verb.to_sym] ||= []
  routes[verb.to_sym] << route_object
end

#verb_helper(verbs) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/hemp/routing/base.rb', line 22

def verb_helper(verbs)
  verbs.each do |verb|
    self.class.
      send(:define_method, verb) do |route_path, controller_hash|
      hash_error(controller_hash) unless hash_valid? controller_hash
      process_route_line verb, route_path, controller_hash[:to]
    end
  end
end

#verify_controller_action(controller_action) ⇒ Object



44
45
46
47
48
# File 'lib/hemp/routing/base.rb', line 44

def verify_controller_action(controller_action)
  error_constructor(
    controller_action, "Controller action does not meet required pattern"
  ) if (controller_action =~ /^[a-zA-Z0-9_]+#[a-zA-Z0-9_]+$/).nil?
end

#verify_route_path(route_path) ⇒ Object



50
51
52
53
54
# File 'lib/hemp/routing/base.rb', line 50

def verify_route_path(route_path)
  error_constructor(
    route_path, "Route should not be a blank string"
  ) if route_path.empty?
end