tunable 0.0.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a58a7749222087c0b2fbb540247bb6a66dcaca507b4c13ba8dfc569ab68d9df
4
- data.tar.gz: 0e6460f96413b4180fe1e1bbf196718ffff757ccf0f62b8ee14607538645b032
3
+ metadata.gz: 844872db005ce594870ba86746820e4299bcecbecaba9606a6e4e7cc2a39177b
4
+ data.tar.gz: 5187361fbcfd3af0a04f5d02ed03e62b3496edd5f962e9d57a7579af4f914529
5
5
  SHA512:
6
- metadata.gz: eebb2f72d572f22e3adad48a71df09cf93c869d886cb186cc0928c4ad2ddecca82b5e38cd6aa718bcc888bde993325ae84f74735c36dc8046668843d39a0dc2a
7
- data.tar.gz: 7065b6d54fe664411222a7df5dba4b2391d9515a1a3c07c7ca8ac2aec88e26ce472942b6c5800e1aa06b1787c89cb5bd5109aeb73616ceccf4fd584b644d970d
6
+ metadata.gz: 615660905363b14900b5d0773f5a90e836fbd3d5a83888dd52359515563d2715c41e74e8db749c6dc9af7acada93de6df0da4345d868c86df9e580a554994574
7
+ data.tar.gz: dcc95b1b796ef5c1ab334c1c3be6e02cddfd90266a61e508d970b73322bca2db0d529ea087fede88602d87d3cd48e64fd595ac4b8d3e3d2dc94af8d5e1d2b645
@@ -12,14 +12,12 @@ module Tunable
12
12
  columns_array = columns.map { |column| connection.quote_column_name(column) }
13
13
 
14
14
  values_array = values.map do |arr|
15
- row_values = []
16
- arr.each_with_index do |val, i|
17
- row_values << connection.quote(val)
18
- end
19
- row_values.join(',')
15
+ arr.map do |val, i|
16
+ val == true ? '"true"' : val == false ? '"false"' : connection.quote(val)
17
+ end.join(',')
20
18
  end
21
19
 
22
- values_array = values_array.map{ |str| str.gsub(EmojiFix::REGEX, " ") }
20
+ values_array = values_array.map { |str| str.gsub(EmojiFix::REGEX, " ") }
23
21
  insert_method = options[:method] || 'INSERT'
24
22
  sql = "#{insert_method} INTO `#{self.table_name}` (#{columns_array.join(',')}) VALUES "
25
23
 
data/lib/tunable/model.rb CHANGED
@@ -114,7 +114,7 @@ module Tunable
114
114
  raise "Invalid value: #{raw_value}. Expected #{default.class}, got #{raw_value.class}"
115
115
  end
116
116
 
117
- value = Tunable.normalize_value(raw_value)
117
+ value = Tunable.normalize_value(raw_value, default.class)
118
118
  current = Tunable.normalize_value(get_value_for(field, false)) # don't fallback to default
119
119
  # debug "Setting #{field} to #{value} (#{value.class}), current: #{current} (#{current.class})"
120
120
 
@@ -148,7 +148,6 @@ module Tunable
148
148
  end
149
149
 
150
150
  def settings_hash
151
-
152
151
  @object_hashed_settings ||= Hasher.flatten(settings.reload, :context, :key)
153
152
 
154
153
  if modified_settings.any?
@@ -167,7 +166,9 @@ module Tunable
167
166
  val = settings_context(context)[key]
168
167
 
169
168
  # if value is nil or no default is set, stop here
170
- return val if !val.nil? or self.class.default_settings(context)[key.to_sym].nil?
169
+ if !val.nil? or self.class.default_settings(context)[key.to_sym].nil?
170
+ return val
171
+ end
171
172
 
172
173
  self.class.default_settings(context)[key.to_sym]
173
174
  end
@@ -224,8 +225,8 @@ module Tunable
224
225
 
225
226
  def get_value_for(field, use_default = true)
226
227
  if instance_variable_defined?("@setting_main_#{field}")
227
- # the instance var is already normalized to 1/0 when called by the setter
228
- Tunable.getter_value(instance_variable_get("@setting_main_#{field}"))
228
+ # the instance var is already normalized when called by the setter
229
+ instance_variable_get("@setting_main_#{field}")
229
230
  else
