Class: Appydave::Tools::Jump::Commands::Add

Inherits:
Base
  • Object
show all
Defined in:
lib/appydave/tools/jump/commands/add.rb

Overview

Add command creates a new location entry

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #options, #path_validator

Instance Method Summary collapse

Constructor Details

#initialize(config, attrs, path_validator: PathValidator.new, **options) ⇒ Add

Returns a new instance of Add.



11
12
13
14
# File 'lib/appydave/tools/jump/commands/add.rb', line 11

def initialize(config, attrs, path_validator: PathValidator.new, **options)
  super(config, path_validator: path_validator, **options)
  @attrs = attrs
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



9
10
11
# File 'lib/appydave/tools/jump/commands/add.rb', line 9

def attrs
  @attrs
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/appydave/tools/jump/commands/add.rb', line 16

def run
  # Validate required fields
  return error_result('Key is required', code: 'INVALID_INPUT') if attrs[:key].nil? || attrs[:key].empty?
  return error_result('Path is required', code: 'INVALID_INPUT') if attrs[:path].nil? || attrs[:path].empty?

  # Check for duplicate
  return error_result("Location '#{attrs[:key]}' already exists", code: 'DUPLICATE_KEY') if config.key_exists?(attrs[:key])

  # Validate path exists (optional warning)
  unless path_validator.exists?(attrs[:path])
    # Just warn, don't fail - path might be created later
    @path_warning = "Warning: Path '#{attrs[:path]}' does not exist"
  end

  # Create and validate location
  location = Location.new(attrs)
  errors = location.validate
  return error_result("Invalid location: #{errors.join(', ')}", code: 'INVALID_INPUT') unless errors.empty?

  # Add to config
  config.add(location)
  config.save

  result = success_result(
    message: "Location '#{attrs[:key]}' added successfully",
    location: location.to_h
  )
  result[:warning] = @path_warning if @path_warning
  result
rescue ArgumentError => e
  error_result(e.message, code: 'INVALID_INPUT')
end