Class: Vimdb::Item

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

Direct Known Subclasses

Commands, Keys, Options

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



19
20
21
# File 'lib/vimdb/item.rb', line 19

def self.all
  @descendants.map(&:item_name)
end

.inherited(mod) ⇒ Object



5
6
7
# File 'lib/vimdb/item.rb', line 5

def self.inherited(mod)
  (@descendants ||= []) << mod
end

.instance(name) ⇒ Object



9
10
11
12
13
# File 'lib/vimdb/item.rb', line 9

def self.instance(name)
  item = @descendants.find {|e| e.item_name == name } or
    abort "Item '#{name}' not found"
  item.new
end

.item_nameObject



15
16
17
# File 'lib/vimdb/item.rb', line 15

def self.item_name
  name[/\w+$/].downcase
end

Instance Method Details

#createObject

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/vimdb/item.rb', line 45

def create
  raise NotImplementedError
end

#default_fieldObject



57
58
59
# File 'lib/vimdb/item.rb', line 57

def default_field
  fields[0]
end

#fieldsObject

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/vimdb/item.rb', line 53

def fields
  raise NotImplementedError
end

#infoObject

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/vimdb/item.rb', line 49

def info
  raise NotImplementedError
end

#keyObject

key used to store item in DB



41
42
43
# File 'lib/vimdb/item.rb', line 41

def key
  self.class.item_name
end

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vimdb/item.rb', line 23

def search(items, query, options = {})
  if query
    query = Regexp.escape(query) unless options[:regexp]
    regex = Regexp.new(query, options[:ignore_case])

    if options[:all]
      items.select! {|item|
        fields.any? {|field| item[field] =~ regex }
      }
    else
      search_field = options[:field] ? options[:field].to_sym : default_field
      items.select! {|item| item[search_field] =~ regex }
    end
  end
  items
end

#tempfile(name) {|file| ... } ⇒ Object

Yields:

  • (file)


73
74
75
76
77
# File 'lib/vimdb/item.rb', line 73

def tempfile(name)
  file = Tempfile.new(Time.now.to_i.to_s).path
  yield(file)
  file
end