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

+ (Object) all



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

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

+ (Object) inherited(mod)



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

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

+ (Object) instance(name)



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

+ (Object) item_name



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

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

Instance Method Details

- (Object) create

Raises:

  • (NotImplementedError)


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

def create
  raise NotImplementedError
end

- (Object) default_field



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

def default_field
  fields[0]
end

- (Object) fields

Raises:

  • (NotImplementedError)


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

def fields
  raise NotImplementedError
end

- (Object) info

Raises:

  • (NotImplementedError)


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

def info
  raise NotImplementedError
end

- (Object) key

key used to store item in DB



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

def key
  self.class.item_name
end

- (Object) search(items, query, options = {})



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

- (Object) tempfile(name) {|file| ... }

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