Class: Capsium::Package::RoutesConfig
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Capsium::Package::RoutesConfig
- Defined in:
- lib/capsium/package/routes_config.rb,
sig/capsium/package/routes_config.rbs
Overview
Canonical routes.json model: optional top-level "index" plus a "routes" array. The legacy object/target form is normalized on read; writers emit only the canonical form.
Instance Attribute Summary collapse
-
#index ⇒ String?
Returns the value of attribute index.
-
#routes ⇒ Array[Route]
Returns the value of attribute routes.
Class Method Summary collapse
Instance Method Summary collapse
- #add(path, target) ⇒ Route
-
#initialize ⇒ RoutesConfig
constructor
A new instance of RoutesConfig.
- #remove(path) ⇒ Array[Route]
- #resolve(path) ⇒ Route?
- #routes_from_json(model, value) ⇒ void
-
#routes_to_json(model, doc) ⇒ void
Writers emit the canonical form only: deterministic, sorted by path.
- #sort! ⇒ RoutesConfig
- #to_hash ⇒ Hash[String, untyped]
- #to_json ⇒ String
- #update(path, updated_path, updated_target) ⇒ Route
Constructor Details
#initialize ⇒ RoutesConfig
Returns a new instance of RoutesConfig.
85 |
# File 'sig/capsium/package/routes_config.rbs', line 85
def initialize: (?index: String? index, ?routes: Array[Route] routes) -> void
|
Instance Attribute Details
#index ⇒ String?
Returns the value of attribute index.
82 83 84 |
# File 'sig/capsium/package/routes_config.rbs', line 82 def index @index end |
#routes ⇒ Array[Route]
Returns the value of attribute routes.
83 84 85 |
# File 'sig/capsium/package/routes_config.rbs', line 83 def routes @routes end |
Class Method Details
.from_json(json) ⇒ instance
152 153 154 155 156 |
# File 'lib/capsium/package/routes_config.rb', line 152 def self.from_json(json) doc = JSON.parse(json) doc["routes"] = (doc["routes"] || []).map { |route| normalize_route(route) } super(JSON.generate(doc)) end |
Instance Method Details
#add(path, target) ⇒ Route
181 182 183 184 185 |
# File 'lib/capsium/package/routes_config.rb', line 181 def add(path, target) route = Route.new(path: path, resource: target) self.routes = routes + [route] route end |
#remove(path) ⇒ Array[Route]
194 195 196 |
# File 'lib/capsium/package/routes_config.rb', line 194 def remove(path) self.routes = routes.reject { |route| route.path == path } end |
#resolve(path) ⇒ Route?
177 178 179 |
# File 'lib/capsium/package/routes_config.rb', line 177 def resolve(path) routes.detect { |route| route.serving_path == path } end |
#routes_from_json(model, value) ⇒ void
This method returns an undefined value.
166 167 168 |
# File 'lib/capsium/package/routes_config.rb', line 166 def routes_from_json(model, value) model.routes = (value || []).map { |route| Route.from_json(JSON.generate(route)) } end |
#routes_to_json(model, doc) ⇒ void
This method returns an undefined value.
Writers emit the canonical form only: deterministic, sorted by path.
171 172 173 174 175 |
# File 'lib/capsium/package/routes_config.rb', line 171 def routes_to_json(model, doc) doc["routes"] = model.routes.sort_by(&:path).map do |route| JSON.parse(route.to_json) end end |
#sort! ⇒ RoutesConfig
198 199 200 201 |
# File 'lib/capsium/package/routes_config.rb', line 198 def sort! self.routes = routes.sort_by(&:path) self end |
#to_hash ⇒ Hash[String, untyped]
104 |
# File 'sig/capsium/package/routes_config.rbs', line 104
def to_hash: () -> Hash[String, untyped]
|
#to_json ⇒ String
103 |
# File 'sig/capsium/package/routes_config.rbs', line 103
def to_json: (*untyped args) -> String
|
#update(path, updated_path, updated_target) ⇒ Route
187 188 189 190 191 192 |
# File 'lib/capsium/package/routes_config.rb', line 187 def update(path, updated_path, updated_target) route = resolve(path) route.path = updated_path route.resource = updated_target route end |