Module: Staticky::Routing::Plugins::Prelude::InstanceMethods
- Defined in:
- lib/staticky/routing/plugins/prelude.rb
Instance Method Summary collapse
- #define(&block) ⇒ Object
- #filepaths ⇒ Object
- #match(path, to:, as: Resource) ⇒ Object
- #resolve(path) ⇒ Object
- #resources ⇒ Object
- #root(to:) ⇒ Object
Instance Method Details
#define(&block) ⇒ Object
18 19 20 21 22 |
# File 'lib/staticky/routing/plugins/prelude.rb', line 18 def define(&block) tap do instance_eval(&block) end end |
#filepaths ⇒ Object
64 |
# File 'lib/staticky/routing/plugins/prelude.rb', line 64 def filepaths = resources.map(&:filepath) |
#match(path, to:, as: Resource) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/staticky/routing/plugins/prelude.rb', line 24 def match(path, to:, as: Resource) path = strip_leading_slash(path) resource = case to when ->(x) { x.is_a?(Class) || x.is_a?(::Phlex::HTML) } component = ensure_instance(to) as.new(component:, url: path) else raise Router::Error, "Invalid route target: #{to.inspect}" end resources << resource index_resource(path, resource) end |
#resolve(path) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/staticky/routing/plugins/prelude.rb', line 43 def resolve(path) return path if path.is_a?(String) && path.start_with?("#") return lookup(path) if path.is_a?(Class) path = strip_leading_slash(path) uri = URI(path) # Return absolute paths as is return path if uri.absolute? if uri.path.size > 1 && uri.path.start_with?("/") uri.path = uri.path[1..] end lookup(uri.path) rescue URI::InvalidURIError raise Router::Error, "Invalid path: #{path}" rescue KeyError raise Router::Error, "No route matches #{path}" end |
#resources ⇒ Object
63 |
# File 'lib/staticky/routing/plugins/prelude.rb', line 63 def resources = @resources ||= [] |
#root(to:) ⇒ Object
39 40 41 |
# File 'lib/staticky/routing/plugins/prelude.rb', line 39 def root(to:) match("/", to:) end |