Class: Rubino::Skills::StateRepository
- Inherits:
-
Object
- Object
- Rubino::Skills::StateRepository
- Defined in:
- lib/rubino/skills/state_repository.rb
Overview
Persists per-skill enable/disable flags in the ‘skill_states` table.
Default-enabled semantics: a skill with no row is treated as enabled, so #enabled? returns true for unknown names. Only an explicit #set with ‘enabled: false` disables a skill, and the choice survives restarts.
Writes go through Sequel’s ‘insert_conflict(target: :name)` which maps to SQLite’s ‘INSERT … ON CONFLICT(name) DO UPDATE` (UPSERT).
Instance Method Summary collapse
- #all ⇒ Object
- #enabled?(name) ⇒ Boolean
-
#initialize(db: nil) ⇒ StateRepository
constructor
A new instance of StateRepository.
- #set(name, enabled:) ⇒ Object
Constructor Details
#initialize(db: nil) ⇒ StateRepository
Returns a new instance of StateRepository.
14 15 16 |
# File 'lib/rubino/skills/state_repository.rb', line 14 def initialize(db: nil) @db = db || Rubino.database.db end |
Instance Method Details
#all ⇒ Object
32 33 34 |
# File 'lib/rubino/skills/state_repository.rb', line 32 def all @db[:skill_states].all.to_h { |row| [row[:name], row[:enabled] == true] } end |
#enabled?(name) ⇒ Boolean
18 19 20 21 22 23 |
# File 'lib/rubino/skills/state_repository.rb', line 18 def enabled?(name) row = @db[:skill_states].where(name: name.to_s).first return true if row.nil? row[:enabled] == true end |
#set(name, enabled:) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/rubino/skills/state_repository.rb', line 25 def set(name, enabled:) now = Time.now.utc.iso8601 @db[:skill_states] .insert_conflict(target: :name, update: { enabled: enabled, updated_at: now }) .insert(name: name.to_s, enabled: enabled, updated_at: now) end |