Class: ClaudeHooks::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_hooks/configuration.rb

Constant Summary collapse

ENV_PREFIX =
'RUBY_CLAUDE_HOOKS_'

Class Method Summary collapse

Class Method Details

.base_dirObject

Get the base directory from ENV or default (backward compatibility) This method will determine which base directory to use based on context



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/claude_hooks/configuration.rb', line 46

def base_dir
  @base_dir ||= begin
    # Check for legacy environment variable first
    env_base_dir = ENV["#{ENV_PREFIX}BASE_DIR"]
    if env_base_dir
      File.expand_path(env_base_dir)
    else
      # Default to home directory for backward compatibility
      home_claude_dir
    end
  end
end

.configObject

Load the entire config as a hash (from ENV and optional config file)



11
12
13
# File 'lib/claude_hooks/configuration.rb', line 11

def config
  @config ||= load_config
end

.get_config_value(env_key, config_key = nil, default = nil) ⇒ Object

Get any configuration value by key First checks ENV with prefix, then config file, then returns default



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/claude_hooks/configuration.rb', line 94

def get_config_value(env_key, config_key = nil, default = nil)
  # Check environment variable first
  env_value = ENV["#{ENV_PREFIX}#{env_key}"]
  return env_value if env_value

  # Check config file using provided key or converted env_key
  file_key = config_key || env_key_to_config_key(env_key)
  config_value = config.dig(file_key)
  return config_value if config_value

  # Return default
  default
end

.home_claude_dirObject

Get the home Claude directory (always ~/.claude)



27
28
29
# File 'lib/claude_hooks/configuration.rb', line 27

def home_claude_dir
  @home_claude_dir ||= File.expand_path('~/.claude')
end

.home_path_for(relative_path) ⇒ Object

Get the full path for a file/directory relative to home_claude_dir



67
68
69
# File 'lib/claude_hooks/configuration.rb', line 67

def home_path_for(relative_path)
  File.join(home_claude_dir, relative_path)
end

.logs_directoryObject

Get the log directory path (always relative to home_claude_dir)



82
83
84
85
86
87
88
89
# File 'lib/claude_hooks/configuration.rb', line 82

def logs_directory
  log_dir = get_config_value('LOG_DIR', 'logDirectory') || 'logs'
  if log_dir.start_with?('/')
    log_dir  # Absolute path
  else
    File.join(home_claude_dir, log_dir)  # Always relative to home_claude_dir
  end
end

.method_missing(method_name, *args, &block) ⇒ Object

Allow access to any config value using method_missing



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/claude_hooks/configuration.rb', line 109

def method_missing(method_name, *args, &block)
  # Convert method name to ENV key format (e.g., my_custom_setting -> MY_CUSTOM_SETTING)
  env_key = method_name.to_s.upcase
  # Convert snake_case method name to camelCase for config file lookup
  config_key = snake_case_to_camel_case(method_name.to_s)

  value = get_config_value(env_key, config_key)
  return value unless value.nil?

  super
end

.path_for(relative_path, base_directory = nil) ⇒ Object

Get the full path for a file/directory relative to base_dir Can optionally specify which base directory to use



61
62
63
64
# File 'lib/claude_hooks/configuration.rb', line 61

def path_for(relative_path, base_directory = nil)
  base_directory ||= base_dir
  File.join(base_directory, relative_path)
end

.project_claude_dirObject

Get the project Claude directory (from CLAUDE_PROJECT_DIR/.claude) Returns nil if CLAUDE_PROJECT_DIR environment variable is not set



33
34
35
36
37
38
39
40
41
42
# File 'lib/claude_hooks/configuration.rb', line 33

def project_claude_dir
  @project_claude_dir ||= begin
    project_dir = ENV['CLAUDE_PROJECT_DIR']
    if project_dir
      File.expand_path(File.join(project_dir, '.claude'))
    else
      nil
    end
  end
end

.project_path_for(relative_path) ⇒ Object

Get the full path for a file/directory relative to project_claude_dir Returns nil if CLAUDE_PROJECT_DIR environment variable is not set



73
74
75
76
77
78
79
# File 'lib/claude_hooks/configuration.rb', line 73

def project_path_for(relative_path)
  if project_claude_dir
    File.join(project_claude_dir, relative_path)
  else
    nil
  end
end

.reload!Object

Unmemoize config



16
17
18
19
20
21
22
23
24
# File 'lib/claude_hooks/configuration.rb', line 16

def reload!
  @config = nil
  @base_dir = nil
  @home_claude_dir = nil
  @project_claude_dir = nil
  @config_file_path = nil
  @home_config_file_path = nil
  @project_config_file_path = nil
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
# File 'lib/claude_hooks/configuration.rb', line 121

def respond_to_missing?(method_name, include_private = false)
  # Check if we have a config value for this method
  env_key = method_name.to_s.upcase
  config_key = snake_case_to_camel_case(method_name.to_s)

  !get_config_value(env_key, config_key).nil? || super
end