Class: FileScheduler::Log

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/file_scheduler/log.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Log

Returns a new instance of Log.



6
7
8
9
# File 'lib/file_scheduler/log.rb', line 6

def initialize(options = {})
  options.set_attributes self, :max_size => 100
  clear
end

Instance Attribute Details

#max_sizeObject

Returns the value of attribute max_size.



4
5
6
# File 'lib/file_scheduler/log.rb', line 4

def max_size
  @max_size
end

Instance Method Details

#clearObject



21
22
23
# File 'lib/file_scheduler/log.rb', line 21

def clear
  @contents = []
end

#distance(content) ⇒ Object



11
12
13
# File 'lib/file_scheduler/log.rb', line 11

def distance(content)
  @contents.index content
end

#dumpObject



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

def dump
  Marshal.dump(@contents)
end

#each(&block) ⇒ Object



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

def each(&block)
  @contents.each(&block)
end

#load(data) ⇒ Object



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

def load(data)
  return unless data

  # Marshalled data starts with \004
  unless data.start_with?("\004")
    data = IO.read(file) if File.exists?(data)
  end

  @contents = Marshal.load data
  self
end

#log(content) ⇒ Object



15
16
17
18
19
# File 'lib/file_scheduler/log.rb', line 15

def log(content)
  @contents.unshift content
  @contents = @contents.first(max_size)
  content
end

#save(file) ⇒ Object



46
47
48
49
50
# File 'lib/file_scheduler/log.rb', line 46

def save(file)
  File.open(file, "w") do |f|
    f.write dump
  end
end