Class: Worktrees::Models::WorktreeConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/worktrees/models/worktree_config.rb

Constant Summary collapse

DEFAULT_ROOT =
'~/.worktrees'
NAME_PATTERN =
/^[0-9]{3}-[a-z0-9-]{1,40}$/.freeze
RESERVED_NAMES =
%w[main master].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worktrees_root: DEFAULT_ROOT, default_base: nil, force_cleanup: false, name_pattern: NAME_PATTERN) ⇒ WorktreeConfig

Returns a new instance of WorktreeConfig.



15
16
17
18
19
20
# File 'lib/worktrees/models/worktree_config.rb', line 15

def initialize(worktrees_root: DEFAULT_ROOT, default_base: nil, force_cleanup: false, name_pattern: NAME_PATTERN)
  @worktrees_root = worktrees_root
  @default_base = default_base
  @force_cleanup = force_cleanup
  @name_pattern = name_pattern
end

Instance Attribute Details

#default_baseObject (readonly)

Returns the value of attribute default_base.



13
14
15
# File 'lib/worktrees/models/worktree_config.rb', line 13

def default_base
  @default_base
end

#force_cleanupObject (readonly)

Returns the value of attribute force_cleanup.



13
14
15
# File 'lib/worktrees/models/worktree_config.rb', line 13

def force_cleanup
  @force_cleanup
end

#name_patternObject (readonly)

Returns the value of attribute name_pattern.



13
14
15
# File 'lib/worktrees/models/worktree_config.rb', line 13

def name_pattern
  @name_pattern
end

#worktrees_rootObject (readonly)

Returns the value of attribute worktrees_root.



13
14
15
# File 'lib/worktrees/models/worktree_config.rb', line 13

def worktrees_root
  @worktrees_root
end

Class Method Details

.default_config_pathObject



40
41
42
# File 'lib/worktrees/models/worktree_config.rb', line 40

def self.default_config_path
  File.join(Dir.home, '.worktrees', 'config.yml')
end

.load(config_path = default_config_path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/worktrees/models/worktree_config.rb', line 22

def self.load(config_path = default_config_path)
  if File.exist?(config_path)
    begin
      config_data = YAML.load_file(config_path)
      new(
        worktrees_root: config_data['worktrees_root'] || DEFAULT_ROOT,
        default_base: config_data['default_base'],
        force_cleanup: config_data['force_cleanup'] || false,
        name_pattern: config_data['name_pattern'] ? Regexp.new(config_data['name_pattern']) : NAME_PATTERN
      )
    rescue Psych::SyntaxError => e
      raise Error, "Invalid configuration file #{config_path}: #{e.message}"
    end
  else
    new
  end
end

Instance Method Details

#expand_worktrees_rootObject



56
57
58
59
60
61
62
# File 'lib/worktrees/models/worktree_config.rb', line 56

def expand_worktrees_root
  if @worktrees_root.start_with?('~')
    File.expand_path(@worktrees_root)
  else
    File.expand_path(@worktrees_root)
  end
end

#to_hObject



64
65
66
67
68
69
70
71
# File 'lib/worktrees/models/worktree_config.rb', line 64

def to_h
  {
    worktrees_root: @worktrees_root,
    default_base: @default_base,
    force_cleanup: @force_cleanup,
    name_pattern: @name_pattern.source
  }
end

#valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
# File 'lib/worktrees/models/worktree_config.rb', line 44

def valid_name?(name)
  return false unless name.is_a?(String)
  return false unless name.match?(@name_pattern)

  # Extract feature part after NNN-
  feature_part = name.split('-', 2)[1]
  return false if feature_part.nil?

  # Check for reserved names
  !RESERVED_NAMES.include?(feature_part.downcase)
end