Class: Simple::Httpd::Rack::DynamicMount
- Inherits:
-
Object
- Object
- Simple::Httpd::Rack::DynamicMount
- Defined in:
- lib/simple/httpd/rack/dynamic_mount.rb
Overview
The Simple::Httpd::Mountpoint.build returns a Rack compatible app, which serves HTTP requests according to a set of dynamic ruby scripts and some existing static files.
Constant Summary collapse
- H =
::Simple::Httpd::Helpers
- Rack =
::Simple::Httpd::Rack
Instance Attribute Summary collapse
-
#mount_point ⇒ Object
readonly
Returns the value of attribute mount_point.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
-
#build_root_controller(helper_paths) ⇒ Object
wraps all helpers into a Simple::Httpd::BaseController subclass.
-
#build_url_map ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#initialize(mount_point, path) ⇒ DynamicMount
constructor
A new instance of DynamicMount.
-
#load_controller(path) ⇒ Object
wraps the source file in path into a root_controller.
Constructor Details
#initialize(mount_point, path) ⇒ DynamicMount
Returns a new instance of DynamicMount.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 21 def initialize(mount_point, path) @mount_point = mount_point @path = path # determine source_paths and controller_paths source_paths = Dir.glob("#{path}/**/*.rb") helper_paths, @controller_paths = source_paths.partition { |str| /_helpers\.rb$/ =~ str } # build root_controller @root_controller = build_root_controller(helper_paths) end |
Instance Attribute Details
#mount_point ⇒ Object (readonly)
Returns the value of attribute mount_point.
19 20 21 |
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 19 def mount_point @mount_point end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
18 19 20 |
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 18 def path @path end |
Class Method Details
.build(mount_point, path) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 10 def self.build(mount_point, path) expect! path => String url_map = new(mount_point, path).build_url_map ::Rack::URLMap.new(url_map) end |
Instance Method Details
#build_root_controller(helper_paths) ⇒ Object
wraps all helpers into a Simple::Httpd::BaseController subclass
53 54 55 56 57 58 59 |
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 53 def build_root_controller(helper_paths) klass = H.subclass ::Simple::Httpd::BaseController, description: "root controller at #{path} w/#{helper_paths.count} helpers" H.instance_eval_paths klass, *helper_paths.sort klass end |
#build_url_map ⇒ Object
rubocop:disable Metrics/AbcSize
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 34 def build_url_map @controller_paths.sort.each_with_object({}) do |absolute_path, url_map| relative_path = absolute_path[(path.length)..-1] relative_mount_point = relative_path == "/root.rb" ? "/" : relative_path.gsub(/\.rb$/, "") controller_class = load_controller absolute_path ::Simple::Httpd.logger.info do absolute_mount_point = File.join(mount_point, relative_mount_point) routes_count = controller_class.routes.reject { |verb, _| verb == "HEAD" }.values.sum(&:count) "#{absolute_mount_point}: mounting #{routes_count} route(s) from #{H.shorten_path absolute_path}" end url_map.update relative_mount_point => controller_class end end |
#load_controller(path) ⇒ Object
wraps the source file in path into a root_controller
62 63 64 65 |
# File 'lib/simple/httpd/rack/dynamic_mount.rb', line 62 def load_controller(path) klass = H.subclass @root_controller, description: "controller at #{path}" H.instance_eval_paths klass, path end |