230
231
  current = main_settings[field.to_sym]
231
232
  return current if current.present? or !use_default
@@ -2,24 +2,25 @@ module Tunable
2
2
 
3
3
  module Normalizer
4
4
 
5
- TRUTHIES = ['true', 't', 'on', 'yes', 'y', '1'].freeze
6
- FALSIES = ['false', 'f', 'off', 'no', 'n', '0'].freeze
5
+ TRUTHIES = ['true', 't', 'on', 'yes', 'y'].freeze
6
+ FALSIES = ['false', 'f', 'off', 'no', 'n'].freeze
7
7
 
8
- def normalize_value(val)
9
- return 1 if TRUTHIES.include?(val.to_s)
10
- return 0 if FALSIES.include?(val.to_s)
8
+ def normalize_value(val, type = nil)
9
+ # if [TrueClass, FalseClass].include?(type)
10
+ # return false if val.to_s == '0'
11
+ # return true if val.to_s == '1'
12
+ # end
13
+
14
+ return true if TRUTHIES.include?(val.to_s)
15
+ return false if FALSIES.include?(val.to_s)
11
16
  return Integer(val) if is_integer?(val)
12
17
  return if val.blank? # false.blank? returns true so this needs to go after the 0 line
13
18
  val
14
19
  end
15
20
 
16
- def getter_value(normalized)
17
- return normalized === 1 ? true : normalized === 0 ? false : normalized
18
- end
19
-
20
21
  # Called from Setting#normalized_value and DeviceActions#toggle_action
21
22
  def normalize_and_get(val)
22
- getter_value(normalize_value(val))
23
+ normalize_value(val)
23
24
  end
24
25
 
25
26
  def matching_type(a, b)
@@ -1,3 +1,3 @@
1
1
  module Tunable
2
- VERSION = '0.0.8'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -32,6 +32,14 @@ describe 'normalization' do
32
32
  }
33
33
  }
34
34
 
35
+ let(:sql_true) {
36
+ ActiveRecord::Base.connection.adapter_name == 'SQLite' ? 't' : 'true'
37
+ }
38
+
39
+ let(:sql_false) {
40
+ ActiveRecord::Base.connection.adapter_name == 'SQLite' ? 'f' : 'false'
41
+ }
42
+
35
43
  describe 'before saving to DB' do
36
44
 
37
45
  after do
@@ -40,19 +48,19 @@ describe 'normalization' do
40
48
 
41
49
  it 'the getter returns true for 1' do
42
50
  @model.boolean_setting = 1
43
- @model.boolean_setting.should === true
51
+ @model.boolean_setting.should === 1
44
52
  end
45
53
 
46
54
  it 'the getter returns false for 0' do
47
55
  @model.boolean_setting = '0' # as string
48
- @model.boolean_setting.should === false
56
+ @model.boolean_setting.should === 0
49
57
  end
50
58
 
51
59
  end
52
60
 
53
61
  describe 'when inserting to DB' do
54
62
 
55
- def get_value(context, key)
63
+ def get_value_from_db(context, key)
56
64
  id = @model.id
57
65
  sql = "select `value` from `settings` where `settable_id` = #{id} and `context` = '#{context}' and `key` = '#{key}';"
58
66
  res = ActiveRecord::Base.connection.execute(sql)
@@ -79,59 +87,59 @@ describe 'normalization' do
79
87
  end
80
88
 
81
89
  it 'keeps 1 as 1' do
82
- get_value(:numbers, :number_1).should == '1'
90
+ get_value_from_db(:numbers, :number_1).should == '1'
83
91
  end
84
92
 
85
93
  it 'keeps 0 as 0' do
86
- get_value(:numbers, :number_0).should == '0'
94
+ get_value_from_db(:numbers, :number_0).should == '0'
87
95
  end
88
96
 
89
97
  it 'keeps 666 as 666' do
90
- get_value(:numbers, :number_of_the_beast).should == '666'
98
+ get_value_from_db(:numbers, :number_of_the_beast).should == '666'
91
99
  end
92
100
 
93
101
  it 'keeps foo as foo' do
