niceties 0.1.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: 5a344de89699dbdc123ebe5cd16c0457e0b35b7ef3717db07348415b752b6b75
4
- data.tar.gz: 3a4f87520eb6808cc9998720aa283f84a6841f9f5505f1d4d6faf02773314441
3
+ metadata.gz: a864755de9a566ad580394e72820ea1f9d824b5f5a54997494198ad984b50c7b
4
+ data.tar.gz: 1dad3eb338a74c7ea1eef8b486a5dac9d188ca0483fde5c2cac12abeb06d7c0e
5
5
  SHA512:
6
- metadata.gz: d05d3be08f1576176537516acedead938c049985e81a5e6ac143d90b3b1af477e2a95110da8209355630ecef5a9dd360f2c34e4bd12c0d2de5c3a28b7a4e53dd
7
- data.tar.gz: e21eb52f1c78b876fb340f3b276eb210c1c96dacea0491f23d700e13e20e777e0fd98f4a5652c5f84f9433c5cf2711533283f6588a5b4ac44ca89b53e4ca0272
6
+ metadata.gz: 7adf9811df2dc492c67768bb5b20076f6651c543231ce94794174b765d88d32e259af2867397ebcf4dcd9c7cada88d892522325045dc38dc7b6bfa2658988052
7
+ data.tar.gz: ea59ade372a556e0455b5847f0c905816f908ea0cc206d0034e1c6159cf2cc912ae48f97ddf1a01e11c2c4b4eadeeda68c491ad40ac174f82af83d3bd9e6ca68
data/.gitignore CHANGED
@@ -6,3 +6,5 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ *.gem
10
+ Gemfile.lock
data/README.md CHANGED
@@ -21,6 +21,53 @@ You should be able to drop Niceties into your app and feel like it’s always be
21
21
 
22
22
  ---
23
23
 
24
+ ## What's on the Menu
25
+
26
+ Niceties provides the following methods so far:
27
+
28
+ ### Object
29
+
30
+ ```rb
31
+ # Object.try_each
32
+ @user.try_each(:full_name, :nickname, :username) # instead of @user&.full_name || @user&.nickname || @user&.username
33
+
34
+ # Object.coalesce
35
+ @user.coalesce(:full_name, :nickname, "Valued Customer") # instead of @user&.full_name || @user&.nickname || "Valued Customer"
36
+
37
+ # Object.not_a?
38
+ @user.not_a? Widget # instead of !@user.is_a? Widget
39
+ ```
40
+
41
+ ### Array
42
+
43
+ ```rb
44
+ # Array.tidy
45
+ ["", nil, 4].tidy => [4] # instead of ["", nil, 4].select { it.present? }
46
+ ```
47
+
48
+ ### Numerics (Integer and Float)
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
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))
65
+ ```
66
+
67
+ More methods will be coming soon and contributions are very much welcomed!
68
+
69
+ ---
70
+
24
71
  ## 🫂 Contributing
25
72
 
26
73
  Niceties is open to anyone with an idea that makes Ruby or Rails feel better to use.
@@ -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.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/niceties.rb CHANGED
@@ -1,9 +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/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"
7
12
 
13
+ # See included modules for implementations
8
14
  module Niceties
9
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.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Dawson
@@ -23,8 +23,8 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: '6.0'
26
- description: Niceties is a carefully curated set of ergonomic Ruby and Rails extensions—small,
27
- expressive methods that make code feel more natural and joyful to write.
26
+ description: Niceties is a curated set of small, ergonomic and expressive methods
27
+ for Ruby and Rails.
28
28
  email:
29
29
  - email@carldaws.com
30
30
  executables: []
@@ -40,7 +40,11 @@ files:
40
40
  - bin/console
41
41
  - bin/setup
42
42
  - lib/niceties.rb
43
- - 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
44
48
  - lib/niceties/version.rb
45
49
  - sig/niceties.rbs
46
50
  homepage: https://github.com/carldaws/niceties
@@ -1,25 +0,0 @@
1
- class Object
2
- def not_a?(klass)
3
- !is_a?(klass)
4
- end
5
-
6
- def try_all(*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