Class: SycLink::Website

Inherits:
Object
  • Object
show all
Includes:
Exporter
Defined in:
lib/syclink/website.rb

Overview

A Website is organizing a link list. The links can be added, updated and removed. It is also possible to search for links. And finally an html representation of the Website can be created.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Exporter

#to_html

Constructor Details

#initialize(title = "Link List") ⇒ Website

Create a new Website



20
21
22
23
# File 'lib/syclink/website.rb', line 20

def initialize(title = "Link List")
  @links = []
  @title = title
end

Instance Attribute Details

The links of the website



15
16
17
# File 'lib/syclink/website.rb', line 15

def links
  @links
end

#titleObject (readonly)

The title of the website



17
18
19
# File 'lib/syclink/website.rb', line 17

def title
  @title
end

Instance Method Details

Add a link to the website



26
27
28
# File 'lib/syclink/website.rb', line 26

def add_link(link)
  links << link
end

Finds all links that contain the search string



45
46
47
# File 'lib/syclink/website.rb', line 45

def find_links(search)
  links.select { |link| link.contains? search }
end

List all attributes of the links



57
58
59
# File 'lib/syclink/website.rb', line 57

def link_attribute_list(attribute)
  links.map { |link| link.send(attribute) }.uniq.sort
end

Groups the links on the provided attribute



50
51
52
53
54
# File 'lib/syclink/website.rb', line 50

def links_group_by(attribute)
  links.map      { |link| { key: link.send(attribute), link: link } }
       .group_by { |entry| entry[:key] }
       .each     { |key, link| link.map! { |l| l[:link] }}
end

List links that match the attributes



36
37
38
39
40
41
42
# File 'lib/syclink/website.rb', line 36

def list_links(args = {})
  if args.empty?
    links
  else
    links.select { |link| link.match? args }
  end
end

Remove a link from the website



31
32
33
# File 'lib/syclink/website.rb', line 31

def remove_link(link)
  links.delete(link)
end