Class: RubynCode::Hooks::SettingsJsonLoader
- Inherits:
-
Object
- Object
- RubynCode::Hooks::SettingsJsonLoader
- Defined in:
- lib/rubyn_code/hooks/settings_json_loader.rb
Overview
Loads external hook commands from a Claude Code-compatible settings.json.
Schema (matches Claude Code’s hook config):
{
"hooks": {
"PreToolUse": [
{
"matcher": "bash|write_file", // regex or "*"
"hooks": [
{ "type": "command",
"command": "/usr/local/bin/policy-check",
"timeout": 60,
"env": { "FOO": "bar" } // optional
}
]
}
],
"PostToolUse": [ ... ],
"UserPromptSubmit": [ ... ],
"SessionStart": [ ... ],
"SessionEnd": [ ... ],
"Stop": [ ... ],
"SubagentStop": [ ... ],
"PreCompact": [ ... ],
"Notification": [ ... ]
}
}
“matcher” may be:
- a regex string matched against the tool name (PreToolUse/PostToolUse)
or the session id (other events accept any value);
- "*" to match everything;
- omitted/null to match everything.
The loader does not validate that commands exist on disk — that’s the Executor’s job (it will fail at fire time with a clear error).
Defined Under Namespace
Classes: LoadError
Instance Attribute Summary collapse
-
#search_paths ⇒ Array<String>
readonly
Paths the loader will try, in order.
Instance Method Summary collapse
-
#initialize(project_root:, home_dir: nil) ⇒ SettingsJsonLoader
constructor
A new instance of SettingsJsonLoader.
-
#load ⇒ Hash<String, Array<Hash>>
Loads and merges settings.json from project + global.
Constructor Details
#initialize(project_root:, home_dir: nil) ⇒ SettingsJsonLoader
Returns a new instance of SettingsJsonLoader.
53 54 55 56 57 58 59 60 |
# File 'lib/rubyn_code/hooks/settings_json_loader.rb', line 53 def initialize(project_root:, home_dir: nil) @home_dir = home_dir || Config::Defaults::HOME_DIR @project_root = project_root @search_paths = [ File.join(@project_root, '.rubyn-code', 'settings.json'), File.join(@home_dir, 'settings.json') ].freeze end |
Instance Attribute Details
#search_paths ⇒ Array<String> (readonly)
Returns paths the loader will try, in order.
49 50 51 |
# File 'lib/rubyn_code/hooks/settings_json_loader.rb', line 49 def search_paths @search_paths end |
Instance Method Details
#load ⇒ Hash<String, Array<Hash>>
Loads and merges settings.json from project + global.
Project wins for the same matcher/event combination: if both files define a hook for the same event, the project’s hook runs first and the global hook runs after, mirroring Claude Code’s behaviour.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rubyn_code/hooks/settings_json_loader.rb', line 70 def load merged = {} @search_paths.each do |path| next unless File.exist?(path) data = parse_file(path) merge_into!(merged, data['hooks'] || {}) end merged end |