niceties 0.3.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 -63
- 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/core_ext/numeric/percent_of.rb +7 -0
- 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/core_ext/object/try_each.rb +26 -0
- 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 -35
- metadata +21 -8
- data/lib/niceties/not_a.rb +0 -10
- data/lib/niceties/percent_of.rb +0 -10
- data/lib/niceties/plus_or_minus.rb +0 -18
- data/lib/niceties/tidy.rb +0 -10
- data/lib/niceties/try_each.rb +0 -26
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,87 +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
|
-
###
|
|
49
|
-
|
|
50
|
-
```rb
|
|
51
|
-
# Integer.percent_of
|
|
52
|
-
25.percent_of(200) => 50 # instead of (200 / 100.0) * 25
|
|
53
|
-
|
|
54
|
-
# Float.percent_of
|
|
55
|
-
16.6.percent_of(1000) => 166 # instead of (1000 / 100.0) * 16.6
|
|
33
|
+
### Numeric
|
|
56
34
|
|
|
57
|
-
|
|
58
|
-
|
|
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
|
|
59
40
|
```
|
|
60
41
|
|
|
61
|
-
### Time
|
|
42
|
+
### Time, Date, DateTime
|
|
62
43
|
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
More methods will be coming soon and contributions are very much welcomed!
|
|
68
|
-
|
|
69
|
-
---
|
|
70
|
-
|
|
71
|
-
## 🫂 Contributing
|
|
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
|
|
72
48
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
> _"This is too small for a gem, but I wish it lived somewhere..."_
|
|
78
|
-
|
|
79
|
-
That’s exactly what Niceties is for.
|
|
49
|
+
(Time.current + 1.day).future? # => true
|
|
50
|
+
(Time.current - 1.day).past? # => true
|
|
51
|
+
```
|
|
80
52
|
|
|
81
|
-
|
|
53
|
+
## Contributing
|
|
82
54
|
|
|
83
|
-
|
|
84
|
-
- Does it align with Ruby’s style and philosophy?
|
|
85
|
-
- 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.
|
|
86
56
|
|
|
87
|
-
|
|
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
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/core_ext/object/try"
|
|
4
|
+
require "active_support/core_ext/object/blank"
|
|
5
|
+
|
|
6
|
+
class Object
|
|
7
|
+
def try_each(*methods)
|
|
8
|
+
methods.each do |method|
|
|
9
|
+
result = try(method)
|
|
10
|
+
return result unless result.nil?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
nil
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def coalesce(*messages)
|
|
17
|
+
*methods, fallback = messages
|
|
18
|
+
|
|
19
|
+
methods.each do |method|
|
|
20
|
+
result = try(method)
|
|
21
|
+
return result if result.present?
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
fallback.is_a?(Symbol) ? try(fallback) : fallback
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -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,41 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "date"
|
|
4
|
-
require "active_support/core_ext/object/try"
|
|
5
|
-
require "active_support/core_ext/object/blank"
|
|
6
3
|
require_relative "niceties/version"
|
|
7
|
-
require_relative "niceties/
|
|
8
|
-
require_relative "niceties/not_a"
|
|
9
|
-
require_relative "niceties/try_each"
|
|
10
|
-
require_relative "niceties/percent_of"
|
|
11
|
-
require_relative "niceties/tidy"
|
|
4
|
+
require_relative "niceties/core_ext"
|
|
12
5
|
|
|
13
|
-
# See included modules for implementations
|
|
14
6
|
module Niceties
|
|
15
7
|
end
|
|
16
|
-
|
|
17
|
-
class Object
|
|
18
|
-
include Niceties::NotA
|
|
19
|
-
include Niceties::TryEach
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
class Array
|
|
23
|
-
include Niceties::Tidy
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
class Numeric
|
|
27
|
-
include Niceties::PlusOrMinus
|
|
28
|
-
include Niceties::PercentOf
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
class Time
|
|
32
|
-
include Niceties::PlusOrMinus
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
class Date
|
|
36
|
-
include Niceties::PlusOrMinus
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
class DateTime
|
|
40
|
-
include Niceties::PlusOrMinus
|
|
41
|
-
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,11 +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/
|
|
47
|
-
- 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
|
|
48
61
|
- lib/niceties/version.rb
|
|
49
62
|
- sig/niceties.rbs
|
|
50
63
|
homepage: https://github.com/carldaws/niceties
|
|
@@ -61,14 +74,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
61
74
|
requirements:
|
|
62
75
|
- - ">="
|
|
63
76
|
- !ruby/object:Gem::Version
|
|
64
|
-
version:
|
|
77
|
+
version: 2.7.0
|
|
65
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
79
|
requirements:
|
|
67
80
|
- - ">="
|
|
68
81
|
- !ruby/object:Gem::Version
|
|
69
82
|
version: '0'
|
|
70
83
|
requirements: []
|
|
71
|
-
rubygems_version:
|
|
84
|
+
rubygems_version: 4.0.3
|
|
72
85
|
specification_version: 4
|
|
73
86
|
summary: A collection of thoughtful Ruby and Rails helpers.
|
|
74
87
|
test_files: []
|
data/lib/niceties/not_a.rb
DELETED
data/lib/niceties/percent_of.rb
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Niceties
|
|
4
|
-
# Niceties for creating ranges
|
|
5
|
-
module PlusOrMinus
|
|
6
|
-
def plus_or_minus(amount)
|
|
7
|
-
(self - amount)..(self + amount)
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def plus_upto(amount)
|
|
11
|
-
self..(self + amount)
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
def minus_upto(amount)
|
|
15
|
-
(self - amount)..self
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
end
|
data/lib/niceties/tidy.rb
DELETED
data/lib/niceties/try_each.rb
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Niceties
|
|
4
|
-
# Niceties for trying multiple methods
|
|
5
|
-
module TryEach
|
|
6
|
-
def try_each(*methods)
|
|
7
|
-
methods.each do |method|
|
|
8
|
-
result = try(method)
|
|
9
|
-
return result unless result.nil?
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
nil
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def coalesce(*messages)
|
|
16
|
-
*methods, fallback = messages
|
|
17
|
-
|
|
18
|
-
methods.each do |method|
|
|
19
|
-
result = try(method)
|
|
20
|
-
return result if result.present?
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
fallback.is_a?(Symbol) ? try(fallback) : fallback
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
end
|