classlist 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b46ec60d2055c6c957a429f49af57adf78b063f933334cdac92d9b62a669f6f
4
- data.tar.gz: b1ead5e0df053f41a9faf85d4b02345910e9efe5479ec5cb483eba116aae0975
3
+ metadata.gz: 13b03b38d0d38240cdabd34d10b534b811b5b292eea5c4e43eabd24f5ab199dc
4
+ data.tar.gz: d83e6fac0aacdb375188e2d5b633bc0a3e7f2f758ef1ff6262ac93ca544b8b5a
5
5
  SHA512:
6
- metadata.gz: 2012cbfeea055153c58050c0e6061157bca4228c97372cddb5b7c0072abac3969c9fb20be3ef8e30c56d58845aa9c521e2b4f2b787d3c0a9c8d3bfc7afa28a41
7
- data.tar.gz: 84a6d60146e626e2e0cef6cc6b4b31a72e459d1dcbe01848ebeb5bf4907d0eb04c78c50564c10dfdab53099aecd18a9055b447f13c88be9aaa890c1c36cfdc21
6
+ metadata.gz: 00bff560b1cd9064b353dd1979f0d5644bbe1e861d6ea0d646e4a25cb16c7eb3ae51b66e49bbcf13fb15531065a6b9d5d4818879088ceca8434174bffc082075
7
+ data.tar.gz: 418764901609311aea4f245cda2d297110a932402a61aa4999f79955e37e4221f8509b6a91a5f86e168ca7cff62c8b1455b8156be3a97b517d4c81ec3ec92311
data/CHANGELOG.md CHANGED
@@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9
9
 
10
10
  ### Added
11
11
 
12
- -
12
+ - Support for chains of operations longer than the most simple cases.
13
+ - Introduce Classlist::Operation as a common super class for Classlist::Add, Classlist::Remove, Classlist::Reset.
14
+ - Classlist::Add that adds all entries when merged
13
15
 
14
16
  ## [1.0.0]
15
17
 
data/README.md CHANGED
@@ -8,6 +8,8 @@ Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
10
  gem 'classlist'
11
+ # or if you don't want to manually require stuff:
12
+ gem 'classlist', require: 'classlist/all'
11
13
  ```
12
14
 
13
15
  And then execute:
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "classlist/operation"
4
+
5
+ # Classlist::Add is an operation that adds tokens to the original
6
+ # classlist when added to it.
7
+ class Classlist::Add < Classlist::Operation
8
+ def merge(original)
9
+ original.entries + entries
10
+ end
11
+
12
+ # resolve changes the original classlist
13
+ def resolve(original)
14
+ entries.each do |entry|
15
+ original.add(entry)
16
+ end
17
+
18
+ super
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Require this file to load all of Classlist into your application.
4
+ #
5
+ # For example in a Gemfile:
6
+ #
7
+ # gem "classlist", require: "classlist/all"
8
+ #
9
+ # Or you can just require what you need manually.
10
+
11
+ require "classlist/add"
12
+ require "classlist/remove"
13
+ require "classlist/reset"
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "classlist"
4
+
5
+ # Classlist::Operations modify the original classlist
6
+ class Classlist::Operation < Classlist
7
+ # resolve changes the original classlist
8
+ def resolve(original_classlist)
9
+ resolve_operations(original_classlist)
10
+ end
11
+ end
@@ -1,11 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "classlist"
3
+ require "classlist/operation"
4
4
 
5
- # Classlist::Remove is a classlist that removes tokens from the original
5
+ # Classlist::Remove is an operation that removes tokens from the original
6
6
  # classlist when added to it.
7
- class Classlist::Remove < Classlist
7
+ class Classlist::Remove < Classlist::Operation
8
8
  def merge(original)
9
9
  original.entries - entries
10
10
  end
11
+
12
+ # #resolve changes the original classlist
13
+ def resolve(original)
14
+ entries.each do |entry|
15
+ original.remove(entry)
16
+ end
17
+
18
+ super
19
+ end
11
20
  end
@@ -1,11 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "classlist"
3
+ require "classlist/operation"
4
4
 
5
- # Classlist::Reset is a classlist that removes all tokens from the original
5
+ # Classlist::Reset is an operation that removes all tokens from the original
6
6
  # classlist when merged.
7
- class Classlist::Reset < Classlist
7
+ class Classlist::Reset < Classlist::Operation
8
8
  def merge(original)
9
9
  original.entries.replace(entries)
10
10
  end
11
+
12
+ # #resolve changes the original classlist
13
+ def resolve(original)
14
+ original.entries.replace(entries)
15
+
16
+ super
17
+ end
11
18
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Classlist
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
data/lib/classlist.rb CHANGED
@@ -12,23 +12,30 @@ class Classlist
12
12
  extend Forwardable
13
13
  def_delegators :@entries, :each
14
14
 
15
- attr_reader :entries
15
+ attr_reader :entries, :operations
16
16
 
17
17
  # Returns the Classlist resulting from adding other to this classlist.
18
18
  def +(other)
19
- result = if other.is_a?(Classlist)
20
- other.merge(self)
19
+ case other
20
+ when Classlist::Operation
21
+ add_operation(other)
22
+ self
23
+ when Classlist
24
+ result = other.merge(self)
25
+ Classlist.new(result)
21
26
  else
22
- entries + build_entries(other)
27
+ result = entries + build_entries(other)
28
+ Classlist.new(result)
23
29
  end
24
-
25
- Classlist.new(result)
26
30
  end
27
31
 
28
32
  def ==(other)
29
33
  return false unless other.is_a?(self.class)
30
34
 
31
- entries == other.entries
35
+ resolve_operations(self)
36
+ other.resolve_operations
37
+
38
+ @entries == other.entries
32
39
  end
33
40
 
34
41
  # Adds the given tokens to the list, omitting any that are already present.
@@ -39,6 +46,10 @@ class Classlist
39
46
  end
40
47
  end
41
48
 
49
+ def add_operation(other)
50
+ @operations << other
51
+ end
52
+
42
53
  def include?(token)
43
54
  entries.include?(token)
44
55
  end
@@ -46,6 +57,7 @@ class Classlist
46
57
 
47
58
  def initialize(entries = [])
48
59
  @entries = build_entries(entries)
60
+ @operations = []
49
61
  end
50
62
 
51
63
  # Returns the item in the list by its index, or null if the index is greater
@@ -91,12 +103,19 @@ class Classlist
91
103
  true
92
104
  end
93
105
 
106
+ def resolve_operations(original_classlist = self)
107
+ operations.each do |operation|
108
+ operation.resolve(original_classlist)
109
+ end
110
+ end
111
+
94
112
  def to_a
95
- entries
113
+ resolve_operations(self)
114
+ @entries
96
115
  end
97
116
 
98
117
  def to_s
99
- entries.join(" ")
118
+ to_a.join(" ")
100
119
  end
101
120
  alias_method :value, :to_s
102
121
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classlist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakob Skjerning
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-04 00:00:00.000000000 Z
11
+ date: 2022-11-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Serverside manipulation of lists of CSS classnames that play nicely with
14
14
  View Components.
@@ -28,6 +28,9 @@ files:
28
28
  - bin/setup
29
29
  - classlist.gemspec
30
30
  - lib/classlist.rb
31
+ - lib/classlist/add.rb
32
+ - lib/classlist/all.rb
33
+ - lib/classlist/operation.rb
31
34
  - lib/classlist/remove.rb
32
35
  - lib/classlist/reset.rb
33
36
  - lib/classlist/version.rb