94
- get_value(:main, :foo).should == 'foo'
102
+ get_value_from_db(:main, :foo).should == 'foo'
95
103
  end
96
104
 
97
105
  it 'normalizes booleany true values to 1' do
98
- get_value(:main, :truthy).should == '1'
106
+ get_value_from_db(:main, :truthy).should == sql_true
99
107
  end
100
108
 
101
109
  it 'normalizes booleany false values to 0' do
102
- get_value(:main, :falsy).should == '0'
110
+ get_value_from_db(:main, :falsy).should == sql_false
103
111
  end
104
112
 
105
- it 'normalizes t (from true) to 1' do
106
- get_value(:main, :t).should == '1'
113
+ it 'normalizes t (from true) to true' do
114
+ get_value_from_db(:main, :t).should == sql_true
107
115
  end
108
116
 
109
117
  it 'normalizes f (from false) to 0' do
110
- get_value(:main, :f).should == '0'
118
+ get_value_from_db(:main, :f).should == sql_false
111
119
  end
112
120
 
113
121
  it 'normalizes on to 1' do
114
- get_value(:onoffs, :on).should == '1'
122
+ get_value_from_db(:onoffs, :on).should == sql_true
115
123
  end
116
124
 
117
125
  it 'normalizes off to 0' do
118
- get_value(:onoffs, :off).should == '0'
126
+ get_value_from_db(:onoffs, :off).should == sql_false
119
127
  end
120
128
 
121
129
  it 'normalizes y to 1' do
122
- get_value(:yesnoes, :y).should == '1'
130
+ get_value_from_db(:yesnoes, :y).should == sql_true
123
131
  end
124
132
 
125
133
  it 'normalizes n to 0' do
126
- get_value(:yesnoes, :n).should == '0'
134
+ get_value_from_db(:yesnoes, :n).should == sql_false
127
135
  end
128
136
 
129
137
  it 'normalizes yes to 1' do
130
- get_value(:yesnoes, :yes).should == '1'
138
+ get_value_from_db(:yesnoes, :yes).should == sql_true
131
139
  end
132
140
 
133
141
  it 'normalizes no to 0' do
134
- get_value(:yesnoes, :no).should == '0'
142
+ get_value_from_db(:yesnoes, :no).should == sql_false
135
143
  end
136
144
 
137
145
  end
@@ -140,47 +148,49 @@ describe 'normalization' do
140
148
 
141
149
  it 'keeps 1 as 1' do
142
150
  @model.update_attribute(:number_setting, 1)
143
- get_value(:main, :number_setting).should == '1'
151
+ get_value_from_db(:main, :number_setting).should == '1'
144
152
  end
145
153
 
146
154
  it 'keeps 0 as 0' do
147
155
  @model.update_attribute(:number_setting, '0')
148
- get_value(:main, :number_setting).should == '0'
156
+ get_value_from_db(:main, :number_setting).should == '0'
149
157
  end
150
158
 
151
159
  it 'keeps foo as foo' do
152
160
  @model.update_attribute(:other_setting, 'foo')
153
- get_value(:main, :other_setting).should == 'foo'
161
+ get_value_from_db(:main, :other_setting).should == 'foo'
154
162
  end
155
163
 
156
164
  it 'normalizes booleany true values to 1' do
157
- @model.update_attribute(:boolean_setting, true)
158
- get_value(:main, :boolean_setting).should == '1'
165
+ # @model.update_attribute(:boolean_setting, true)
166
+ @model.boolean_setting = true
167
+ @model.save
168
+ get_value_from_db(:main, :boolean_setting).should == 'true'
159
169
  end
160
170
 
161
171
  it 'normalizes booleany false values to 0' do
162
- @model.update_attribute(:boolean_setting, false)
163
- get_value(:main, :boolean_setting).should == '0'
172
+ @model.update(boolean_setting: false)
173
+ get_value_from_db(:main, :boolean_setting).should == 'false'
164
174
  end
165
175
 
166
176
  it 'normalizes on to 1' do
167
177
  @model.update_attribute(:on_off_setting, 'on')
168
- get_value(:main, :on_off_setting).should == '1'
178
+ get_value_from_db(:main, :on_off_setting).should == 'true'
169
179
  end
