Class: FileRecord::Record

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

Class Method Summary collapse

Class Method Details

.read_instances(model, file, options = {}) ⇒ Object

Used by read_model and read_log to load all instances from a file



47
48
49
50
51
52
# File 'lib/file_record/record.rb', line 47

def read_instances( model, file, options={} )
  instances = YAML::load_stream( File.open( file ) )
  instances.each do |i|
    model.new( i )
  end
end

.read_log(model, time, options = {}) ⇒ Object

Read in all log model instances from a time stamped file



62
63
64
65
66
67
68
69
70
# File 'lib/file_record/record.rb', line 62

def read_log( model, time, options={} )

  options[:time] = time
  file_path = FileUtility.new(model, options).file_path

  if File.exists? file_path
    read_instances model, file_path
  end
end

.read_model(model, options = {}) ⇒ Object

Read in all models instances from the model file



55
56
57
58
59
# File 'lib/file_record/record.rb', line 55

def read_model( model, options={} )

  file_path = FileUtility.new(model, options).file_path
  read_instances model, file_path
end

.save_log(model, options = {}) ⇒ Object

record a child of Tempo::Model::Log



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/file_record/record.rb', line 27

def save_log( model, options={} )

  options = options.dup
  options[:create] = true
  options[:destroy] = true

  model.days_index.each do |day, days_logs|

    options[:time] = day
    ut = FileUtility.new(model, options)

    # don't create an empty file
    next if days_logs.empty?

    ut.save_instances_to_file days_logs
  end
end

.save_model(model, options = {}) ⇒ Object

record a child of Tempo::Model::Base



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/file_record/record.rb', line 11

def save_model( model, options={} )

  options = options.dup
  options[:create] = true
  options[:destroy] = true

  file_path = FileUtility.new(model, options).file_path

  File.open( file_path,'a' ) do |f|
    model.index.each do |m|
      f.puts YAML::dump( m.freeze_dry )
    end
  end
end