Class: Vimdb::User

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

Instance Method Summary collapse

Constructor Details

#initialize(item, db) ⇒ User

Returns a new instance of User.



3
4
5
6
# File 'lib/vimdb/user.rb', line 3

def initialize(item, db)
  @item, @db = item, db
  @reload = !File.exists?(@db.file)
end

Instance Method Details

#itemsObject



8
9
10
11
# File 'lib/vimdb/user.rb', line 8

def items
  !@reload && @db.get(@item.key) ||
    @db.set(@item.key, @item.create).tap { @reload = false }
end

#reloadObject



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

def reload
  @reload = true
end

#search(query, options = {}) ⇒ Object



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

def search(query, options = {})
  results = @item.search(items.dup, query, options)
  results = items - results if options[:not]
  sort = options[:sort] || options[:field] || @item.default_field
  results.sort_by! {|e| e[sort.to_sym] || '' }
  results.reverse! if options[:reverse_sort]
  results
end