in_time_scope 0.1.0 → 0.1.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 +22 -6
- data/lib/in_time_scope/version.rb +1 -1
- data/lib/in_time_scope.rb +14 -8
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bf64f421102e871af6a7dc8cb3299d027baab7a43a8db7a8cac7d0652356628a
|
|
4
|
+
data.tar.gz: b21727c22eda667f0aba90e84a4a1ede84191a612c105a4fd4d1cf8713ec3718
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e2df0f634c6ffd4e2929ff40775916fdedc4a52bc60399a322eeb0fbb40371adb37a62f27ce7742dae5c245c870ebf9376c08dd20bea5ea3dfa2a0a1786e470d
|
|
7
|
+
data.tar.gz: '08314bae4f6641237320af99c52ae039a6f65888113ac74e32ba07434893ed8a97124209d60baacb02487c693fe37c9da458a953d83105c58998c8517931d092'
|
data/README.md
CHANGED
|
@@ -87,11 +87,12 @@ Use these options in `in_time_scope` to customize column behavior.
|
|
|
87
87
|
|
|
88
88
|
| Option | Applies to | Type | Default | Description | Example |
|
|
89
89
|
| --- | --- | --- | --- | --- | --- |
|
|
90
|
-
| `:scope_name` (1st arg) | in_time | `Symbol` | `:in_time` | Creates a named scope like `
|
|
90
|
+
| `:scope_name` (1st arg) | in_time | `Symbol` | `:in_time` | Creates a named scope like `in_time_published` | `in_time_scope :published` |
|
|
91
91
|
| `start_at: { column: ... }` | start_at | `Symbol` / `nil` | `:start_at` (or `:"<scope>_start_at"` when `:scope_name` is set) | Use a custom column name; set `nil` to disable `start_at` | `start_at: { column: :available_at }` |
|
|
92
92
|
| `end_at: { column: ... }` | end_at | `Symbol` / `nil` | `:end_at` (or `:"<scope>_end_at"` when `:scope_name` is set) | Use a custom column name; set `nil` to disable `end_at` | `end_at: { column: nil }` |
|
|
93
93
|
| `start_at: { null: ... }` | start_at | `true/false` | auto (schema) | Force NULL-aware vs NOT NULL behavior | `start_at: { null: false }` |
|
|
94
94
|
| `end_at: { null: ... }` | end_at | `true/false` | auto (schema) | Force NULL-aware vs NOT NULL behavior | `end_at: { null: true }` |
|
|
95
|
+
| `prefix: true` | scope_name | `true/false` | `false` | Use prefix style method name like `published_in_time` instead of `in_time_published` | `in_time_scope :published, prefix: true` |
|
|
95
96
|
|
|
96
97
|
### Alternative: Start-Only History (No `end_at`)
|
|
97
98
|
Use this when periods never overlap and you want exactly one "current" row.
|
|
@@ -175,8 +176,8 @@ Customize which columns are used and define more than one time window per model.
|
|
|
175
176
|
create_table :events do |t|
|
|
176
177
|
t.datetime :available_at, null: true
|
|
177
178
|
t.datetime :expired_at, null: true
|
|
178
|
-
t.datetime :
|
|
179
|
-
t.datetime :
|
|
179
|
+
t.datetime :published_start_at, null: false
|
|
180
|
+
t.datetime :published_end_at, null: false
|
|
180
181
|
|
|
181
182
|
t.timestamps
|
|
182
183
|
end
|
|
@@ -187,15 +188,30 @@ class Event < ActiveRecord::Base
|
|
|
187
188
|
# Use different column names
|
|
188
189
|
in_time_scope start_at: { column: :available_at }, end_at: { column: :expired_at }
|
|
189
190
|
|
|
190
|
-
# Define an additional scope
|
|
191
|
-
in_time_scope :published
|
|
191
|
+
# Define an additional scope - uses published_start_at / published_end_at by default
|
|
192
|
+
in_time_scope :published
|
|
192
193
|
end
|
|
193
194
|
|
|
194
195
|
Event.in_time
|
|
195
196
|
# => uses available_at / expired_at
|
|
196
197
|
|
|
198
|
+
Event.in_time_published
|
|
199
|
+
# => uses published_start_at / published_end_at
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Using `prefix: true` Option
|
|
203
|
+
Use the `prefix: true` option if you prefer the scope name as a prefix instead of suffix.
|
|
204
|
+
|
|
205
|
+
```ruby
|
|
206
|
+
class Event < ActiveRecord::Base
|
|
207
|
+
include InTimeScope
|
|
208
|
+
|
|
209
|
+
# With prefix: true, the method name becomes published_in_time instead of in_time_published
|
|
210
|
+
in_time_scope :published, prefix: true
|
|
211
|
+
end
|
|
212
|
+
|
|
197
213
|
Event.published_in_time
|
|
198
|
-
# => uses
|
|
214
|
+
# => uses published_start_at / published_end_at
|
|
199
215
|
```
|
|
200
216
|
|
|
201
217
|
### Using with `has_one` Associations
|
data/lib/in_time_scope.rb
CHANGED
|
@@ -11,14 +11,14 @@ module InTimeScope
|
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
module ClassMethods
|
|
14
|
-
def in_time_scope(scope_name = nil, start_at: {}, end_at: {})
|
|
14
|
+
def in_time_scope(scope_name = nil, start_at: {}, end_at: {}, prefix: false)
|
|
15
15
|
scope_name ||= :in_time
|
|
16
|
-
|
|
16
|
+
scope_prefix = scope_name == :in_time ? "" : "#{scope_name}_"
|
|
17
17
|
|
|
18
|
-
start_config = normalize_config(start_at, :"
|
|
19
|
-
end_config = normalize_config(end_at, :"
|
|
18
|
+
start_config = normalize_config(start_at, :"#{scope_prefix}start_at", :start_at)
|
|
19
|
+
end_config = normalize_config(end_at, :"#{scope_prefix}end_at", :end_at)
|
|
20
20
|
|
|
21
|
-
define_scope_methods(scope_name, start_config, end_config)
|
|
21
|
+
define_scope_methods(scope_name, start_config, end_config, prefix)
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
private
|
|
@@ -42,8 +42,14 @@ module InTimeScope
|
|
|
42
42
|
col ? col.null : true
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
def define_scope_methods(scope_name, start_config, end_config)
|
|
46
|
-
method_name = scope_name == :in_time
|
|
45
|
+
def define_scope_methods(scope_name, start_config, end_config, prefix)
|
|
46
|
+
method_name = if scope_name == :in_time
|
|
47
|
+
:in_time
|
|
48
|
+
elsif prefix
|
|
49
|
+
:"#{scope_name}_in_time"
|
|
50
|
+
else
|
|
51
|
+
:"in_time_#{scope_name}"
|
|
52
|
+
end
|
|
47
53
|
instance_method_name = :"#{method_name}?"
|
|
48
54
|
|
|
49
55
|
start_column = start_config[:column]
|
|
@@ -89,7 +95,7 @@ module InTimeScope
|
|
|
89
95
|
end
|
|
90
96
|
|
|
91
97
|
def define_latest_scope(method_name, start_column, start_null)
|
|
92
|
-
latest_method_name = method_name == :in_time ? :latest_in_time : :"latest_#{method_name}
|
|
98
|
+
latest_method_name = method_name == :in_time ? :latest_in_time : :"latest_#{method_name}"
|
|
93
99
|
tbl = table_name
|
|
94
100
|
col = start_column
|
|
95
101
|
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: in_time_scope
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- kyohah
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-01-
|
|
10
|
+
date: 2026-01-29 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: activerecord
|