Module: PlainRecord::Model::List

Defined in:
lib/plain_record/model/list.rb

Overview

Methods code, that is specific for list storage, when each file contain only several entry.

Instance Method Summary collapse

Instance Method Details

#delete_entry(file, entry = nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/plain_record/model/list.rb', line 42

def delete_entry(file, entry = nil)
  if entry.nil? or 1 == @loaded[file].length
    delete_file(file)
  else
    @loaded[file].delete(entry)
    save_file(file)
  end
end

#each_entry(matcher = { }) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/plain_record/model/list.rb', line 34

def each_entry(matcher = { })
  files(matcher).each do |file|
    load_file(file).each do |entry|
      yield entry
    end
  end
end

#load_file(file) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/plain_record/model/list.rb', line 26

def load_file(file)
  unless @loaded.has_key? file
    data = ::YAML.load_file(file)
    @loaded[file] = data.map { |i| self.new(file, i) }
  end
  @loaded[file]
end

#move_entry(entry, from, to) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/plain_record/model/list.rb', line 51

def move_entry(entry, from, to)
  if from
    @loaded[from].delete(entry)
    if @loaded[from].empty?
      delete_file(from)
    else
      save_file(from)
    end
  end
  @loaded[to] = [] unless @loaded.has_key? to
  @loaded[to] << entry
end