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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5303d182f90f15c71f74382af7747c3f59db95b71869c76558607575f16096d8
4
- data.tar.gz: 587bb532dadde01c08f8ec563c0032adaa596d9bc7ee35ebc049735917ba0919
3
+ metadata.gz: a864755de9a566ad580394e72820ea1f9d824b5f5a54997494198ad984b50c7b
4
+ data.tar.gz: 1dad3eb338a74c7ea1eef8b486a5dac9d188ca0483fde5c2cac12abeb06d7c0e
5
5
  SHA512:
6
- metadata.gz: 5f64b16ad027af13f3564e9f163c374f69ab68389fd1017b7e52254b0ae415541e504d7449c03ce860e113ed704a8f37aaba65aef1d8f83b56cfb6be73e2bd0e
7
- data.tar.gz: 4fd2cd8131b9c76525abd11b7f986e1ec7f507efec8855a1c2215eda59a8e33cbaf584158c94aef2493da4d7cd457d3e1f8673c44c719f58b340e633a15a57e9
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
- ### Numerics (Integer and Float)
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,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Niceties
4
+ # Niceties for determining object classes
5
+ module NotA
6
+ def not_a?(klass)
7
+ !is_a?(klass)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Niceties
4
+ # Niceties for calculating percentages
5
+ module PercentOf
6
+ def percent_of(other)
7
+ (other / 100.0) * to_f
8
+ end
9
+ end
10
+ end
@@ -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,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Niceties
4
+ # Niceties for removing blanks from Arrays
5
+ module Tidy
6
+ def tidy
7
+ select { it.present? }
8
+ end
9
+ end
10
+ 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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Niceties
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
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/object"
7
- require_relative "niceties/array"
8
- require_relative "niceties/integer"
9
- require_relative "niceties/float"
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 individual class files for implementations
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.2.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/array.rb
44
- - lib/niceties/float.rb
45
- - lib/niceties/integer.rb
46
- - lib/niceties/object.rb
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
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Niceties for the Array class
4
- class Array
5
- def tidy
6
- select { it.present? }
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Niceties for the Float class
4
- class Float
5
- def percent_of(other)
6
- (other / 100.0) * self
7
- end
8
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Niceties for the Integer class
4
- class Integer
5
- def percent_of(other)
6
- (other / 100.0) * to_f
7
- end
8
- end
@@ -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