Class: Classlist
- Inherits:
-
Object
- Object
- Classlist
- Extended by:
- Forwardable
- Defined in:
- lib/classlist.rb,
lib/classlist/version.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Add, ArgumentError, Error, Operation, Remove, Reset
Constant Summary collapse
- VERSION =
"1.1.0"
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#operations ⇒ Object
readonly
Returns the value of attribute operations.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Returns the Classlist resulting from adding other to this classlist.
- #==(other) ⇒ Object
-
#add(tokens) ⇒ Object
Adds the given tokens to the list, omitting any that are already present.
- #add_operation(other) ⇒ Object
- #include?(token) ⇒ Boolean (also: #contains)
-
#initialize(entries = []) ⇒ Classlist
constructor
A new instance of Classlist.
-
#item(index) ⇒ Object
Returns the item in the list by its index, or null if the index is greater than or equal to the list's length.
-
#length ⇒ Object
An integer representing the number of objects stored in the object.
-
#merge(classlist) ⇒ Object
Returns a list of tokens in this classlist merged with the given classlist.
-
#remove(tokens) ⇒ Object
Removes the specified tokens from the classlist, ignoring any that are not present.
-
#replace(old_token, new_token) ⇒ Object
Replaces an existing token with a new token.
- #resolve_operations(original_classlist = self) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object (also: #value)
-
#toggle(token, force = nil) ⇒ Object
Removes an existing token from the list and returns false.
Constructor Details
#initialize(entries = []) ⇒ Classlist
Returns a new instance of Classlist.
58 59 60 61 |
# File 'lib/classlist.rb', line 58 def initialize(entries = []) @entries = build_entries(entries) @operations = [] end |
Instance Attribute Details
#entries ⇒ Object
Returns the value of attribute entries.
15 16 17 |
# File 'lib/classlist.rb', line 15 def entries @entries end |
#operations ⇒ Object (readonly)
Returns the value of attribute operations.
15 16 17 |
# File 'lib/classlist.rb', line 15 def operations @operations end |
Instance Method Details
#+(other) ⇒ Object
Returns the Classlist resulting from adding other to this classlist.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/classlist.rb', line 18 def +(other) case other when Classlist::Operation add_operation(other) self when Classlist result = other.merge(self) Classlist.new(result) else result = entries + build_entries(other) Classlist.new(result) end end |
#==(other) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/classlist.rb', line 32 def ==(other) return false unless other.is_a?(self.class) resolve_operations(self) other.resolve_operations @entries == other.entries end |
#add(tokens) ⇒ Object
Adds the given tokens to the list, omitting any that are already present.
42 43 44 45 46 47 |
# File 'lib/classlist.rb', line 42 def add(tokens) entries = build_entries(tokens) entries.each do |entry| self.entries.push(entry) unless self.entries.include?(entry) end end |
#add_operation(other) ⇒ Object
49 50 51 |
# File 'lib/classlist.rb', line 49 def add_operation(other) @operations << other end |
#include?(token) ⇒ Boolean Also known as: contains
53 54 55 |
# File 'lib/classlist.rb', line 53 def include?(token) entries.include?(token) end |
#item(index) ⇒ Object
Returns the item in the list by its index, or null if the index is greater than or equal to the list's length.
65 66 67 68 69 |
# File 'lib/classlist.rb', line 65 def item(index) return nil if index.negative? entries[index] end |
#length ⇒ Object
An integer representing the number of objects stored in the object.
72 73 74 |
# File 'lib/classlist.rb', line 72 def length entries.length end |
#merge(classlist) ⇒ Object
Returns a list of tokens in this classlist merged with the given classlist.
77 78 79 |
# File 'lib/classlist.rb', line 77 def merge(classlist) (classlist.entries + entries).uniq end |
#remove(tokens) ⇒ Object
Removes the specified tokens from the classlist, ignoring any that are not present.
83 84 85 86 87 88 |
# File 'lib/classlist.rb', line 83 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.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/classlist.rb', line 93 def replace(old_token, new_token) return false unless include?(old_token) if include?(new_token) remove(old_token) else index = entries.index(old_token) entries[index] = new_token end true end |
#resolve_operations(original_classlist = self) ⇒ Object
106 107 108 109 110 |
# File 'lib/classlist.rb', line 106 def resolve_operations(original_classlist = self) operations.each do |operation| operation.resolve(original_classlist) end end |
#to_a ⇒ Object
112 113 114 115 |
# File 'lib/classlist.rb', line 112 def to_a resolve_operations(self) @entries end |
#to_s ⇒ Object Also known as: value
117 118 119 |
# File 'lib/classlist.rb', line 117 def to_s to_a.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.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/classlist.rb', line 128 def toggle(token, force = nil) raise ArgumentError, "The token can not contain whitespace." if token.to_s.include?(" ") if entries.include?(token) remove(token) unless force == true result = false else add(token) unless force == false result = true end if force.nil? result else force end end |