s2p 0.0.5 → 0.0.7
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 +4 -4
- data/LICENSE +21 -0
- data/lib/s2p/component.rb +20 -0
- data/lib/s2p/search.rb +210 -0
- data/lib/s2p/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f70e0e13983e0319e806abfb6a3233c9662974e1d4ed4aa5c3399639b52ed217
|
4
|
+
data.tar.gz: 8913f389f10b5cb25889e39ecb61134db36ccd578666137208228391e3c2bcd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e487b0839f7d7dd5799b775013968b6b48bcc6c86a22443abe579cc33b3b13874f7f5b09d41214031031e1bd5cc6c22d7e3ac76cfc4468861c2fd2294dd6fed
|
7
|
+
data.tar.gz: 448b69b923d8e4df14c7c092c3e6773b4e86653871d47b4020688a74e9a244d5e0e22338f364344dc516e98dc4849ecfd6f7cd01f5b5f9de11f6ed2badac926b
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Gregory Brown (skillstopractice.com)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/lib/s2p/component.rb
CHANGED
@@ -58,4 +58,24 @@ module S2P
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
61
|
+
|
62
|
+
module ComponentDebugger
|
63
|
+
def to_html
|
64
|
+
if self.class.const_defined?(:DEBUG_TEMPLATE)
|
65
|
+
t(self.class::DEBUG_TEMPLATE, c: self, x: self.x)
|
66
|
+
else
|
67
|
+
t(DEBUG_TEMPLATE, c: self, x: self.x)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
DEBUG_TEMPLATE = <<-ERB
|
72
|
+
<div class="border border-solid p-3 border-danger">
|
73
|
+
<strong><pre><%= c.class %></pre></strong>
|
74
|
+
|
75
|
+
<% if c.respond_to?(:debugging_info) %>
|
76
|
+
<pre><%= c.debugging_info.pretty_inspect %></pre>
|
77
|
+
<% end %>
|
78
|
+
</div>
|
79
|
+
ERB
|
80
|
+
end
|
61
81
|
end
|
data/lib/s2p/search.rb
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
module S2P
|
2
|
+
class Search
|
3
|
+
include ActiveModel::Validations
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def setup(&conds)
|
7
|
+
@before_callback = conds
|
8
|
+
end
|
9
|
+
|
10
|
+
def scope
|
11
|
+
@before_callback.present? ? @before_callback[from] : from
|
12
|
+
end
|
13
|
+
|
14
|
+
def search_keys
|
15
|
+
[:page, :sort_order, :order_by] + conditions.keys + (@additional_search_params || [])
|
16
|
+
end
|
17
|
+
|
18
|
+
def additional_search_params(*keys)
|
19
|
+
@additional_search_params = keys
|
20
|
+
end
|
21
|
+
|
22
|
+
def create(params={})
|
23
|
+
new(params).tap(&:save)
|
24
|
+
end
|
25
|
+
|
26
|
+
def searches(from)
|
27
|
+
@from ||= from
|
28
|
+
end
|
29
|
+
|
30
|
+
def sortable_by(*names, table_name: self.from.table_name)
|
31
|
+
names.each do |name|
|
32
|
+
sortable_by!(name,
|
33
|
+
asc: ->(s) { s.reorder("#{table_name}.#{name} asc") },
|
34
|
+
desc: ->(s) { s.reorder("#{table_name}.#{name} desc") }
|
35
|
+
)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def sortable_by_alias(sort_alias, sort_condition)
|
40
|
+
sortable_by!(sort_alias,
|
41
|
+
asc: ->(s) { s.reorder("#{sort_condition} asc") },
|
42
|
+
desc: ->(s) { s.reorder("#{sort_condition} desc") }
|
43
|
+
)
|
44
|
+
end
|
45
|
+
|
46
|
+
def sortable_by!(name, asc:, desc:)
|
47
|
+
name = name.to_sym
|
48
|
+
|
49
|
+
case asc
|
50
|
+
when String
|
51
|
+
sort_by_filters[name][:asc] = ->(s) { s.reorder(asc) }
|
52
|
+
when Proc
|
53
|
+
sort_by_filters[name][:asc] = asc
|
54
|
+
end
|
55
|
+
|
56
|
+
case desc
|
57
|
+
when String
|
58
|
+
sort_by_filters[name][:desc] = ->(s) { s.reorder(desc) }
|
59
|
+
when Proc
|
60
|
+
sort_by_filters[name][:desc] = desc
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# FIXME: Consider introducing a SortFilter object of some sort?
|
65
|
+
def sort_by_filters
|
66
|
+
@sort_by_filters ||= Hash.new { |h,k| h[k] = {} }
|
67
|
+
@sort_by_filters[:id][:asc] ||= ->(s) { s.reorder(id: :asc) }
|
68
|
+
@sort_by_filters[:id][:desc] ||= ->(s) { s.reorder(id: :desc) }
|
69
|
+
|
70
|
+
@sort_by_filters
|
71
|
+
end
|
72
|
+
|
73
|
+
def sort_by_default(key)
|
74
|
+
@sort_by_default_key = key
|
75
|
+
end
|
76
|
+
|
77
|
+
def sort_by_default_key
|
78
|
+
@sort_by_default_key || :id
|
79
|
+
end
|
80
|
+
|
81
|
+
def default_sort_order(params)
|
82
|
+
sort_order_defaults.update(params).symbolize_keys!
|
83
|
+
end
|
84
|
+
|
85
|
+
def sort_order_defaults
|
86
|
+
@sort_order_defaults ||= Hash.new { |h,k| h[k] = :asc }
|
87
|
+
end
|
88
|
+
|
89
|
+
def cond(key, &scope)
|
90
|
+
conditions[key] = scope
|
91
|
+
end
|
92
|
+
|
93
|
+
def conditions
|
94
|
+
@conditions ||= {}
|
95
|
+
end
|
96
|
+
|
97
|
+
def cond_eq(*keys)
|
98
|
+
keys.each do |key|
|
99
|
+
cond_eq!(key, key)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def cond_eq!(key, column)
|
104
|
+
cond(key) { |s,v| s.where(column => v) }
|
105
|
+
end
|
106
|
+
|
107
|
+
def cond_start(*keys)
|
108
|
+
keys.each do |key|
|
109
|
+
cond_start!(key, "#{from.table_name}.#{key}")
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def cond_start!(key, column)
|
114
|
+
cond(key) { |s,v| s.where("#{column} like ?", "#{v}%") }
|
115
|
+
end
|
116
|
+
|
117
|
+
def cond_like(*keys)
|
118
|
+
keys.each do |key|
|
119
|
+
cond_like!(key, "#{from.table_name}.#{key}")
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def cond_like!(key, column)
|
124
|
+
cond(key) { |s,v| s.where("#{column} like ?", "%#{v}%") }
|
125
|
+
end
|
126
|
+
|
127
|
+
def cond_lt(*keys)
|
128
|
+
keys.each do |key|
|
129
|
+
cond(key) { |s,v| s.where("#{from.table_name}.#{key} < ?", v) }
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def cond_lte(*keys)
|
134
|
+
keys.each do |key|
|
135
|
+
cond_lte!(key, "#{from.table_name}.#{key}")
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def cond_lte!(key, column)
|
140
|
+
cond(key) { |s,v| s.where("#{column} <= ?", v) }
|
141
|
+
end
|
142
|
+
|
143
|
+
def cond_gte(*keys)
|
144
|
+
keys.each do |key|
|
145
|
+
cond_gte!(key, "#{from.table_name}.#{key}")
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def cond_gte!(key, column)
|
150
|
+
cond(key) { |s,v| s.where("#{column} >= ?", v) }
|
151
|
+
end
|
152
|
+
|
153
|
+
def cond_gt(*keys)
|
154
|
+
keys.each do |key|
|
155
|
+
cond(key) { |s,v| s.where("#{from.table_name}.#{key} > ?", v) }
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def cond_range(*ranges)
|
160
|
+
ranges.each do |start_key, end_key, column|
|
161
|
+
cond_range!(start_key, end_key, column)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def cond_range!(start_key, end_key, column)
|
166
|
+
cond_gte!(start_key, column)
|
167
|
+
cond_lte!(end_key, column)
|
168
|
+
end
|
169
|
+
|
170
|
+
def cond_cont(*keys)
|
171
|
+
keys.each do |key|
|
172
|
+
cond(key) { |s,v| s.where("#{from.table_name}.#{key} like ?", "%#{v}%") }
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
attr_reader :from
|
177
|
+
end
|
178
|
+
|
179
|
+
attr_reader :scopes, :results, :params, :conditions
|
180
|
+
|
181
|
+
def initialize(params={})
|
182
|
+
@params = params.symbolize_keys
|
183
|
+
@conditions = self.class.conditions.select { |k,v| @params[k].present? }
|
184
|
+
|
185
|
+
@sort_by_key = @params.fetch(:order_by, self.class.sort_by_default_key).to_sym
|
186
|
+
@sort_order = @params.fetch(:sort_order, self.class.sort_order_defaults[@sort_by_key]).to_sym
|
187
|
+
|
188
|
+
# FIXME: Should this be a validation instead?
|
189
|
+
setup(**params) if respond_to?(:setup)
|
190
|
+
end
|
191
|
+
|
192
|
+
def save
|
193
|
+
if valid?
|
194
|
+
@results = @conditions.inject(self.class.scope) { |relation, (key, scope)|
|
195
|
+
scope.call(relation, @params[key], @params)
|
196
|
+
}
|
197
|
+
|
198
|
+
@results = sorted(@results)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
def sorted(relation)
|
203
|
+
self.class.sort_by_filters[@sort_by_key][@sort_order][relation]
|
204
|
+
end
|
205
|
+
|
206
|
+
def to_s
|
207
|
+
@params.select { |k,v| v.present? }.map { |k,v| "<strong>#{k.to_s.humanize}</strong>: #{v}" }.join(" + ")
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
data/lib/s2p/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: s2p
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregory Brown
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-04-04 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
12
|
description: Experimental dev utilies from skillstopractice.com. Will eventually find
|
13
13
|
homes in their own gems if they pan out.
|
@@ -16,9 +16,11 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- LICENSE
|
19
20
|
- lib/s2p/component.rb
|
20
21
|
- lib/s2p/contractor.rb
|
21
22
|
- lib/s2p/notebook.rb
|
23
|
+
- lib/s2p/search.rb
|
22
24
|
- lib/s2p/version.rb
|
23
25
|
licenses:
|
24
26
|
- MIT
|