Class: Classlist

Inherits:
Object
  • Object
show all
Defined in:
lib/classlist.rb,
lib/classlist/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries = []) ⇒ Classlist



18
19
20
# File 'lib/classlist.rb', line 18

def initialize(entries = [])
  @entries = build_entries(entries)
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



8
9
10
# File 'lib/classlist.rb', line 8

def entries
  @entries
end

Instance Method Details

#add(tokens) ⇒ Object

Adds the given tokens to the list, omitting any that are already present.



11
12
13
14
15
16
# File 'lib/classlist.rb', line 11

def add(tokens)
  entries = build_entries(tokens)
  entries.each do |entry|
    self.entries.push(entry) unless self.entries.include?(entry)
  end
end

#remove(tokens) ⇒ Object

Removes the specified tokens from the classlist, ignoring any that are not present.



24
25
26
27
28
29
# File 'lib/classlist.rb', line 24

def remove(tokens)
  entries = build_entries(tokens)
  entries.each do |entry|
    self.entries.delete(entry)
  end
end

#replace(old_token, new_token) ⇒ Object

Replaces an existing token with a new token. If the first token doesn’t exist, #replace returns false immediately, without adding the new token to the token list.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/classlist.rb', line 34

def replace(old_token, new_token)
  return false unless entries.include?(old_token)

  if entries.include?(new_token)
    entries.delete(old_token)
  else
    index = entries.index(old_token)
    entries[index] = new_token
  end

  true
end

#to_aObject



47
48
49
# File 'lib/classlist.rb', line 47

def to_a
  entries
end

#to_sObject



51
52
53
# File 'lib/classlist.rb', line 51

def to_s
  entries.join(" ")
end

#toggle(token, force = nil) ⇒ Object

Removes an existing token from the list and returns false. If the token doesn’t exist it’s added and the function returns true.

If force is included, it turns the toggle into a one way-only operation. If set to false, then token will only be removed, but not added. If set to true, then token will only be added, but not removed.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/classlist.rb', line 61

def toggle(token, force = nil)
  if entries.include?(token)
    entries.delete(token) unless force == true
    result = false
  else
    entries.push(token) unless force == false
    result = true
  end

  if force.nil?
    result
  else
    force
  end
end