Class: Fsimp

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, permission = "a+") ⇒ Fsimp

Returns a new instance of Fsimp.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/fsimp.rb', line 4

def initialize(name, permission="a+")
  @name = name
  @perm = permission
  if File.file?(@name)
    puts "#{@name} exists!"
  else
    f = File.new(@name, @perm)
    puts "#{@name} created with #{@perm} permissions!"
    f.close
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/fsimp.rb', line 3

def name
  @name
end

#permObject (readonly)

Returns the value of attribute perm.



3
4
5
# File 'lib/fsimp.rb', line 3

def perm
  @perm
end

Instance Method Details

#deleteObject



57
58
59
60
61
62
63
64
# File 'lib/fsimp.rb', line 57

def delete
  if File.file?(@name)
    File.delete(@name)
    delete
  else
    puts "#{@name} deleted successfully!"
  end
end

#filter(text) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fsimp.rb', line 37

def filter(text)
  case text
  when Array
    txt = text.to_s.gsub("[","").gsub("]","")
    filter(txt)
  when Hash
    txt = []
    text.each {|k, v| txt << "#{k}=>#{v}, "}
    filter(txt)
  else
    if File.writable?(@name)
      File.write(@name, " "+text+" \n", mode: "a+")
    else
      puts "you don't have permission to write to this file."
    end
  end
end

#readObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fsimp.rb', line 24

def read 
  if File.readable?(@name)
    lnum = 1
    File.foreach(@name) do |line|
      puts "#{lnum}. #{line}"
      lnum += 1
    end
  else
    puts "you don't have permission to read #{@name}"
  end
end

#write(*text) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fsimp.rb', line 36

def write(*text)
  def filter(text)
    case text
    when Array
      txt = text.to_s.gsub("[","").gsub("]","")
      filter(txt)
    when Hash
      txt = []
      text.each {|k, v| txt << "#{k}=>#{v}, "}
      filter(txt)
    else
      if File.writable?(@name)
        File.write(@name, " "+text+" \n", mode: "a+")
      else
        puts "you don't have permission to write to this file."
      end
    end
  end
  filter(text)
end