Class: SycLink::Link

Inherits:
Object
  • Object
show all
Includes:
LinkChecker
Defined in:
lib/syclink/link.rb

Overview

Creates a link with url, name, description and tag

Constant Summary collapse

ATTRS =

Attributes that are accessible

[:url, :name, :description, :tag]

Instance Method Summary collapse

Methods included from LinkChecker

#response, #response_of_file, #response_of_uri, #to_uri

Constructor Details

#initialize(url, params = {}) ⇒ Link

Create a new link with url and params. If params are not provided defaults are used for name the url is used, description is empty and tag is set to ‘untagged’

Usage

Link.new("http://example.com", name: "example",
                               description: "For testing purposes",
                               tag:         "Test,Example")

Params

url

the URL of the link

name

the name of the link. If not given the URL is used

description

the description of the link (optional)

tag

if not given it is set to ‘untagged’



34
35
36
37
38
39
40
# File 'lib/syclink/link.rb', line 34

def initialize(url, params = {})
  @url         = url
  params       = defaults(url).merge(select_defined(params))
  @name        = params[:name]
  @description = params[:description]
  @tag         = params[:tag]
end

Instance Method Details

#contains?(search) ⇒ Boolean

Checks whether the search string is contained in one or more of the attributes. If the search string is found true is returned otherwise false

link.contains?("example.com")

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/syclink/link.rb', line 65

def contains?(search)
  search = search.delete(' ').downcase
  target = instance_variables.map { |v| instance_variable_get v }.join
  target.downcase.delete(' ').scan(search).size > 0
end

#match?(args) ⇒ Boolean

Checks whether the link matches the values provided by args and returns true if so otherwise false

link.match?(name: "Example", tag: "Test")

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/syclink/link.rb', line 55

def match?(args)
  select_defined(args).reduce(true) do |sum, attribute|
    sum = sum && (send(attribute[0]) == attribute[1])
  end 
end

#rowObject

Return the values of the link in an array

link.row


73
74
75
# File 'lib/syclink/link.rb', line 73

def row
  [ url, name, description, tag ]
end

#update(args) ⇒ Object

Updates the attributes of the link specified by args and returns the updated link

link.update(name: "Example website for testing purposes")


45
46
47
48
49
50
# File 'lib/syclink/link.rb', line 45

def update(args)
  select_defined(args).each do |attribute, value|
    send("#{attribute}=", value)
  end
  self
end