niceties 0.2.0 → 0.4.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/Gemfile +1 -0
- data/README.md +33 -54
- data/lib/niceties/core_ext/array/tidy.rb +9 -0
- data/lib/niceties/core_ext/array.rb +3 -0
- data/lib/niceties/core_ext/date/plus_or_minus.rb +8 -0
- data/lib/niceties/core_ext/date/tenses.rb +8 -0
- data/lib/niceties/core_ext/date.rb +4 -0
- data/lib/niceties/core_ext/date_time/plus_or_minus.rb +8 -0
- data/lib/niceties/core_ext/date_time/tenses.rb +8 -0
- data/lib/niceties/core_ext/date_time.rb +4 -0
- data/lib/niceties/{integer.rb → core_ext/numeric/percent_of.rb} +1 -2
- data/lib/niceties/core_ext/numeric/plus_or_minus.rb +19 -0
- data/lib/niceties/core_ext/numeric.rb +4 -0
- data/lib/niceties/core_ext/object/not_a.rb +7 -0
- data/lib/niceties/{object.rb → core_ext/object/try_each.rb} +3 -5
- data/lib/niceties/core_ext/object.rb +4 -0
- data/lib/niceties/core_ext/time/plus_or_minus.rb +7 -0
- data/lib/niceties/core_ext/time/tenses.rb +17 -0
- data/lib/niceties/core_ext/time.rb +4 -0
- data/lib/niceties/core_ext.rb +8 -0
- data/lib/niceties/version.rb +1 -1
- data/lib/niceties.rb +1 -7
- metadata +21 -7
- data/lib/niceties/array.rb +0 -8
- data/lib/niceties/float.rb +0 -8
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0a9ad3ee3ee46696a6666807b885e63a91aa712b498fc1a40e5a15504c12802d
|
|
4
|
+
data.tar.gz: e53b14269f544e47be8cf5fe1824faf37320460e7549fb60eb9b88ed1ba1ddc1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4c0ef7842dba7bd9a6ef8b594707a2fb5d698b892848ea86c7fd6f1e9d611af40d2e414d9736e4bb579af8689efc8804d69f1c129a8c13ac323bdd18a38a3cc9
|
|
7
|
+
data.tar.gz: 67f149fa184f53744fdc857edcc3652b33c0fa00c76040c050cb6ac24561a5624c05da14163ce277325eb72a8eafdef64707ae06532840c8dcf60f8eecc61368
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,78 +1,57 @@
|
|
|
1
1
|
# Niceties
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Nothing fancy, nothing heavy — just expressive, elegant helpers that make
|
|
5
|
-
everyday code smoother and more joyful.
|
|
3
|
+
Small, expressive helpers for Ruby and Rails. The kind of thing you'd monkey-patch in your own app — given a permanent home.
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
## Installation
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
Niceties is built on a few quiet principles:
|
|
14
|
-
|
|
15
|
-
- **Ergonomics matter.** Tiny syntax improvements compound across projects and years.
|
|
16
|
-
- **Less is more.** This isn’t a kitchen sink. Every addition is judged by feel.
|
|
17
|
-
- **No surprises.** If you have to explain what it’s doing, it probably doesn’t belong here.
|
|
18
|
-
- **Ruby’s voice, not ours.** Every method should *read* like Ruby. Nothing flashy, nothing clever for its own sake.
|
|
19
|
-
|
|
20
|
-
You should be able to drop Niceties into your app and feel like it’s always been there.
|
|
21
|
-
|
|
22
|
-
---
|
|
23
|
-
|
|
24
|
-
## What's on the Menu
|
|
7
|
+
```ruby
|
|
8
|
+
gem 'niceties'
|
|
9
|
+
```
|
|
25
10
|
|
|
26
|
-
|
|
11
|
+
## Methods
|
|
27
12
|
|
|
28
13
|
### Object
|
|
29
14
|
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
|
|
15
|
+
```ruby
|
|
16
|
+
@user.try_each(:full_name, :nickname, :username)
|
|
17
|
+
# => returns first non-nil result
|
|
33
18
|
|
|
34
|
-
|
|
35
|
-
|
|
19
|
+
@user.coalesce(:full_name, :nickname, "Guest")
|
|
20
|
+
# => returns first present result, or fallback
|
|
36
21
|
|
|
37
|
-
|
|
38
|
-
|
|
22
|
+
@user.not_a?(Widget)
|
|
23
|
+
# => true if not a Widget
|
|
39
24
|
```
|
|
40
25
|
|
|
41
26
|
### Array
|
|
42
27
|
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
|
|
28
|
+
```ruby
|
|
29
|
+
["", nil, 4].tidy
|
|
30
|
+
# => [4]
|
|
46
31
|
```
|
|
47
32
|
|
|
48
|
-
###
|
|
33
|
+
### Numeric
|
|
49
34
|
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
#
|
|
55
|
-
16.6.percent_of(1000) => 166 # instead of (1000 / 100.0) * 16.6
|
|
35
|
+
```ruby
|
|
36
|
+
25.percent_of(200) # => 50
|
|
37
|
+
5.plus_or_minus(2) # => 3..7
|
|
38
|
+
5.plus_upto(2) # => 5..7
|
|
39
|
+
5.minus_upto(2) # => 3..5
|
|
56
40
|
```
|
|
57
41
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
---
|
|
61
|
-
|
|
62
|
-
## 🫂 Contributing
|
|
42
|
+
### Time, Date, DateTime
|
|
63
43
|
|
|
64
|
-
|
|
44
|
+
```ruby
|
|
45
|
+
Time.current.plus_or_minus(1.hour) # => Range
|
|
46
|
+
Time.current.plus_upto(1.hour) # => now..future
|
|
47
|
+
Time.current.minus_upto(1.hour) # => past..now
|
|
65
48
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
That’s exactly what Niceties is for.
|
|
49
|
+
(Time.current + 1.day).future? # => true
|
|
50
|
+
(Time.current - 1.day).past? # => true
|
|
51
|
+
```
|
|
71
52
|
|
|
72
|
-
|
|
53
|
+
## Contributing
|
|
73
54
|
|
|
74
|
-
|
|
75
|
-
- Does it align with Ruby’s style and philosophy?
|
|
76
|
-
- Would I be delighted if this existed in the language?
|
|
55
|
+
If you've written a helper and thought "this is too small for a gem" — that's what Niceties is for.
|
|
77
56
|
|
|
78
|
-
|
|
57
|
+
PRs welcome if the method is readable, unsurprising, and feels like Ruby.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Numeric::PlusOrMinus
|
|
4
|
+
def plus_or_minus(amount)
|
|
5
|
+
(self - amount)..(self + amount)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def plus_upto(amount)
|
|
9
|
+
self..(self + amount)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def minus_upto(amount)
|
|
13
|
+
(self - amount)..self
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class Numeric
|
|
18
|
+
include Numeric::PlusOrMinus
|
|
19
|
+
end
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def not_a?(klass)
|
|
6
|
-
!is_a?(klass)
|
|
7
|
-
end
|
|
3
|
+
require "active_support/core_ext/object/try"
|
|
4
|
+
require "active_support/core_ext/object/blank"
|
|
8
5
|
|
|
6
|
+
class Object
|
|
9
7
|
def try_each(*methods)
|
|
10
8
|
methods.each do |method|
|
|
11
9
|
result = try(method)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/core_ext/time/calculations"
|
|
4
|
+
|
|
5
|
+
module Time::Tenses
|
|
6
|
+
def future?
|
|
7
|
+
self > Time.current
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def past?
|
|
11
|
+
self < Time.current
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class Time
|
|
16
|
+
include Time::Tenses
|
|
17
|
+
end
|
data/lib/niceties/version.rb
CHANGED
data/lib/niceties.rb
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "active_support/core_ext/object/try"
|
|
4
|
-
require "active_support/core_ext/object/blank"
|
|
5
3
|
require_relative "niceties/version"
|
|
6
|
-
require_relative "niceties/
|
|
7
|
-
require_relative "niceties/array"
|
|
8
|
-
require_relative "niceties/integer"
|
|
9
|
-
require_relative "niceties/float"
|
|
4
|
+
require_relative "niceties/core_ext"
|
|
10
5
|
|
|
11
|
-
# See individual class files for implementations
|
|
12
6
|
module Niceties
|
|
13
7
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: niceties
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Carl Dawson
|
|
@@ -40,10 +40,24 @@ files:
|
|
|
40
40
|
- bin/console
|
|
41
41
|
- bin/setup
|
|
42
42
|
- lib/niceties.rb
|
|
43
|
-
- lib/niceties/
|
|
44
|
-
- lib/niceties/
|
|
45
|
-
- lib/niceties/
|
|
46
|
-
- lib/niceties/
|
|
43
|
+
- lib/niceties/core_ext.rb
|
|
44
|
+
- lib/niceties/core_ext/array.rb
|
|
45
|
+
- lib/niceties/core_ext/array/tidy.rb
|
|
46
|
+
- lib/niceties/core_ext/date.rb
|
|
47
|
+
- lib/niceties/core_ext/date/plus_or_minus.rb
|
|
48
|
+
- lib/niceties/core_ext/date/tenses.rb
|
|
49
|
+
- lib/niceties/core_ext/date_time.rb
|
|
50
|
+
- lib/niceties/core_ext/date_time/plus_or_minus.rb
|
|
51
|
+
- lib/niceties/core_ext/date_time/tenses.rb
|
|
52
|
+
- lib/niceties/core_ext/numeric.rb
|
|
53
|
+
- lib/niceties/core_ext/numeric/percent_of.rb
|
|
54
|
+
- lib/niceties/core_ext/numeric/plus_or_minus.rb
|
|
55
|
+
- lib/niceties/core_ext/object.rb
|
|
56
|
+
- lib/niceties/core_ext/object/not_a.rb
|
|
57
|
+
- lib/niceties/core_ext/object/try_each.rb
|
|
58
|
+
- lib/niceties/core_ext/time.rb
|
|
59
|
+
- lib/niceties/core_ext/time/plus_or_minus.rb
|
|
60
|
+
- lib/niceties/core_ext/time/tenses.rb
|
|
47
61
|
- lib/niceties/version.rb
|
|
48
62
|
- sig/niceties.rbs
|
|
49
63
|
homepage: https://github.com/carldaws/niceties
|
|
@@ -60,14 +74,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
60
74
|
requirements:
|
|
61
75
|
- - ">="
|
|
62
76
|
- !ruby/object:Gem::Version
|
|
63
|
-
version:
|
|
77
|
+
version: 2.7.0
|
|
64
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
79
|
requirements:
|
|
66
80
|
- - ">="
|
|
67
81
|
- !ruby/object:Gem::Version
|
|
68
82
|
version: '0'
|
|
69
83
|
requirements: []
|
|
70
|
-
rubygems_version:
|
|
84
|
+
rubygems_version: 4.0.3
|
|
71
85
|
specification_version: 4
|
|
72
86
|
summary: A collection of thoughtful Ruby and Rails helpers.
|
|
73
87
|
test_files: []
|
data/lib/niceties/array.rb
DELETED