Class: RailsAiBridge::Services::FileManagementService

Inherits:
RailsAiBridge::Service show all
Defined in:
lib/rails_ai_bridge/services/file_management_service.rb

Overview

Application service for file system operations confined to an allowed base directory.

All paths are expanded and must lie under Rails.root when Rails is loaded, otherwise under Dir.pwd. This limits read/write/delete to the application tree and returns failures (including SecurityError) as RailsAiBridge::Service::Result instead of raising.

Examples:

Write a file (relative to allowed base)

result = Services::FileManagementService.call(:write, path: "tmp/config.yml", content: "key: value")
if result.success?
  puts "File written successfully"
end

Read a file

result = Services::FileManagementService.call(:read, path: "config/database.yml")
if result.success?
  puts result.data
end

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RailsAiBridge::Service

#initialize

Constructor Details

This class inherits a constructor from RailsAiBridge::Service

Class Method Details

.call(operation) ⇒ RailsAiBridge::Service::Result

Returns success or failure with errors.

Parameters:

  • operation (Symbol, nil)

    one of +:write+, +:read+, +:delete+, +:exist?+; +nil+ is rejected with a failure result (see #call)

  • kwargs (Hash)

    operation-specific arguments (+:path+ required for all supported operations; +:content+ required for +:write+)

Returns:



30
31
32
# File 'lib/rails_ai_bridge/services/file_management_service.rb', line 30

def self.call(operation, **)
  new.call(operation, **)
end

Instance Method Details

#call(operation) ⇒ RailsAiBridge::Service::Result

Dispatches the file operation after validating +operation+ and (for supported ops) the path.

Parameters:

  • operation (Symbol, nil)

    +:write+, +:read+, +:delete+, +:exist?+, another value (unsupported), or +nil+. When +nil+, returns failure with error +"Operation cannot be nil"+ and does not touch the filesystem.

  • kwargs (Hash)

    forwarded to the underlying operation (+:path+, +:content+, etc.)

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails_ai_bridge/services/file_management_service.rb', line 41

def call(operation, **)
  return Service::Result.new(false, errors: ['Operation cannot be nil']) if operation.nil?

  case operation.to_sym
  when :write
    write_file(**)
  when :read
    read_file(**)
  when :delete
    delete_file(**)
  when :exist?
    file_exists?(**)
  else
    Service::Result.new(false, errors: ["Unsupported operation: #{operation}"])
  end
end