Class: SycLink::Designer
- Inherits:
-
Object
- Object
- SycLink::Designer
- Includes:
- Infrastructure
- Defined in:
- lib/syclink/designer.rb
Overview
Creates a designer that acts as a proxy between the user and the website. The designer will create a website with the provided title.
Instance Attribute Summary collapse
-
#website ⇒ Object
The website the designer is working on.
Instance Method Summary collapse
-
#add_link(url, args = {}) ⇒ Object
Adds a link based on the provided arguments to the website.
-
#add_links_from_file(file) ⇒ Object
Reads arguments from a CSV file and creates links accordingly.
-
#create_website(directory, template_filename) ⇒ Object
Creates the html representation of the website.
-
#delete_website(directory) ⇒ Object
Deletes the website if it already exists.
-
#export(format) ⇒ Object
Export links to specified format.
-
#find_links(search) ⇒ Object
Finds links based on a search string.
-
#import_links(importer) ⇒ Object
Accepts and SycLink::Importer to import Links and add them to the website.
-
#list_links(args = {}) ⇒ Object
List links contained in the website and optionally filter on attributes.
-
#load_website(website) ⇒ Object
Loads a website based on the provided YAML-file and asigns it to the designer to operate on.
-
#merge_links ⇒ Object
Merge links with same URL.
-
#new_website(title = "SYC LINK") ⇒ Object
Creates a new website where designer can operate on.
-
#remove_links(urls) ⇒ Object
Deletes one or more links from the website.
-
#save_website(directory) ⇒ Object
Saves the website to the specified directory with the downcased name of the website and the extension ‘website’.
-
#update_link(url, args) ⇒ Object
Updates a link.
- #update_links_from_file(file) ⇒ Object
Methods included from Infrastructure
#copy_file_if_missing, #create_directory_if_missing, #html_file, #load_config, #yaml_file
Instance Attribute Details
#website ⇒ Object
The website the designer is working on
16 17 18 |
# File 'lib/syclink/designer.rb', line 16 def website @website end |
Instance Method Details
#add_link(url, args = {}) ⇒ Object
Adds a link based on the provided arguments to the website
24 25 26 |
# File 'lib/syclink/designer.rb', line 24 def add_link(url, args = {}) website.add_link(Link.new(url, args)) end |
#add_links_from_file(file) ⇒ Object
Reads arguments from a CSV file and creates links accordingly. The links are added to the websie
30 31 32 33 34 35 36 37 38 |
# File 'lib/syclink/designer.rb', line 30 def add_links_from_file(file) File.foreach(file) do |line| next if line.chomp.empty? url, name, description, tag = line.chomp.split(';') website.add_link(Link.new(url, { name: name, description: description, tag: tag })) end end |
#create_website(directory, template_filename) ⇒ Object
Creates the html representation of the website. The website is saved to the directory with websites title and needs an erb-template where the links are integrated to. An example template can be found at templates/syclink.html.erb
121 122 123 124 125 126 |
# File 'lib/syclink/designer.rb', line 121 def create_website(directory, template_filename) template = File.read(template_filename) File.open(html_file(directory, website.title), 'w') do |f| f.puts website.to_html(template) end end |
#delete_website(directory) ⇒ Object
Deletes the website if it already exists
111 112 113 114 115 |
# File 'lib/syclink/designer.rb', line 111 def delete_website(directory) if File.exists? yaml_file(directory, website.title) FileUtils.rm(yaml_file(directory, website.title)) end end |
#export(format) ⇒ Object
Export links to specified format
48 49 50 51 52 53 54 55 |
# File 'lib/syclink/designer.rb', line 48 def export(format) = "to_#{format.downcase}" if website.respond_to? website.send() else raise "cannot export to #{format}" end end |
#find_links(search) ⇒ Object
Finds links based on a search string
63 64 65 |
# File 'lib/syclink/designer.rb', line 63 def find_links(search) website.find_links(search) end |
#import_links(importer) ⇒ Object
Accepts and SycLink::Importer to import Links and add them to the website
41 42 43 44 45 |
# File 'lib/syclink/designer.rb', line 41 def import_links(importer) importer.links.each do |link| website.add_link(link) end end |
#list_links(args = {}) ⇒ Object
List links contained in the website and optionally filter on attributes
58 59 60 |
# File 'lib/syclink/designer.rb', line 58 def list_links(args = {}) website.list_links(args) end |
#load_website(website) ⇒ Object
Loads a website based on the provided YAML-file and asigns it to the designer to operate on
106 107 108 |
# File 'lib/syclink/designer.rb', line 106 def load_website(website) @website = YAML.load_file(website) end |
#merge_links ⇒ Object
Merge links with same URL
84 85 86 |
# File 'lib/syclink/designer.rb', line 84 def merge_links website.merge_links_on(:url) end |
#new_website(title = "SYC LINK") ⇒ Object
Creates a new website where designer can operate on
19 20 21 |
# File 'lib/syclink/designer.rb', line 19 def new_website(title = "SYC LINK") @website = Website.new(title) end |
#remove_links(urls) ⇒ Object
Deletes one or more links from the website. Returns the deleted links. Expects the links provided in an array
90 91 92 93 94 |
# File 'lib/syclink/designer.rb', line 90 def remove_links(urls) urls.map { |url| list_links({ url: url }) }.flatten.compact.each do |link| website.remove_link(link) end end |
#save_website(directory) ⇒ Object
Saves the website to the specified directory with the downcased name of the website and the extension ‘website’. The website is save as YAML
98 99 100 101 102 |
# File 'lib/syclink/designer.rb', line 98 def save_website(directory) File.open(yaml_file(directory, website.title), 'w') do |f| YAML.dump(website, f) end end |
#update_link(url, args) ⇒ Object
Updates a link. The link is identified by the URL. If there is more than one link with the same URL, only the first link is updated
69 70 71 |
# File 'lib/syclink/designer.rb', line 69 def update_link(url, args) website.find_links(url).first.update(args) end |
#update_links_from_file(file) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/syclink/designer.rb', line 73 def update_links_from_file(file) File.foreach(file) do |line| next if line.chomp.empty? url, name, description, tag = line.chomp.split(';') website.find_links(url).first.update({ name: name, description: description, tag: tag }) end end |