Class: Spree::AgentTools::ResourceMap
- Inherits:
-
Object
- Object
- Spree::AgentTools::ResourceMap
- Defined in:
- lib/spree/agent_tools/resource_map.rb
Overview
What the generic tools are allowed to look at.
Not a hand-written list: the Admin API's controllers already declare
their model, serializer and permission scope, so the map is derived from
them — spree_api registers one entry per admin resource controller
after the classes load. Core owns the structure and the refusal below;
a resource no controller serves is not reachable by any agent.
Permissions therefore mirror the controllers by construction rather than
by convention: the catalog has no read_price_lists, and the price-lists
controller rides read_products, so the derived entry does too.
Defined Under Namespace
Classes: Entry
Constant Summary collapse
- UnscopedResourceError =
A registered resource whose model has no store scoping. Raised rather than quietly leaking another store's records.
Class.new(StandardError)
- PARENT_SEGMENT =
The parent segment of a nested path:
/promotions/:promotion_id/promotion_rulesnamespromotions. %r{/(?<parent>[a-z_]+)/:[a-z_]+_id(?:/|\z)}
Class Attribute Summary collapse
-
.deriver ⇒ #call?
How the map fills itself the first time it is read.
Class Method Summary collapse
-
.all ⇒ Array<Entry>
Every registered resource, in registration order.
-
.available_for(context) ⇒ Array<Entry>
Resources this caller may read, so a tool description lists only what they can actually ask about.
-
.derive! ⇒ void
Fills the map once, from whatever
deriverwas registered. - .find(key) ⇒ Entry?
-
.register(key:, model_name:, permission:, serializer_name:, dashboard_path: nil, scope_name: nil, write_permission: nil, writable_attributes: [], create_workflow_key: nil, update_workflow_key: nil, distinct: nil, api_paths: nil, index_paths: nil, access_permissions: nil) ⇒ Entry
Registers one resource.
-
.reset! ⇒ void
Forgets every registration, so the derivation can run again from scratch on a code reload.
-
.stale! ⇒ void
Marks the map for rebuilding on its next read.
Class Attribute Details
.deriver ⇒ #call?
How the map fills itself the first time it is read. spree_api sets
this to its controller walk; core owns only the structure, so an
installation without the API engine simply has an empty map.
422 423 424 |
# File 'lib/spree/agent_tools/resource_map.rb', line 422 def deriver @deriver end |
Class Method Details
.all ⇒ Array<Entry>
Returns every registered resource, in registration order.
425 426 427 428 |
# File 'lib/spree/agent_tools/resource_map.rb', line 425 def all derive! entries.values end |
.available_for(context) ⇒ Array<Entry>
Resources this caller may read, so a tool description lists only what they can actually ask about.
534 535 536 |
# File 'lib/spree/agent_tools/resource_map.rb', line 534 def available_for(context) all.select { |entry| entry.readable_by?(context) } end |
.derive! ⇒ void
This method returns an undefined value.
Fills the map once, from whatever deriver was registered.
Under a mutex, because the deriver rebuilds in place — it clears the map and registers each entry — so a second thread reading between those two moments would see an empty map and report every resource as unknown.
The flag is cleared again if the deriver raises: the map is empty by
then, and leaving it marked derived would answer "unknown resource"
for the rest of the process, since stale! only runs at boot and on
reload. A failed derivation is retried by the next caller instead.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/spree/agent_tools/resource_map.rb', line 511 def derive! return if @derived || deriver.nil? derive_mutex.synchronize do return if @derived # Set before calling, so a deriver that reads the map while filling # it does not recurse. @derived = true begin deriver.call rescue StandardError @derived = false raise end end end |
.find(key) ⇒ Entry?
484 485 486 487 |
# File 'lib/spree/agent_tools/resource_map.rb', line 484 def find(key) derive! entries[key.to_s] end |
.register(key:, model_name:, permission:, serializer_name:, dashboard_path: nil, scope_name: nil, write_permission: nil, writable_attributes: [], create_workflow_key: nil, update_workflow_key: nil, distinct: nil, api_paths: nil, index_paths: nil, access_permissions: nil) ⇒ Entry
Registers one resource. Called by spree_api for each admin resource
controller; an extension with its own admin controller is picked up
by the same walk, so nothing registers by hand.
Re-registering a key replaces it, because the walk runs again on every code reload in development.
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/spree/agent_tools/resource_map.rb', line 457 def register(key:, model_name:, permission:, serializer_name:, dashboard_path: nil, scope_name: nil, write_permission: nil, writable_attributes: [], create_workflow_key: nil, update_workflow_key: nil, distinct: nil, api_paths: nil, index_paths: nil, access_permissions: nil) entry = Entry.new(key: key.to_s, model_name: model_name, permission: , serializer_name: serializer_name, dashboard_path: dashboard_path, scope_name: scope_name, write_permission: , writable_attributes: writable_attributes, create_workflow_key: create_workflow_key, update_workflow_key: update_workflow_key, distinct: distinct, api_paths: api_paths&.to_a, index_paths: index_paths, access_permissions: ) entries[entry.key] = entry end |
.reset! ⇒ void
This method returns an undefined value.
Forgets every registration, so the derivation can run again from scratch on a code reload.
478 479 480 |
# File 'lib/spree/agent_tools/resource_map.rb', line 478 def reset! @entries = {} end |
.stale! ⇒ void
This method returns an undefined value.
Marks the map for rebuilding on its next read. Called on every code reload, because a controller edited in development changes what the map should say — but the rebuild itself waits for a reader.
494 495 496 |
# File 'lib/spree/agent_tools/resource_map.rb', line 494 def stale! @derived = false end |