simple_query 0.2.0 → 0.3.1
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/README.md +64 -0
- data/lib/simple_query/builder.rb +19 -0
- data/lib/simple_query/clauses/where_clause.rb +3 -0
- data/lib/simple_query/version.rb +1 -1
- data/lib/simple_query.rb +21 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa6c4c630a5cd486970ce4ca43b444f082050924305cb20e835a861b32676275
|
4
|
+
data.tar.gz: de8f1cc3758f4a65edbc4a1d4bbe1c5edde866d6016332d5b415c35d97423e7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9fb69f0d5505926239e5dcc396dedb098f974b38a0a1d135ddeb50927de9b88b8dc0f0fb850a2ab2fe6b9e7ba9a4955738c36d4a1c0f974e507740fa00edb15
|
7
|
+
data.tar.gz: e8acc149f25a8b898a04ec7ca5203f98898d88bff334700ff5e4b6e0bb538b56fb47edd0b92cb66fbd86ac89c3f98d7c48930f25e9870bb34208b0bbd2735c4f
|
data/README.md
CHANGED
@@ -82,6 +82,27 @@ User.simple_query
|
|
82
82
|
.lazy_execute
|
83
83
|
```
|
84
84
|
|
85
|
+
Placeholder-Based Conditions
|
86
|
+
|
87
|
+
SimpleQuery now supports **ActiveRecord-style placeholders**, letting you pass arrays with `?` or `:named` placeholders to your `.where` clauses:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# Positional placeholders:
|
91
|
+
User.simple_query
|
92
|
+
.where(["name LIKE ?", "%Alice%"])
|
93
|
+
.execute
|
94
|
+
|
95
|
+
# Named placeholders:
|
96
|
+
User.simple_query
|
97
|
+
.where(["email = :email", { email: "alice@example.com" }])
|
98
|
+
.execute
|
99
|
+
|
100
|
+
# Multiple placeholders in one condition:
|
101
|
+
User.simple_query
|
102
|
+
.where(["age >= :min_age AND age <= :max_age", { min_age: 18, max_age: 35 }])
|
103
|
+
.execute
|
104
|
+
```
|
105
|
+
|
85
106
|
## Custom Read Models
|
86
107
|
By default, SimpleQuery returns results as `Struct` objects for maximum speed. However, you can also define a lightweight model class for more explicit attribute handling or custom logic.
|
87
108
|
|
@@ -108,6 +129,48 @@ end
|
|
108
129
|
```
|
109
130
|
This custom read model approach provides more clarity or domain-specific logic while still being faster than typical ActiveRecord instantiation.
|
110
131
|
|
132
|
+
## Named Scopes
|
133
|
+
SimpleQuery now supports named scopes, allowing you to reuse common query logic in a style similar to ActiveRecord’s built-in scopes. To define a scope, use the simple_scope class method in your model:
|
134
|
+
```ruby
|
135
|
+
class User < ActiveRecord::Base
|
136
|
+
include SimpleQuery
|
137
|
+
|
138
|
+
simple_scope :active do
|
139
|
+
where(active: true)
|
140
|
+
end
|
141
|
+
|
142
|
+
simple_scope :admins do
|
143
|
+
where(admin: true)
|
144
|
+
end
|
145
|
+
|
146
|
+
simple_scope :by_name do |name|
|
147
|
+
where(name: name)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
```
|
151
|
+
You can then chain these scopes seamlessly with the normal SimpleQuery DSL:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
# Parameterless scopes
|
155
|
+
results = User.simple_query.active.admins.execute
|
156
|
+
|
157
|
+
# Parameterized scope
|
158
|
+
results = User.simple_query.by_name("Jane Doe").execute
|
159
|
+
|
160
|
+
# Mixing scopes with other DSL calls
|
161
|
+
results = User.simple_query
|
162
|
+
.by_name("John")
|
163
|
+
.active
|
164
|
+
.select(:id, :name)
|
165
|
+
.order(name: :asc)
|
166
|
+
.execute
|
167
|
+
```
|
168
|
+
### How It Works
|
169
|
+
|
170
|
+
Each scope block (e.g. by_name) is evaluated in the context of the SimpleQuery builder, so you can call any DSL method (where, order, etc.) inside it.
|
171
|
+
Parameterized scopes accept arguments — passed directly to the block (e.g. |name| above).
|
172
|
+
Scopes return self, so you can chain multiple scopes or mix them with standard query methods.
|
173
|
+
|
111
174
|
## Features
|
112
175
|
|
113
176
|
- Efficient query building
|
@@ -120,6 +183,7 @@ This custom read model approach provides more clarity or domain-specific logic w
|
|
120
183
|
- Having and Grouping
|
121
184
|
- Subqueries
|
122
185
|
- Custom Read models
|
186
|
+
- Named Scopes
|
123
187
|
|
124
188
|
## Performance
|
125
189
|
|
data/lib/simple_query/builder.rb
CHANGED
@@ -220,5 +220,24 @@ module SimpleQuery
|
|
220
220
|
raise ArgumentError, "Unsupported select field type: #{field.class}"
|
221
221
|
end
|
222
222
|
end
|
223
|
+
|
224
|
+
def method_missing(method_name, *args, &block)
|
225
|
+
if (scope_block = find_scope(method_name))
|
226
|
+
instance_exec(*args, &scope_block)
|
227
|
+
self
|
228
|
+
else
|
229
|
+
super
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
def respond_to_missing?(method_name, include_private = false)
|
234
|
+
!!find_scope(method_name) || super
|
235
|
+
end
|
236
|
+
|
237
|
+
def find_scope(method_name)
|
238
|
+
return unless model.respond_to?(:_simple_scopes)
|
239
|
+
|
240
|
+
model._simple_scopes[method_name.to_sym]
|
241
|
+
end
|
223
242
|
end
|
224
243
|
end
|
@@ -30,6 +30,9 @@ module SimpleQuery
|
|
30
30
|
condition.map { |field, value| @table[field].eq(value) }
|
31
31
|
when Arel::Nodes::Node
|
32
32
|
[condition]
|
33
|
+
when Array
|
34
|
+
sanitized_sql = ActiveRecord::Base.send(:sanitize_sql_array, condition)
|
35
|
+
[Arel.sql(sanitized_sql)]
|
33
36
|
else
|
34
37
|
[Arel.sql(condition.to_s)]
|
35
38
|
end
|
data/lib/simple_query/version.rb
CHANGED
data/lib/simple_query.rb
CHANGED
@@ -36,6 +36,27 @@ module SimpleQuery
|
|
36
36
|
ActiveRecord::Base.include(SimpleQuery)
|
37
37
|
end
|
38
38
|
|
39
|
+
class_methods do
|
40
|
+
def _simple_scopes
|
41
|
+
@_simple_scopes ||= {}
|
42
|
+
end
|
43
|
+
|
44
|
+
# A reusable scope that can be applied to a SimpleQuery::Builder instance
|
45
|
+
# Example:
|
46
|
+
# simple_scope :active do
|
47
|
+
# where(active: true)
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# Parameterized scope:
|
51
|
+
# simple_scope :by_name do |name|
|
52
|
+
# where(name: name)
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
def simple_scope(name, &block)
|
56
|
+
_simple_scopes[name.to_sym] = block
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
39
60
|
included do
|
40
61
|
def self.simple_query
|
41
62
|
Builder.new(self)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Kholodniak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-02-
|
11
|
+
date: 2025-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -108,13 +108,13 @@ files:
|
|
108
108
|
- lib/simple_query/clauses/where_clause.rb
|
109
109
|
- lib/simple_query/read_model.rb
|
110
110
|
- lib/simple_query/version.rb
|
111
|
-
homepage: https://
|
111
|
+
homepage: https://github.com/kholdrex/simple_query
|
112
112
|
licenses:
|
113
113
|
- MIT
|
114
114
|
metadata:
|
115
|
-
homepage_uri: https://
|
115
|
+
homepage_uri: https://github.com/kholdrex/simple_query
|
116
116
|
source_code_uri: https://github.com/kholdrex/simple_query
|
117
|
-
changelog_uri: https://github.com/kholdrex/simple_query/blob/
|
117
|
+
changelog_uri: https://github.com/kholdrex/simple_query/blob/master/CHANGELOG.md
|
118
118
|
post_install_message:
|
119
119
|
rdoc_options: []
|
120
120
|
require_paths:
|