turbo_reflex 0.0.22 → 0.0.25

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: 0a7328a5d0699434ae297cf74bd329fff233f070838fdf432cc97639346d10fc
4
- data.tar.gz: 3d81fc83f65dce19ae4feebf8ddf2b70cb1e8f9cbea30fe326375b5eb231e7b0
3
+ metadata.gz: c28abca449b5630362e4a5926b270c4c8075a1ef989434c0899355d96390b388
4
+ data.tar.gz: 6a767145f9a32ab240feac42051f0d2be4c4d395202641b4e8caaf3cabe9c242
5
5
  SHA512:
6
- metadata.gz: 49cbf87cb1923f54495f888ceee1520ba889a02d85b84f85014ed2a74a1bbdd0039d975675e1d8f12a028cf4a4a5a526603e0645b081d957d435553736ade6b8
7
- data.tar.gz: 39bdf7de08fb53445a261737dd4c87c142c727408c5ae29b80f257bb025087c31ff14ccdc3029d8e5ddbe76e22823ea9df0703acb6dc5375fc8b1e70da4bd2b0
6
+ metadata.gz: 36568b96f6cfd94e119adec06a2624c12a042447cdbbd5973de82ed8b629135a30ef033f23384da49084fa76a76a3ccc5422e3e0ec5267269cba693f7fcbce1d
7
+ data.tar.gz: 58efc30fb5c49b605b76a5d8998cd6f6d69b4288bad4d3a3b5b1085e26236f0162e282be402e34837e1e33ec78f405cb2cb5034279ee8bf9cb6fdd70a157c02c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- turbo_reflex (0.0.22)
4
+ turbo_reflex (0.0.25)
5
5
  rails (>= 6.1)
6
6
  turbo-rails (>= 1.1)
7
7
  turbo_ready (>= 0.1)
@@ -94,11 +94,11 @@ GEM
94
94
  coderay (1.1.3)
95
95
  concurrent-ruby (1.1.10)
96
96
  crass (1.0.6)
97
- cuprite (0.14.2)
97
+ cuprite (0.14.3)
98
98
  capybara (~> 3.0)
99
- ferrum (~> 0.12.0)
99
+ ferrum (~> 0.13.0)
100
100
  erubi (1.11.0)
101
- ferrum (0.12)
101
+ ferrum (0.13)
102
102
  addressable (~> 2.5)
103
103
  concurrent-ruby (~> 1.1)
104
104
  webrick (~> 1.7)
@@ -193,19 +193,19 @@ GEM
193
193
  regexp_parser (2.6.0)
194
194
  rexml (3.2.5)
195
195
  rouge (4.0.0)
196
- rubocop (1.35.1)
196
+ rubocop (1.39.0)
197
197
  json (~> 2.3)
198
198
  parallel (~> 1.10)
199
199
  parser (>= 3.1.2.1)
200
200
  rainbow (>= 2.2.2, < 4.0)
201
201
  regexp_parser (>= 1.8, < 3.0)
202
202
  rexml (>= 3.2.5, < 4.0)
203
- rubocop-ast (>= 1.20.1, < 2.0)
203
+ rubocop-ast (>= 1.23.0, < 2.0)
204
204
  ruby-progressbar (~> 1.7)
205
205
  unicode-display_width (>= 1.4.0, < 3.0)
206
206
  rubocop-ast (1.23.0)
207
207
  parser (>= 3.1.1.0)
208
- rubocop-performance (1.14.3)
208
+ rubocop-performance (1.15.0)
209
209
  rubocop (>= 1.7.0, < 2.0)
210
210
  rubocop-ast (>= 0.4.0)
211
211
  ruby-progressbar (1.11.0)
@@ -222,15 +222,16 @@ GEM
222
222
  actionpack (>= 5.2)
223
223
  activesupport (>= 5.2)
224
224
  sprockets (>= 3.0.0)
225
+ sqlite3 (1.5.3-aarch64-linux)
225
226
  sqlite3 (1.5.3-arm64-darwin)
226
- standard (1.16.1)
227
- rubocop (= 1.35.1)
228
- rubocop-performance (= 1.14.3)
227
+ standard (1.18.0)
228
+ rubocop (= 1.39.0)
229
+ rubocop-performance (= 1.15.0)
229
230
  standardrb (1.0.1)
230
231
  standard
231
- tailwindcss-rails (2.0.19-aarch64-linux)
232
+ tailwindcss-rails (2.0.21-aarch64-linux)
232
233
  railties (>= 6.0.0)
233
- tailwindcss-rails (2.0.19-arm64-darwin)
234
+ tailwindcss-rails (2.0.21-arm64-darwin)
234
235
  railties (>= 6.0.0)