170
180
 
171
181
  it 'normalizes off to 0' do
172
182
  @model.update_attribute(:on_off_setting, 'off')
173
- get_value(:main, :on_off_setting).should == '0'
183
+ get_value_from_db(:main, :on_off_setting).should == 'false'
174
184
  end
175
185
 
176
186
  it 'normalizes on to 1' do
177
187
  @model.update_attribute(:y_n_setting, 'y')
178
- get_value(:main, :y_n_setting).should == '1'
188
+ get_value_from_db(:main, :y_n_setting).should == 'true'
179
189
  end
180
190
 
181
191
  it 'normalizes off to 0' do
182
192
  @model.update_attribute(:y_n_setting, 'n')
183
- get_value(:main, :y_n_setting).should == '0'
193
+ get_value_from_db(:main, :y_n_setting).should == 'false'
184
194
  end
185
195
 
186
196
  end
@@ -196,59 +206,59 @@ describe 'normalization' do
196
206
  end
197
207
 
198
208
  it 'keeps 1 as 1' do
199
- get_value(:numbers, :number_1).should == '1'
209
+ get_value_from_db(:numbers, :number_1).should == '1'
200
210
  end
201
211
 
202
212
  it 'keeps 0 as 0' do
203
- get_value(:numbers, :number_0).should == '0'
213
+ get_value_from_db(:numbers, :number_0).should == '0'
204
214
  end
205
215
 
206
216
  it 'keeps 666 as 666' do
207
- get_value(:numbers, :number_of_the_beast).should == '666'
217
+ get_value_from_db(:numbers, :number_of_the_beast).should == '666'
208
218
  end
209
219
 
210
220
  it 'keeps foo as foo' do
211
- get_value(:main, :foo).should == 'foo'
221
+ get_value_from_db(:main, :foo).should == 'foo'
212
222
  end
213
223
 
214
224
  it 'normalizes booleany true values to 1' do
215
- get_value(:main, :truthy).should == '1'
225
+ get_value_from_db(:main, :truthy).should == 'true'
216
226
  end
217
227
 
218
228
  it 'normalizes booleany false values to 0' do
219
- get_value(:main, :falsy).should == '0'
229
+ get_value_from_db(:main, :falsy).should == 'false'
220
230
  end
221
231
 
222
232
  it 'normalizes t (from true) to 1' do
223
- get_value(:main, :t).should == '1'
233
+ get_value_from_db(:main, :t).should == 'true'
224
234
  end
225
235
 
226
236
  it 'normalizes f (from false) to 0' do
227
- get_value(:main, :f).should == '0'
237
+ get_value_from_db(:main, :f).should == 'false'
228
238
  end
229
239
 
230
240
  it 'normalizes on to 1' do
231
- get_value(:onoffs, :on).should == '1'
241
+ get_value_from_db(:onoffs, :on).should == 'true'
232
242
  end
233
243
 
234
244
  it 'normalizes off to 0' do
235
- get_value(:onoffs, :off).should == '0'
245
+ get_value_from_db(:onoffs, :off).should == 'false'
236
246
  end
237
247
 
238
248
  it 'normalizes y to 1' do
239
- get_value(:yesnoes, :y).should == '1'
249
+ get_value_from_db(:yesnoes, :y).should == 'true'
240
250
  end
241
251
 
242
252
  it 'normalizes n to 0' do
243
- get_value(:yesnoes, :n).should == '0'
253
+ get_value_from_db(:yesnoes, :n).should == 'false'
244
254
  end
245
255
 
246
256
  it 'normalizes yes to 1' do
247
- get_value(:yesnoes, :yes).should == '1'
257
+ get_value_from_db(:yesnoes, :yes).should == 'true'
248
258
  end
249
259
 
250
260
  it 'normalizes no to 0' do
251
- get_value(:yesnoes, :no).should == '0'
261
+ get_value_from_db(:yesnoes, :no).should == 'false'
252
262
  end
253
263
 
254
264
  end
@@ -274,66 +284,66 @@ describe 'normalization' do
274
284
  end
275
285
  end
276
286
 
