Class: SycLink::Link

Inherits:
Object
  • Object
show all
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

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’



16
17
18
19
20
21
22
# File 'lib/syclink/link.rb', line 16

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

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/syclink/link.rb', line 42

def contains?(search)
  search = search.delete(' ')
  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

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/syclink/link.rb', line 33

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

#update(args) ⇒ Object

Updates the attributes of the link specified by args



25
26
27
28
29
# File 'lib/syclink/link.rb', line 25

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