Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding missing files to branch #41

Open
wants to merge 1 commit into
base: practice-day4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
inherit_from: ../../../.rubocop.yml

AllCops:
NewCops: enable
TargetRubyVersion: 3.x
59 changes: 59 additions & 0 deletions file_manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

# Module for file manipulation, defined as module for expansion
module FileManager
require 'tempfile'
require 'fileutils'
require 'find'
# Class archiver for files management
class Archiver
attr_accessor :file, :modes

@modes = %w[reader writer both write_start]
# p "creating methods #{@modes}"

@modes.each do |mode|
define_method("open_file_as_#{mode}") do |file|
@file = File.open(file, 'r') if mode.to_sym == :reader
@file = File.open(file, 'w') if mode.to_sym == :writer
@file = File.open(file, 'r+') if mode.to_sym == :both
end
end

def write_file(data: nil, append: true)
append ? File.write(@file, data, mode: 'a') : File.write(@file, data)
end

def read_file(lines: false)
lines ? file.read : file.readlines.map(&:chomp)
end

def close
@file.close
end
end
end

# Create new instance
file = FileManager::Archiver.new

# Testing append default
file.open_file_as_writer('test.txt')
file.write_file(data: 'testing archiver class')
file.close

file.open_file_as_writer('test.txt')
file.write_file(data: 'testing archiver class line 2')
file.close

file.open_file_as_reader('test.txt')
p file.read_file(lines: false)
file.close

file.open_file_as_writer('test.txt')
file.write_file(data: 'Removed everything!', append: false)
file.close

file.open_file_as_reader('test.txt')
puts file.read_file(lines: true)
file.close
1 change: 1 addition & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed everything!