235
236
  thor (1.2.1)
236
237
  timeout (0.3.0)
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  </h1>
9
9
  <p align="center">
10
10
  <a href="http://blog.codinghorror.com/the-best-code-is-no-code-at-all/">
11
- <img alt="Lines of Code" src="https://img.shields.io/badge/loc-1188-47d299.svg" />
11
+ <img alt="Lines of Code" src="https://img.shields.io/badge/loc-1228-47d299.svg" />
12
12
  </a>
13
13
  <a href="https://codeclimate.com/github/hopsoft/turbo_reflex/maintainability">
14
14
  <img src="https://api.codeclimate.com/v1/badges/fe1162a742fe83a4fdfd/maintainability" />
@@ -112,6 +112,7 @@ TurboReflex is a lightweight Turbo Frame extension... which means that reactivit
112
112
  - [rails](https://rubygems.org/gems/rails) `>=6.1`
113
113
  - [turbo-rails](https://rubygems.org/gems/turbo-rails) `>=1.1`
114
114
  - [@hotwired/turbo-rails](https://yarnpkg.com/package/@hotwired/turbo-rails) `>=7.1`
115
+ - [ruby](https://github.com/ruby/ruby) `>=2.7`
115
116
 
116
117
  ## Setup
117
118
 
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TurboReflex::AttributeSet
4
+ def initialize(prefix, attributes: {})
5
+ prefix = prefix.to_s
6
+ attrs = attributes.to_h.transform_values(&:to_s)
7
+
8
+ attrs.each do |key, value|
9
+ key = key.to_s.strip
10
+
11
+ next unless key.start_with?(prefix)
12
+
13
+ name = key.parameterize.underscore.delete_prefix("#{prefix}_")
14
+ value = value.to_i if value.to_s.match?(/\A\d+\z/)
15
+ value = value == "true" if value.is_a?(String) && value.match?(/\A(true|false)\z/i)
16
+ instance_variable_set "@#{name}", value
17
+
18
+ next if orig_respond_to_missing?(name, false)
19
+
20
+ self.class.define_method(name) { instance_variable_get :"@#{name}" }
21
+ self.class.define_method("#{name}?") { public_send(name).present? }
22
+ end
23
+ end
24
+
25
+ def respond_to?(name, include_all = false)
26
+ respond_to_missing? name, include_all
27
+ end
28
+
29
+ alias_method :orig_respond_to_missing?, :respond_to_missing?
30
+
31
+ def respond_to_missing?(name, include_all)
32
+ true
33
+ end
34
+
35
+ def method_missing(name, *args)
36
+ return false if name.end_with?("?")
37
+ nil
38
+ end
39
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "attribute_set"
4
+
3
5
  # TurboReflex base superclass.
4
6
  # All TurboReflex classes should inherit from this class.
5
7
  #
@@ -99,20 +101,10 @@ class TurboReflex::Base
99
101
  def element
100
102
  @element ||= begin
101
103
  attributes = params[:element_attributes]
102
- attrs = attributes.keys.each_with_object({}) do |key, memo|
103
- memo[:aria] ||= {}
104
- memo[:dataset] ||= {}
105
- if key.start_with?("data_")
106
- memo[:dataset][key[5..].parameterize.underscore.to_sym] = attributes[key]
107
- elsif key.start_with?("aria_")
108
- memo[:aria][key[5..].parameterize.underscore.to_sym] = attributes[key]
109
- else
110
- memo[key.parameterize.underscore.to_sym] = attributes[key]
111
- end
112
- end
113
- attrs[:aria] = OpenStruct.new(attrs[:aria])
114
- attrs[:dataset] = OpenStruct.new(attrs[:dataset])
115
- OpenStruct.new attrs
104
+ OpenStruct.new attributes.merge(
105
+ aria: TurboReflex::AttributeSet.new(:aria, attributes: attributes),
106
+ dataset: TurboReflex::AttributeSet.new(:data, attributes: attributes)
107
+ )
116
108
  end
117
109
  end
118
110
 
@@ -20,6 +20,11 @@ class TurboReflex::ProvisionalState
20
20
  state_manager[key] = value
21
21
  end
22
22
 
23
+ def clear
24
+ keys.each { |key| state_manager[key] = nil }
25
+ keys.clear
26
+ end
27
+
23
28
  private
24
29
 
25
30
  attr_reader :state_manager
@@ -34,8 +34,8 @@ class TurboReflex::State
34
34
  end
35
35
 
36
36
  def initialize(ordinal_payload = nil)
37
- @internal_keys = []
38
37
  @internal_data = {}.with_indifferent_access
38
+ @internal_keys = []
39
39
 
40
40
  deserialize(ordinal_payload).each do |(key, value)|
41
41
  write key, value
@@ -64,6 +64,12 @@ class TurboReflex::State
64
64
  value
65
65
  end
66
66
 
67
+ def delete(*keys)
68
+ key = key_for(*keys)
69
+ internal_keys.delete key
70
+ internal_data.delete key
71
+ end
72
+
67
73
  def payload
68
74
  serialize_base64 internal_data
69
75
  end
@@ -72,6 +78,11 @@ class TurboReflex::State
72
78
  serialize internal_list
73
79
  end
74
80
 
81
+ def clear
82
+ internal_keys.clear
83
+ internal_data.clear
84
+ end
85
+
75
86
  def shrink!
76
87
  @internal_data = shrink(internal_data).with_indifferent_access
77
88
  @internal_keys = internal_keys & internal_data.keys
@@ -81,13 +92,13 @@ class TurboReflex::State
81
92
  return if internal_keys.blank?
82
93
  return if internal_data.blank?
83
94
 
84
- percentage = max_bytesize > 0 ? ordinal_payload.bytesize / max_bytesize.to_f : 0
95
+ percentage = (max_bytesize > 0) ? ordinal_payload.bytesize / max_bytesize.to_f : 0
85
96
  while percentage > 1
86
97
  keys_to_keep = internal_keys.slice((internal_keys.length - (internal_keys.length / percentage).floor)..-1)
87
98
  keys_to_remove = internal_keys - keys_to_keep
88
99
  @internal_keys = keys_to_keep
89
100
  keys_to_remove.each { |key| internal_data.delete key }
90
- percentage = max_bytesize > 0 ? ordinal_payload.bytesize / max_bytesize.to_f : 0
101
+ percentage = (max_bytesize > 0) ? ordinal_payload.bytesize / max_bytesize.to_f : 0
91
102
  end
92
103
  end
93
104
 
@@ -99,7 +99,7 @@ class TurboReflex::StateManager
99
99
  @state ||= TurboReflex::State.new
100
100
  end
101
101
 
102
- delegate :cache_key, :payload, to: :state
102
+ delegate :cache_key, to: :state
103
103
 
104
104
  def [](*keys, default: nil)
105
105
  state.read(*keys, default: default)
@@ -107,7 +107,7 @@ class TurboReflex::StateManager
107
107
 
108
108
  def []=(*keys, value)
109
109
  state_will_change! if value != self[*keys]
110
- state.write(*keys, value)
110
+ value.nil? ? state.delete(*keys) : state.write(*keys, value)
111
111
  end
112
112
 
113
113
  def provisional_state
@@ -116,12 +116,27 @@ class TurboReflex::StateManager
116
116
 
117
117
  alias_method :now, :provisional_state
118
118
 
119
- def write_cookie
120
- return unless changed?
121
- clear_provisional_state!
119
+ def clear
120
+ provisional_state.clear
121
+ state.clear
122
+ end
123
+
124
+ def payload
125
+ provisional_state.clear
126
+ state.shrink!
127
+ state.payload
128
+ end
129
+
130
+ def ordinal_payload
131
+ provisional_state.clear
122
132
  state.shrink!
123
133
  state.prune! max_bytesize: TurboReflex::StateManager.cookie_max_bytesize
124
- cookies.signed["turbo_reflex.state"] = {value: state.ordinal_payload, path: "/", expires: 1.day.from_now}
134
+ state.ordinal_payload
135
+ end
136
+
137
+ def write_cookie
138
+ return unless changed? || cookie.blank?
139
+ cookies.signed["turbo_reflex.state"] = {value: ordinal_payload, path: "/", expires: 1.day.from_now}
125
140
  changes_applied
126
141
  rescue => error
127
142
  Rails.logger.error "Failed to write the TurboReflex::State cookie! #{error.message}"
@@ -145,9 +160,4 @@ class TurboReflex::StateManager
145
160
  def cookie
146
161
  cookies.signed["turbo_reflex.state"]
147
162
  end
148
-
149
- def clear_provisional_state!
150
- provisional_state.keys.each { |key| state.write key, nil }
151
- @provisional_state = nil
152
- end
153
163
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TurboReflex
4
- VERSION = "0.0.22"
4
+ VERSION = "0.0.25"
5
5
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "turbo_reflex",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "Reflexes for Turbo Frames that help you build robust reactive applications",
5
5
  "main": "app/javascript/index.js",
6
6
  "repository": "https://github.com/hopsoft/turbo_reflex",