lib/classlist.rb in classlist-1.0.0 vs lib/classlist.rb in classlist-1.1.0

- old
+ new

@@ -10,44 +10,56 @@ class Error < StandardError; end extend Forwardable def_delegators :@entries, :each - attr_reader :entries + attr_reader :entries, :operations # Returns the Classlist resulting from adding other to this classlist. def +(other) - result = if other.is_a?(Classlist) - other.merge(self) + case other + when Classlist::Operation + add_operation(other) + self + when Classlist + result = other.merge(self) + Classlist.new(result) else - entries + build_entries(other) + result = entries + build_entries(other) + Classlist.new(result) end - - Classlist.new(result) end def ==(other) return false unless other.is_a?(self.class) - entries == other.entries + resolve_operations(self) + other.resolve_operations + + @entries == other.entries end # Adds the given tokens to the list, omitting any that are already present. def add(tokens) entries = build_entries(tokens) entries.each do |entry| self.entries.push(entry) unless self.entries.include?(entry) end end + def add_operation(other) + @operations << other + end + def include?(token) entries.include?(token) end alias_method :contains, :include? def initialize(entries = []) @entries = build_entries(entries) + @operations = [] end # Returns the item in the list by its index, or null if the index is greater # than or equal to the list's length. def item(index) @@ -89,15 +101,22 @@ end true end + def resolve_operations(original_classlist = self) + operations.each do |operation| + operation.resolve(original_classlist) + end + end + def to_a - entries + resolve_operations(self) + @entries end def to_s - entries.join(" ") + to_a.join(" ") end alias_method :value, :to_s # Removes an existing token from the list and returns false. If the token # doesn't exist it's added and the function returns true.