plus_or_minus 0.1.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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/lib/plus_or_minus/version.rb +5 -0
- data/lib/plus_or_minus.rb +34 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2aff73c6ed5d9c3c36af73f5021e679dc4964f0105352450955a100448ad06f9
|
4
|
+
data.tar.gz: d067a1b5f570a60ef306ba4f0b353382129c6836e66b0ebac58257f4355d9eec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8648f33b2048c16fbcb51433f8f13348afd3f0050715f0f7e90db51c62a2e9d2f8c98f81c821380dcd6eece94bd832bd9da4700bcc6906232e7b76354bc9b1e2
|
7
|
+
data.tar.gz: de4b139d1ae262ba45fbb5678bcef41f9b3dc414feff7f9708a42951710611c4a241221f9bb74a440fc7a7474eac5d64cee8b0cf2ec2b33a255a28568240b1bc
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Carl Dawson
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# PlusOrMinus
|
2
|
+
|
3
|
+
**PlusOrMinus** is a simple Ruby gem that extends `Time`, `DateTime`, and `Numeric` with convenient range methods for adding and subtracting values.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'plus_or_minus'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute `bundle install`
|
14
|
+
|
15
|
+
Or, install it manually with `gem install plus_or_minus`
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
To use PlusOrMinus, you need to enable the refinements within your scope:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require "plus_or_minus"
|
23
|
+
using PlusOrMinus
|
24
|
+
|
25
|
+
# Numeric
|
26
|
+
5.plus_or_minus(2) # => (3..7)
|
27
|
+
5.plus_upto(2) # (5..7)
|
28
|
+
5.minus_upto(2) # (3..5)
|
29
|
+
|
30
|
+
# Time
|
31
|
+
time = Time.now
|
32
|
+
time.plus_or_minus(3600) # => (time - 1.hour)..(time + 1.hour)
|
33
|
+
time.plus_upto(86400) # => time..(time + 1.day)
|
34
|
+
time.minus_upto(86400) # => (time - 1.day)..time
|
35
|
+
|
36
|
+
# DateTime
|
37
|
+
date = DateTime.now
|
38
|
+
date.plus_or_minus(1) # => (date - 1.day)..(date + 1.day)
|
39
|
+
date.plus_upto(3) # => date..(date + 3.days)
|
40
|
+
date.minus_upto(3) # => (date - 3.days)..date
|
41
|
+
```
|
42
|
+
|
43
|
+
## Rails
|
44
|
+
|
45
|
+
If you're using Rails, PlusOrMinus works beautifully with ActiveRecord queries and Rails' time helpers:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
using PlusOrMinus
|
49
|
+
|
50
|
+
# Fetch users created within the last hour
|
51
|
+
User.where(created_at: Time.current.minus_upto(1.hour))
|
52
|
+
|
53
|
+
# Fetch events happening within ±3 days from today
|
54
|
+
Event.where(start_time: Date.today.plus_or_minus(3.days))
|
55
|
+
|
56
|
+
# Find orders with a deadline in the next 2 weeks
|
57
|
+
Order.where(due_date: Date.today.plus_upto(2.weeks))
|
58
|
+
|
59
|
+
# Fetch logs recorded within 5 minutes of a specific timestamp
|
60
|
+
Log.where(timestamp: some_time.plus_or_minus(5.minutes))
|
61
|
+
|
62
|
+
# With Paranoia or similar:
|
63
|
+
Model.where(deleted_at: parent.deleted_at.plus_or_minus(1.minute))
|
64
|
+
```
|
65
|
+
|
66
|
+
## Refinements
|
67
|
+
|
68
|
+
This gem uses refinements to extend Time, DateTime, and Numeric. To use it in a file, you must explicitly enable refinements with `using PlusOrMinus`.
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
This gem is available as open-source under the MIT License.
|
73
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "plus_or_minus/version"
|
4
|
+
require "date"
|
5
|
+
|
6
|
+
module PlusOrMinus
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
module InstanceMethods
|
10
|
+
def plus_or_minus(span)
|
11
|
+
(self - span)..(self + span)
|
12
|
+
end
|
13
|
+
|
14
|
+
def plus_upto(span)
|
15
|
+
self..(self + span)
|
16
|
+
end
|
17
|
+
|
18
|
+
def minus_upto(span)
|
19
|
+
(self - span)..self
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
refine Time do
|
24
|
+
import_methods InstanceMethods
|
25
|
+
end
|
26
|
+
|
27
|
+
refine DateTime do
|
28
|
+
import_methods InstanceMethods
|
29
|
+
end
|
30
|
+
|
31
|
+
refine Numeric do
|
32
|
+
import_methods InstanceMethods
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: plus_or_minus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Carl Dawson
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-03-03 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
description: PlusOrMinus refines Time, DateTime, and Numeric with convenient range
|
13
|
+
operations.
|
14
|
+
email:
|
15
|
+
- email@carldaws.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE.txt
|
21
|
+
- README.md
|
22
|
+
- lib/plus_or_minus.rb
|
23
|
+
- lib/plus_or_minus/version.rb
|
24
|
+
homepage: https://github.com/carldaws/plus_or_minus
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.1.0
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubygems_version: 3.6.2
|
43
|
+
specification_version: 4
|
44
|
+
summary: A simple refinement-based gem for numeric and date range calculations.
|
45
|
+
test_files: []
|