Class: ZtreningM::Lines

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

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Lines

Returns a new instance of Lines.



14
15
16
17
18
19
20
# File 'lib/ztrening.rb', line 14

def initialize(filename=nil)
  throw "File arg ne smije biti prazan" if filename.nil?
  throw "File not exist" if !File.exist? filename
  @file = File.readlines filename
  @lines = @file.collect{|v| v=v.chop if v.end_with? "\n"; v}
  @filename = filename
end

Instance Method Details

#add(line = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ztrening.rb', line 30

def add line=nil
  throw "line arg prazan!" if line.nil?
  if (@lines.grep line)[0]
    r=0
  else
    @lines << line
    r=1
  end
  write!
  r
end

#delete(line) ⇒ Object



26
27
28
# File 'lib/ztrening.rb', line 26

def delete line
  @lines.delete line
end

#find(line) ⇒ Object



22
23
24
# File 'lib/ztrening.rb', line 22

def find line
  @lines.grep line || nil
end

#linesObject



42
43
44
# File 'lib/ztrening.rb', line 42

def lines
  @lines
end

#write!Object



46
47
48
# File 'lib/ztrening.rb', line 46

def write!
  File.open(@filename,'w'){ |f| f.write "#{@lines.compact.join("\n")}\n" }
end