niceties 0.1.0 → 0.2.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: 5303d182f90f15c71f74382af7747c3f59db95b71869c76558607575f16096d8
4
+ data.tar.gz: 587bb532dadde01c08f8ec563c0032adaa596d9bc7ee35ebc049735917ba0919
5
5
  SHA512:
6
- metadata.gz: d05d3be08f1576176537516acedead938c049985e81a5e6ac143d90b3b1af477e2a95110da8209355630ecef5a9dd360f2c34e4bd12c0d2de5c3a28b7a4e53dd
7
- data.tar.gz: e21eb52f1c78b876fb340f3b276eb210c1c96dacea0491f23d700e13e20e777e0fd98f4a5652c5f84f9433c5cf2711533283f6588a5b4ac44ca89b53e4ca0272
6
+ metadata.gz: 5f64b16ad027af13f3564e9f163c374f69ab68389fd1017b7e52254b0ae415541e504d7449c03ce860e113ed704a8f37aaba65aef1d8f83b56cfb6be73e2bd0e
7
+ data.tar.gz: 4fd2cd8131b9c76525abd11b7f986e1ec7f507efec8855a1c2215eda59a8e33cbaf584158c94aef2493da4d7cd457d3e1f8673c44c719f58b340e633a15a57e9
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,44 @@ 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
+
58
+ More methods will be coming soon and contributions are very much welcomed!
59
+
60
+ ---
61
+
24
62
  ## 🫂 Contributing
25
63
 
26
64
  Niceties is open to anyone with an idea that makes Ruby or Rails feel better to use.
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,8 @@
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,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Niceties for the Object class
1
4
  class Object
2
5
  def not_a?(klass)
3
6
  !is_a?(klass)
4
7
  end
5
8
 
6
- def try_all(*methods)
9
+ def try_each(*methods)
7
10
  methods.each do |method|
8
11
  result = try(method)
9
12
  return result unless result.nil?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Niceties
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/niceties.rb CHANGED
@@ -4,6 +4,10 @@ require "active_support/core_ext/object/try"
4
4
  require "active_support/core_ext/object/blank"
5
5
  require_relative "niceties/version"
6
6
  require_relative "niceties/object"
7
+ require_relative "niceties/array"
8
+ require_relative "niceties/integer"
9
+ require_relative "niceties/float"
7
10
 
11
+ # See individual class files for implementations
8
12
  module Niceties
9
13
  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.2.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,6 +40,9 @@ 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
43
46
  - lib/niceties/object.rb
44
47
  - lib/niceties/version.rb
45
48
  - sig/niceties.rbs