simple_query 0.2.0 → 0.3.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 +4 -4
- data/README.md +43 -0
- data/lib/simple_query/builder.rb +19 -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: f15e71d4ac10ed8d359f177e46f36f12b9721da6f9f51c75d8a0571cf245aac0
|
4
|
+
data.tar.gz: 8090bd8f93899061f15a048a7c57a73e48e8805e7fee3807bb39ba91e0012b1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 903a9d4754ed89bf718918de17d94bf09969dc5a725d933b68c101a71737d397e90a89ac3ab141b4e18f855b39a81d62a27982c207dca20e7df7f0c6d8571746
|
7
|
+
data.tar.gz: 2965ff1462a4036112ba0bfe974087c72ba5e8f9b8db15d39a5f5879394dbe074cf9942682f9b50732c61f0d25749c514d38f1e11e2c8e0414308ee676f14ba1
|
data/README.md
CHANGED
@@ -108,6 +108,48 @@ end
|
|
108
108
|
```
|
109
109
|
This custom read model approach provides more clarity or domain-specific logic while still being faster than typical ActiveRecord instantiation.
|
110
110
|
|
111
|
+
## Named Scopes
|
112
|
+
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:
|
113
|
+
```ruby
|
114
|
+
class User < ActiveRecord::Base
|
115
|
+
include SimpleQuery
|
116
|
+
|
117
|
+
simple_scope :active do
|
118
|
+
where(active: true)
|
119
|
+
end
|
120
|
+
|
121
|
+
simple_scope :admins do
|
122
|
+
where(admin: true)
|
123
|
+
end
|
124
|
+
|
125
|
+
simple_scope :by_name do |name|
|
126
|
+
where(name: name)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
130
|
+
You can then chain these scopes seamlessly with the normal SimpleQuery DSL:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
# Parameterless scopes
|
134
|
+
results = User.simple_query.active.admins.execute
|
135
|
+
|
136
|
+
# Parameterized scope
|
137
|
+
results = User.simple_query.by_name("Jane Doe").execute
|
138
|
+
|
139
|
+
# Mixing scopes with other DSL calls
|
140
|
+
results = User.simple_query
|
141
|
+
.by_name("John")
|
142
|
+
.active
|
143
|
+
.select(:id, :name)
|
144
|
+
.order(name: :asc)
|
145
|
+
.execute
|
146
|
+
```
|
147
|
+
### How It Works
|
148
|
+
|
149
|
+
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.
|
150
|
+
Parameterized scopes accept arguments — passed directly to the block (e.g. |name| above).
|
151
|
+
Scopes return self, so you can chain multiple scopes or mix them with standard query methods.
|
152
|
+
|
111
153
|
## Features
|
112
154
|
|
113
155
|
- Efficient query building
|
@@ -120,6 +162,7 @@ This custom read model approach provides more clarity or domain-specific logic w
|
|
120
162
|
- Having and Grouping
|
121
163
|
- Subqueries
|
122
164
|
- Custom Read models
|
165
|
+
- Named Scopes
|
123
166
|
|
124
167
|
## Performance
|
125
168
|
|
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
|
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.0
|
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-26 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:
|