s2p 0.0.4 → 0.0.6
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/lib/s2p/notebook.rb +22 -3
- data/lib/s2p/search.rb +225 -0
- data/lib/s2p/version.rb +1 -1
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f98a35cf88d547dc89b457175c31b8a9932f18c44af388772992de27703b670c
|
4
|
+
data.tar.gz: 93f867ace5138682fbb13b16b189317ad71b4366298bf1bcd9ad581b613b8b8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2421346b53220c10b4ad444cd3dc28dcc2efd1484f28468c0096640b94deaaeabab0f156d082fef3d6d02d47c9b800e8d04a45ed5fe797e3ac2be40a725f67e3
|
7
|
+
data.tar.gz: 6ca78934f8dfb0630e52262f38ed540214bda5767224b0f7a9f7b0b9e42f0e74780359e124257e2fcf21ce83cc45f824b9b82280c0666fa155ad16d67e5b6798
|
data/lib/s2p/notebook.rb
CHANGED
@@ -19,13 +19,32 @@ module S2P
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
class Notebook
|
23
|
+
class Component
|
24
|
+
def self.[](*a, **o)
|
25
|
+
new(*a, **o)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
22
30
|
class NotebookRunner < Redcarpet::Render::HTML
|
31
|
+
def codespan(code)
|
32
|
+
case code
|
33
|
+
when /^~ /
|
34
|
+
eval(code[/^~ (.*)/, 1], MAIN_BINDING)
|
35
|
+
else
|
36
|
+
eval("puts(#{code})", MAIN_BINDING)
|
37
|
+
end
|
38
|
+
|
39
|
+
""
|
40
|
+
end
|
41
|
+
|
23
42
|
def block_code(text, operation)
|
24
43
|
case operation
|
25
|
-
when "ruby"
|
44
|
+
when nil, "ruby"
|
26
45
|
eval(text, MAIN_BINDING) # See end of file for this hack.
|
27
|
-
|
28
|
-
|
46
|
+
when "text"
|
47
|
+
puts(text)
|
29
48
|
end
|
30
49
|
|
31
50
|
""
|
data/lib/s2p/search.rb
ADDED
@@ -0,0 +1,225 @@
|
|
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
|
+
def cond_localized_date_range(*ranges)
|
177
|
+
ranges.each do |range|
|
178
|
+
cond_localized_date_range!(*range)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# NOTE: You can use cond_range instead of this method when working with date columns
|
183
|
+
# in the DB as they're zoneless. This method is only necessary when working with
|
184
|
+
# datetime fields, because for those you will need to translate the beginning/end of the
|
185
|
+
# range to the localized beginning/end of day times.
|
186
|
+
def cond_localized_date_range!(start_key, end_key, column)
|
187
|
+
cond(start_key) { |s,v| s.where("#{from.table_name}.#{column} >= ?", PetersenToolbox.to_local_time(Date.parse(v)).beginning_of_day) }
|
188
|
+
cond(end_key) { |s,v| s.where("#{from.table_name}.#{column} <= ?", PetersenToolbox.to_local_time(Date.parse(v)).end_of_day) }
|
189
|
+
end
|
190
|
+
|
191
|
+
attr_reader :from
|
192
|
+
end
|
193
|
+
|
194
|
+
attr_reader :scopes, :results, :params, :conditions
|
195
|
+
|
196
|
+
def initialize(params={})
|
197
|
+
@params = params.symbolize_keys
|
198
|
+
@conditions = self.class.conditions.select { |k,v| @params[k].present? }
|
199
|
+
|
200
|
+
@sort_by_key = @params.fetch(:order_by, self.class.sort_by_default_key).to_sym
|
201
|
+
@sort_order = @params.fetch(:sort_order, self.class.sort_order_defaults[@sort_by_key]).to_sym
|
202
|
+
|
203
|
+
# FIXME: Should this be a validation instead?
|
204
|
+
setup(**params) if respond_to?(:setup)
|
205
|
+
end
|
206
|
+
|
207
|
+
def save
|
208
|
+
if valid?
|
209
|
+
@results = @conditions.inject(self.class.scope) { |relation, (key, scope)|
|
210
|
+
scope.call(relation, @params[key], @params)
|
211
|
+
}
|
212
|
+
|
213
|
+
@results = sorted(@results)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
def sorted(relation)
|
218
|
+
self.class.sort_by_filters[@sort_by_key][@sort_order][relation]
|
219
|
+
end
|
220
|
+
|
221
|
+
def to_s
|
222
|
+
@params.select { |k,v| v.present? }.map { |k,v| "<strong>#{k.to_s.humanize}</strong>: #{v}" }.join(" + ")
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
data/lib/s2p/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gregory Brown
|
8
|
+
autorequire:
|
8
9
|
bindir: bin
|
9
10
|
cert_chain: []
|
10
|
-
date: 2025-02-
|
11
|
+
date: 2025-02-08 00:00:00.000000000 Z
|
11
12
|
dependencies: []
|
12
13
|
description: Experimental dev utilies from skillstopractice.com. Will eventually find
|
13
14
|
homes in their own gems if they pan out.
|
@@ -19,10 +20,13 @@ files:
|
|
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
|
25
|
+
homepage:
|
23
26
|
licenses:
|
24
27
|
- MIT
|
25
28
|
metadata: {}
|
29
|
+
post_install_message:
|
26
30
|
rdoc_options: []
|
27
31
|
require_paths:
|
28
32
|
- lib
|
@@ -37,7 +41,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
41
|
- !ruby/object:Gem::Version
|
38
42
|
version: '0'
|
39
43
|
requirements: []
|
40
|
-
rubygems_version: 3.
|
44
|
+
rubygems_version: 3.1.4
|
45
|
+
signing_key:
|
41
46
|
specification_version: 4
|
42
47
|
summary: Experimental dev utilies from skillstopractice.com
|
43
48
|
test_files: []
|