Class: Capsium::Package::Routes
- Inherits:
-
Object
- Object
- Capsium::Package::Routes
- Extended by:
- Forwardable
- Defined in:
- sig/capsium/package/routes.rbs,
lib/capsium/package/routes.rb
Overview
Loads, generates and writes routes.json (ARCHITECTURE.md section 4).
Constant Summary collapse
- INDEX_ROUTE =
"/"- INDEX_RESOURCE =
"content/index.html"- DATASET_ROUTE_PREFIX =
"/api/v1/data/"
Instance Attribute Summary collapse
-
#config ⇒ RoutesConfig
readonly
Returns the value of attribute config.
-
#manifest ⇒ Manifest
readonly
Returns the value of attribute manifest.
-
#path ⇒ String
readonly
Returns the value of attribute path.
-
#storage ⇒ Storage
readonly
Returns the value of attribute storage.
Instance Method Summary collapse
- #add_route(route, target) ⇒ Route
-
#generate_routes ⇒ RoutesConfig
Auto-generation (ARCHITECTURE.md section 4): every manifest resource gets a route at its path relative to content/; HTML files get two routes; the index HTML additionally gets "/"; every dataset gets /api/v1/data/
. -
#initialize(path, manifest, storage) ⇒ Routes
constructor
A new instance of Routes.
- #remove_route(route) ⇒ Array[Route]
- #resolve(url_path) ⇒ Route?
- #save_to_file(output_path = @path) ⇒ void
- #to_hash ⇒ Hash[String, untyped]
- #to_json(*_args) ⇒ String
- #update_route(route, updated_route, updated_target) ⇒ Route
Constructor Details
#initialize(path, manifest, storage) ⇒ Routes
Returns a new instance of Routes.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/capsium/package/routes.rb', line 18 def initialize(path, manifest, storage) @path = path @manifest = manifest @storage = storage @config = if File.exist?(path) RoutesConfig.from_json(File.read(path)) else generate_routes end end |
Instance Attribute Details
#config ⇒ RoutesConfig (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/capsium/package/routes.rb', line 10 def config @config end |
#manifest ⇒ Manifest (readonly)
Returns the value of attribute manifest.
10 11 12 |
# File 'lib/capsium/package/routes.rb', line 10 def manifest @manifest end |
#path ⇒ String (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/capsium/package/routes.rb', line 10 def path @path end |
#storage ⇒ Storage (readonly)
Returns the value of attribute storage.
10 11 12 |
# File 'lib/capsium/package/routes.rb', line 10 def storage @storage end |
Instance Method Details
#add_route(route, target) ⇒ Route
33 34 35 |
# File 'lib/capsium/package/routes.rb', line 33 def add_route(route, target) @config.add(route, target) end |
#generate_routes ⇒ RoutesConfig
Auto-generation (ARCHITECTURE.md section 4): every manifest
resource gets a route at its path relative to content/; HTML
files get two routes; the index HTML additionally gets "/";
every dataset gets /api/v1/data/
58 59 60 61 |
# File 'lib/capsium/package/routes.rb', line 58 def generate_routes routes = resource_routes + dataset_routes RoutesConfig.new(index: index_resource, routes: routes.sort_by(&:path)) end |
#remove_route(route) ⇒ Array[Route]
41 42 43 |
# File 'lib/capsium/package/routes.rb', line 41 def remove_route(route) @config.remove(route) end |
#resolve(url_path) ⇒ Route?
29 30 31 |
# File 'lib/capsium/package/routes.rb', line 29 def resolve(url_path) @config.resolve(url_path) end |
#save_to_file(output_path = @path) ⇒ void
This method returns an undefined value.
49 50 51 |
# File 'lib/capsium/package/routes.rb', line 49 def save_to_file(output_path = @path) File.write(output_path, to_json) end |
#to_hash ⇒ Hash[String, untyped]
26 |
# File 'sig/capsium/package/routes.rbs', line 26
def to_hash: () -> Hash[String, untyped]
|
#to_json(*_args) ⇒ String
45 46 47 |
# File 'lib/capsium/package/routes.rb', line 45 def to_json(*_args) @config.to_json end |
#update_route(route, updated_route, updated_target) ⇒ Route
37 38 39 |
# File 'lib/capsium/package/routes.rb', line 37 def update_route(route, updated_route, updated_target) @config.update(route, updated_route, updated_target) end |