Class: Hiiro::Link
- Inherits:
-
Object
- Object
- Hiiro::Link
- Defined in:
- lib/hiiro/link.rb
Class Method Summary collapse
- .create_table!(db) ⇒ Object
- .find_by_shorthand(s) ⇒ Object
- .ordered ⇒ Object
- .search(query) ⇒ Object
Instance Method Summary collapse
- #display_string(index = nil, exclude_tags: []) ⇒ Object
- #matches?(*terms) ⇒ Boolean
- #tags ⇒ Object
- #tags=(v) ⇒ Object
- #to_h ⇒ Object
Class Method Details
.create_table!(db) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/hiiro/link.rb', line 7 def self.create_table!(db) db.create_table?(:links) do primary_key :id String :url, null: false, unique: true String :description String :shorthand String :tags_json # JSON array of tag strings String :created_at end end |
.find_by_shorthand(s) ⇒ Object
21 22 23 |
# File 'lib/hiiro/link.rb', line 21 def self.find_by_shorthand(s) where(shorthand: s).first end |
.ordered ⇒ Object
36 |
# File 'lib/hiiro/link.rb', line 36 def self.ordered = order(:created_at).all |
.search(query) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hiiro/link.rb', line 25 def self.search(query) q = "%#{query}%" where( Sequel.|( Sequel.like(:url, q), Sequel.like(:description, q), Sequel.like(:shorthand, q) ) ).order(:created_at).all end |
Instance Method Details
#display_string(index = nil, exclude_tags: []) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/hiiro/link.rb', line 43 def display_string(index = nil, exclude_tags: []) num = index ? "#{(index + 1).to_s.rjust(3)}." : "" shorthand_str = shorthand ? " [#{shorthand}]" : "" desc_str = description.to_s.empty? ? "" : " - #{description}" = - Array() = .any? ? " " + .map { |t| "\e[30;104m#{t}\e[0m" }.join(' ') : "" "#{num}#{shorthand_str} #{url}#{desc_str}#{}".strip end |
#matches?(*terms) ⇒ Boolean
38 39 40 41 |
# File 'lib/hiiro/link.rb', line 38 def matches?(*terms) searchable = [url, description, shorthand].compact.join(' ').downcase terms.all? { |term| searchable.include?(term.downcase) } end |
#tags=(v) ⇒ Object
19 |
# File 'lib/hiiro/link.rb', line 19 def (v) ; self. = Hiiro::DB::JSON.dump(v); end |
#to_h ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/hiiro/link.rb', line 52 def to_h { 'url' => url, 'description' => description, 'shorthand' => shorthand, 'created_at' => created_at } end |