Class: Capsium::Package::RoutesConfig

Inherits:
Lutaml::Model::Serializable
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoutesConfig

Returns a new instance of RoutesConfig.

Parameters:



85
# File 'sig/capsium/package/routes_config.rbs', line 85

def initialize: (?index: String? index, ?routes: Array[Route] routes) -> void

Instance Attribute Details

#indexString?

Returns the value of attribute index.

Returns:

  • (String, nil)


82
83
84
# File 'sig/capsium/package/routes_config.rbs', line 82

def index
  @index
end

#routesArray[Route]

Returns the value of attribute routes.

Returns:



83
84
85
# File 'sig/capsium/package/routes_config.rbs', line 83

def routes
  @routes
end

Class Method Details

.from_json(json) ⇒ instance

Parameters:

  • json (String)

Returns:

  • (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

Parameters:

  • path (String)
  • target (String)

Returns:



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]

Parameters:

  • path (String)

Returns:



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?

Parameters:

  • path (String)

Returns:



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.

Parameters:



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.

Parameters:



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

Returns:



198
199
200
201
# File 'lib/capsium/package/routes_config.rb', line 198

def sort!
  self.routes = routes.sort_by(&:path)
  self
end

#to_hashHash[String, untyped]

Returns:

  • (Hash[String, untyped])


104
# File 'sig/capsium/package/routes_config.rbs', line 104

def to_hash: () -> Hash[String, untyped]

#to_jsonString

Parameters:

  • args (Object)

Returns:

  • (String)


103
# File 'sig/capsium/package/routes_config.rbs', line 103

def to_json: (*untyped args) -> String

#update(path, updated_path, updated_target) ⇒ Route

Parameters:

  • path (String)
  • updated_path (String)
  • updated_target (String)

Returns:



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