Class: Cronbox::DataFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ DataFile

Returns a new instance of DataFile.



5
6
7
8
9
# File 'lib/cronbox/data_file.rb', line 5

def initialize(file)
  @file = file
  reset
  reload if File.exist?(@file)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



33
34
35
# File 'lib/cronbox/data_file.rb', line 33

def config
  @config
end

#entriesObject

Returns the value of attribute entries.



33
34
35
# File 'lib/cronbox/data_file.rb', line 33

def entries
  @entries
end

Instance Method Details

#add_entry(entry) ⇒ Object



47
48
49
50
# File 'lib/cronbox/data_file.rb', line 47

def add_entry(entry)
  entry['id'] = next_id
  @entries.push entry
end

#del_entry(entry) ⇒ Object



52
53
54
# File 'lib/cronbox/data_file.rb', line 52

def del_entry(entry)
  @entries.delete(entry)
end

#empty!Object



28
29
30
31
# File 'lib/cronbox/data_file.rb', line 28

def empty!
  reset
  save!
end

#find_entry(query) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cronbox/data_file.rb', line 35

def find_entry(query)
  k = query.keys.first
  v = query[k]
  return nil unless v
  @entries.each do |e|
    if e[k.to_s] == v
      return e
    end
  end
  nil
end

#reloadObject



16
17
18
# File 'lib/cronbox/data_file.rb', line 16

def reload
  import_text(File.read(@file))
end

#resetObject



11
12
13
14
# File 'lib/cronbox/data_file.rb', line 11

def reset
  @config ||= Hash.new
  @entries = Array.new
end

#save!Object



20
21
22
23
24
25
26
# File 'lib/cronbox/data_file.rb', line 20

def save!
  save_dir = File.dirname(@file)
  unless Dir.exist?(save_dir)
    raise "Directory does not exist, #{save_dir}"
  end
  File.open(@file, 'w') { |file| file.write(export_text) }
end