Class: Hiiro::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/link.rb

Class Method Summary collapse

Instance Method Summary collapse

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

.orderedObject



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}"
  link_tags = tags - Array(exclude_tags)
  tags_str = link_tags.any? ? " " + link_tags.map { |t| "\e[30;104m#{t}\e[0m" }.join(' ') : ""
  "#{num}#{shorthand_str} #{url}#{desc_str}#{tags_str}".strip
end

#matches?(*terms) ⇒ Boolean

Returns:

  • (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

#tagsObject



18
# File 'lib/hiiro/link.rb', line 18

def tags     = Hiiro::Tag.for(self).map(&:name)

#tags=(v) ⇒ Object



19
# File 'lib/hiiro/link.rb', line 19

def tags=(v) ; self.tags_json = Hiiro::DB::JSON.dump(v); end

#to_hObject



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