Class: Porous::Routes
- Inherits:
-
Object
- Object
- Porous::Routes
- Defined in:
- lib/porous/routes.rb
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #add_redirect(path, redirect_to) ⇒ Object
- #add_route(name, path, component, component_props, on_enter) ⇒ Object
- #add_subroutes(path, &block) ⇒ Object
-
#build_params_and_regex(path) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#combine(other) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
-
#initialize(parent = nil) ⇒ Routes
constructor
A new instance of Routes.
-
#route(*params, &block) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#validate_component(component) ⇒ Object
rubocop:enable Metrics/AbcSize.
Constructor Details
#initialize(parent = nil) ⇒ Routes
Returns a new instance of Routes.
7 8 9 10 |
# File 'lib/porous/routes.rb', line 7 def initialize(parent = nil) @parent = parent @routes = [] end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
5 6 7 |
# File 'lib/porous/routes.rb', line 5 def routes @routes end |
Instance Method Details
#add_redirect(path, redirect_to) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/porous/routes.rb', line 36 def add_redirect(path, redirect_to) @routes << { path: path, redirect_to: redirect_to }.merge(build_params_and_regex(path)) end |
#add_route(name, path, component, component_props, on_enter) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/porous/routes.rb', line 43 def add_route(name, path, component, component_props, on_enter) validate_component(component) @routes << { path: path, component: component, component_props: component_props, on_enter: on_enter, name: name || component.to_s.gsub(/(.)([A-Z])/, '\1_\2').downcase }.merge(build_params_and_regex(path)) end |
#add_subroutes(path, &block) ⇒ Object
54 55 56 57 58 |
# File 'lib/porous/routes.rb', line 54 def add_subroutes(path, &block) subroutes = Routes.new(path) subroutes.instance_exec(&block) @routes += subroutes.routes end |
#build_params_and_regex(path) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/porous/routes.rb', line 61 def build_params_and_regex(path) regex = ['^'] params = [] parts = path.split('/') regex << '\/' if parts.empty? parts.each do |part| next if part.empty? regex << '\/' case part[0] when ':' params << part[1..] regex << '([^\/]+)' when '*' params << part[1..] regex << '(.*)' break else regex << part end end regex << '$' { regex: Regexp.new(regex.join), params: params } end |
#combine(other) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
90 91 92 |
# File 'lib/porous/routes.rb', line 90 def combine(other) @routes += other.routes end |
#route(*params, &block) ⇒ Object
rubocop:disable Metrics/AbcSize
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/porous/routes.rb', line 13 def route(*params, &block) path = params.first.gsub(%r{^/}, '') path = @parent ? "#{@parent}/#{path}" : "/#{path}" add_subroutes(path, &block) if block_given? if params.last[:redirect_to] add_redirect(path, params.last[:redirect_to]) else add_route(params.last[:as], path, params.last[:to], params.last[:props], params.last[:on_enter]) end end |
#validate_component(component) ⇒ Object
rubocop:enable Metrics/AbcSize
27 28 29 30 31 32 33 34 |
# File 'lib/porous/routes.rb', line 27 def validate_component(component) raise Error, 'Component not exists' unless component return if component.include?(Porous::Component) raise Error, "Invalid #{component} class, should mixin Porous::Component" end |