Class: Gearhead::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/gearhead/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router:) ⇒ Router

Returns a new instance of Router.



5
6
7
# File 'lib/gearhead/router.rb', line 5

def initialize(router:)
  @router = router
end

Instance Attribute Details

#routerObject (readonly)

Returns the value of attribute router.



3
4
5
# File 'lib/gearhead/router.rb', line 3

def router
  @router
end

Instance Method Details

#applyObject



9
10
11
12
13
14
# File 'lib/gearhead/router.rb', line 9

def apply
  if Gearhead.config.automount.enabled?
    define_automount_route
  end
  define_gear_routes
end

#build_action(action) ⇒ Object



50
51
52
# File 'lib/gearhead/router.rb', line 50

def build_action(action)
  build_route(action.verbs, action.name)
end

#build_route(verbs, *args) ⇒ Object



54
55
56
# File 'lib/gearhead/router.rb', line 54

def build_route(verbs, *args)
  Array.wrap(verbs).each { |verb| router.send(verb, *args) }
end

#define_actions(gear) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gearhead/router.rb', line 28

def define_actions(gear)
  @router.member do
    gear._gear_member_actions.values.each do |custom_action|
      custom_action.verbs.each do |verb|
        args = [custom_action.name, to: 'gearhead/gears#member_action', defaults: { member_action: custom_action.name }]
        router.send(verb, *args)
      end
    end
  end

  @router.collection do
    gear._gear_collection_actions.values.each do |custom_action|

      custom_action.verbs.each do |verb|

        args = [custom_action.name, to: 'gearhead/gears#collection_action', defaults: { collection_action: custom_action.name }]
        router.send(verb, *args)
      end
    end
  end
end

#define_automount_routeObject



16
17
18
# File 'lib/gearhead/router.rb', line 16

def define_automount_route
  @router.resources :gears, path: [::Gearhead.config.endpoint, ":resource_class"].join("/"), controller: "gearhead/gears", param: :resource_id
end

#define_gear_routesObject



20
21
22
23
24
25
26
# File 'lib/gearhead/router.rb', line 20

def define_gear_routes
  ::Gearhead.registry.all.each do |thing|
    @router.resources thing.resource.model_name.route_key, path: [::Gearhead.config.endpoint, thing.path].join("/"), controller: "gearhead/gears", param: :resource_id, defaults: { resource_class: thing.resource.model_name.route_key } do
      define_actions(thing)
    end
  end
end