niceties 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 +10 -1
- data/lib/niceties/not_a.rb +10 -0
- data/lib/niceties/percent_of.rb +10 -0
- data/lib/niceties/plus_or_minus.rb +18 -0
- data/lib/niceties/tidy.rb +10 -0
- data/lib/niceties/try_each.rb +26 -0
- data/lib/niceties/version.rb +1 -1
- data/lib/niceties.rb +33 -5
- metadata +6 -5
- data/lib/niceties/array.rb +0 -8
- data/lib/niceties/float.rb +0 -8
- data/lib/niceties/integer.rb +0 -8
- data/lib/niceties/object.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a864755de9a566ad580394e72820ea1f9d824b5f5a54997494198ad984b50c7b
|
4
|
+
data.tar.gz: 1dad3eb338a74c7ea1eef8b486a5dac9d188ca0483fde5c2cac12abeb06d7c0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7adf9811df2dc492c67768bb5b20076f6651c543231ce94794174b765d88d32e259af2867397ebcf4dcd9c7cada88d892522325045dc38dc7b6bfa2658988052
|
7
|
+
data.tar.gz: ea59ade372a556e0455b5847f0c905816f908ea0cc206d0034e1c6159cf2cc912ae48f97ddf1a01e11c2c4b4eadeeda68c491ad40ac174f82af83d3bd9e6ca68
|
data/README.md
CHANGED
@@ -45,7 +45,7 @@ Niceties provides the following methods so far:
|
|
45
45
|
["", nil, 4].tidy => [4] # instead of ["", nil, 4].select { it.present? }
|
46
46
|
```
|
47
47
|
|
48
|
-
###
|
48
|
+
### Numerics (Integer and Float)
|
49
49
|
|
50
50
|
```rb
|
51
51
|
# Integer.percent_of
|
@@ -53,6 +53,15 @@ Niceties provides the following methods so far:
|
|
53
53
|
|
54
54
|
# Float.percent_of
|
55
55
|
16.6.percent_of(1000) => 166 # instead of (1000 / 100.0) * 16.6
|
56
|
+
|
57
|
+
# Numeric.plus_or_minus
|
58
|
+
5.plus_or_minus(2) => 3..7 # instead of (5-2)..(5+2)
|
59
|
+
```
|
60
|
+
|
61
|
+
### Time
|
62
|
+
|
63
|
+
```rb
|
64
|
+
User.where(created_at: Time.current.minus_upto(1.hour))
|
56
65
|
```
|
57
66
|
|
58
67
|
More methods will be coming soon and contributions are very much welcomed!
|
@@ -0,0 +1,18 @@
|
|
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
|
@@ -0,0 +1,26 @@
|
|
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
|
data/lib/niceties/version.rb
CHANGED
data/lib/niceties.rb
CHANGED
@@ -1,13 +1,41 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "date"
|
3
4
|
require "active_support/core_ext/object/try"
|
4
5
|
require "active_support/core_ext/object/blank"
|
5
6
|
require_relative "niceties/version"
|
6
|
-
require_relative "niceties/
|
7
|
-
require_relative "niceties/
|
8
|
-
require_relative "niceties/
|
9
|
-
require_relative "niceties/
|
7
|
+
require_relative "niceties/plus_or_minus"
|
8
|
+
require_relative "niceties/not_a"
|
9
|
+
require_relative "niceties/try_each"
|
10
|
+
require_relative "niceties/percent_of"
|
11
|
+
require_relative "niceties/tidy"
|
10
12
|
|
11
|
-
# See
|
13
|
+
# See included modules for implementations
|
12
14
|
module Niceties
|
13
15
|
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carl Dawson
|
@@ -40,10 +40,11 @@ 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/not_a.rb
|
44
|
+
- lib/niceties/percent_of.rb
|
45
|
+
- lib/niceties/plus_or_minus.rb
|
46
|
+
- lib/niceties/tidy.rb
|
47
|
+
- lib/niceties/try_each.rb
|
47
48
|
- lib/niceties/version.rb
|
48
49
|
- sig/niceties.rbs
|
49
50
|
homepage: https://github.com/carldaws/niceties
|
data/lib/niceties/array.rb
DELETED
data/lib/niceties/float.rb
DELETED
data/lib/niceties/integer.rb
DELETED
data/lib/niceties/object.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Niceties for the Object class
|
4
|
-
class Object
|
5
|
-
def not_a?(klass)
|
6
|
-
!is_a?(klass)
|
7
|
-
end
|
8
|
-
|
9
|
-
def try_each(*methods)
|
10
|
-
methods.each do |method|
|
11
|
-
result = try(method)
|
12
|
-
return result unless result.nil?
|
13
|
-
end
|
14
|
-
|
15
|
-
nil
|
16
|
-
end
|
17
|
-
|
18
|
-
def coalesce(*messages)
|
19
|
-
*methods, fallback = messages
|
20
|
-
|
21
|
-
methods.each do |method|
|
22
|
-
result = try(method)
|
23
|
-
return result if result.present?
|
24
|
-
end
|
25
|
-
|
26
|
-
fallback.is_a?(Symbol) ? try(fallback) : fallback
|
27
|
-
end
|
28
|
-
end
|