Class: Vimdb::Item
- Inherits:
-
Object
- Object
- Vimdb::Item
- Defined in:
- lib/vimdb/item.rb
Class Method Summary collapse
Instance Method Summary collapse
- #create ⇒ Object
- #default_field ⇒ Object
- #fields ⇒ Object
- #info ⇒ Object
-
#key ⇒ Object
key used to store item in DB.
- #search(items, query, options = {}) ⇒ Object
Class Method Details
.all ⇒ Object
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_name ⇒ Object
15 16 17 |
# File 'lib/vimdb/item.rb', line 15 def self.item_name name[/\w+$/].downcase end |
Instance Method Details
#create ⇒ Object
45 46 47 |
# File 'lib/vimdb/item.rb', line 45 def create raise NotImplementedError end |
#default_field ⇒ Object
57 58 59 |
# File 'lib/vimdb/item.rb', line 57 def default_field fields[0] end |
#fields ⇒ Object
53 54 55 |
# File 'lib/vimdb/item.rb', line 53 def fields raise NotImplementedError end |
#info ⇒ Object
49 50 51 |
# File 'lib/vimdb/item.rb', line 49 def info raise NotImplementedError end |
#key ⇒ Object
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, = {}) if query query = Regexp.escape(query) unless [:regexp] regex = Regexp.new(query, [:ignore_case]) if [:all] items.select! {|item| fields.any? {|field| item[field] =~ regex } } else search_field = [:field] ? [:field].to_sym : default_field items.select! {|item| item[search_field] =~ regex } end end items end |