277
- def get_value_from_model(context, key)
287
+ def get_value_from_db_from_model(context, key)
278
288
  settings = @model.reload.settings_hash
279
289
  ctx = "raw_#{context}".to_sym
280
290
  settings[ctx][key]
281
291
  end
282
292
 
283
293
  it 'normalizes 1 to true' do
284
- get_value_from_model(:numbers, :number_1).should === true
294
+ get_value_from_db_from_model(:numbers, :number_1).should === 1
285
295
  end
286
296
 
287
297
  it 'normalizes 0 to false' do
288
- get_value_from_model(:numbers, :number_0).should === false
298
+ get_value_from_db_from_model(:numbers, :number_0).should === 0
289
299
  end
290
300
 
291
301
  it 'keeps 666 as 666' do
292
- get_value_from_model(:numbers, :number_of_the_beast).should == 666
302
+ get_value_from_db_from_model(:numbers, :number_of_the_beast).should == 666
293
303
  end
294
304
 
295
305
  it 'keeps foo as foo' do
296
- get_value_from_model(:main, :foo).should == 'foo'
306
+ get_value_from_db_from_model(:main, :foo).should == 'foo'
297
307
  end
298
308
 
299
309
  it 'normalizes booleany true values to true' do
300
- get_value_from_model(:main, :truthy).should === true
310
+ get_value_from_db_from_model(:main, :truthy).should === true
301
311
  end
302
312
 
303
313
  it 'normalizes booleany false values to false' do
304
- get_value_from_model(:main, :falsy).should === false
314
+ get_value_from_db_from_model(:main, :falsy).should === false
305
315
  end
306
316
 
307
317
  it 'normalizes t (from true) to true' do
308
- get_value_from_model(:main, :t).should === true
318
+ get_value_from_db_from_model(:main, :t).should === true
309
319
  end
310
320
 
311
321
  it 'normalizes f (from false) to false' do
312
- get_value_from_model(:main, :f).should === false
322
+ get_value_from_db_from_model(:main, :f).should === false
313
323
  end
314
324
 
315
325
  it 'normalizes on to true' do
316
- get_value_from_model(:onoffs, :on).should === true
326
+ get_value_from_db_from_model(:onoffs, :on).should === true
317
327
  end
318
328
 
319
329
  it 'normalizes off to false' do
320
- get_value_from_model(:onoffs, :off).should === false
330
+ get_value_from_db_from_model(:onoffs, :off).should === false
321
331
  end
322
332
 
323
333
  it 'normalizes y to true' do
324
- get_value_from_model(:yesnoes, :y).should === true
334
+ get_value_from_db_from_model(:yesnoes, :y).should === true
325
335
  end
326
336
 
327
337
  it 'normalizes n to false' do
328
- get_value_from_model(:yesnoes, :n).should === false
338
+ get_value_from_db_from_model(:yesnoes, :n).should === false
329
339
  end
330
340
 
331
341
  it 'normalizes yes to true' do
332
- get_value_from_model(:yesnoes, :yes).should === true
342
+ get_value_from_db_from_model(:yesnoes, :yes).should === true
333
343
  end
334
344
 
335
345
  it 'normalizes no to false' do
336
- get_value_from_model(:yesnoes, :no).should === false
346
+ get_value_from_db_from_model(:yesnoes, :no).should === false
337
347
  end
338
348
 
339
349
  end
@@ -125,8 +125,8 @@ describe 'setting on/off?' do
125
125
  end
126
126
 
127
127
  describe '1' do
128
- it 'returns true' do
129
- @model.setting_on?(:numbers, :number_1).should === true
128
+ it 'returns false' do
129
+ @model.setting_on?(:numbers, :number_1).should === false
130
130
  end
131
131
  end
132
132
 
@@ -195,8 +195,8 @@ describe 'setting on/off?' do
195
195
  end
196
196
 
197
197
  describe '0' do
198
- it 'returns true' do
199
- @model.setting_off?(:numbers, :number_0).should === true
198
+ it 'returns false' do
199
+ @model.setting_off?(:numbers, :number_0).should === false
200
200
  end
201
201
  end
202
202
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tunable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomás Pollak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-07-05 00:00:00.000000000 Z
11
+ date: 2025-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord