niceties 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5303d182f90f15c71f74382af7747c3f59db95b71869c76558607575f16096d8
4
- data.tar.gz: 587bb532dadde01c08f8ec563c0032adaa596d9bc7ee35ebc049735917ba0919
3
+ metadata.gz: 0a9ad3ee3ee46696a6666807b885e63a91aa712b498fc1a40e5a15504c12802d
4
+ data.tar.gz: e53b14269f544e47be8cf5fe1824faf37320460e7549fb60eb9b88ed1ba1ddc1
5
5
  SHA512:
6
- metadata.gz: 5f64b16ad027af13f3564e9f163c374f69ab68389fd1017b7e52254b0ae415541e504d7449c03ce860e113ed704a8f37aaba65aef1d8f83b56cfb6be73e2bd0e
7
- data.tar.gz: 4fd2cd8131b9c76525abd11b7f986e1ec7f507efec8855a1c2215eda59a8e33cbaf584158c94aef2493da4d7cd457d3e1f8673c44c719f58b340e633a15a57e9
6
+ metadata.gz: 4c0ef7842dba7bd9a6ef8b594707a2fb5d698b892848ea86c7fd6f1e9d611af40d2e414d9736e4bb579af8689efc8804d69f1c129a8c13ac323bdd18a38a3cc9
7
+ data.tar.gz: 67f149fa184f53744fdc857edcc3652b33c0fa00c76040c050cb6ac24561a5624c05da14163ce277325eb72a8eafdef64707ae06532840c8dcf60f8eecc61368
data/Gemfile CHANGED
@@ -9,5 +9,6 @@ gem "irb"
9
9
  gem "rake", "~> 13.0"
10
10
 
11
11
  gem "minitest", "~> 5.16"
12
+ gem "ostruct"
12
13
 
13
14
  gem "rubocop", "~> 1.21"
data/README.md CHANGED
@@ -1,78 +1,57 @@
1
1
  # Niceties
2
2
 
3
- Niceties is a curated collection of small, thoughtful additions to Rails.
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
- It’s the kind of thing you might monkey-patch in your own app and forget about. Niceties gives those private little helpers a permanent, public home.
5
+ ## Installation
8
6
 
9
- ---
10
-
11
- ## ✨ Philosophy
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
- Niceties provides the following methods so far:
11
+ ## Methods
27
12
 
28
13
  ### Object
29
14
 
30
- ```rb
31
- # Object.try_each
32
- @user.try_each(:full_name, :nickname, :username) # instead of @user&.full_name || @user&.nickname || @user&.username
15
+ ```ruby
16
+ @user.try_each(:full_name, :nickname, :username)
17
+ # => returns first non-nil result
33
18
 
34
- # Object.coalesce
35
- @user.coalesce(:full_name, :nickname, "Valued Customer") # instead of @user&.full_name || @user&.nickname || "Valued Customer"
19
+ @user.coalesce(:full_name, :nickname, "Guest")
20
+ # => returns first present result, or fallback
36
21
 
37
- # Object.not_a?
38
- @user.not_a? Widget # instead of !@user.is_a? Widget
22
+ @user.not_a?(Widget)
23
+ # => true if not a Widget
39
24
  ```
40
25
 
41
26
  ### Array
42
27
 
43
- ```rb
44
- # Array.tidy
45
- ["", nil, 4].tidy => [4] # instead of ["", nil, 4].select { it.present? }
28
+ ```ruby
29
+ ["", nil, 4].tidy
30
+ # => [4]
46
31
  ```
47
32
 
48
- ### Numerics (Integer and Float)
33
+ ### Numeric
49
34
 
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
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
56
40
  ```
57
41
 
58
- More methods will be coming soon and contributions are very much welcomed!
59
-
60
- ---
61
-
62
- ## 🫂 Contributing
42
+ ### Time, Date, DateTime
63
43
 
64
- Niceties is open to anyone with an idea that makes Ruby or Rails feel better to use.
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
65
48
 
66
- If you’ve ever written a helper method and thought:
67
-
68
- > _"This is too small for a gem, but I wish it lived somewhere..."_
69
-
70
- That’s exactly what Niceties is for.
49
+ (Time.current + 1.day).future? # => true
50
+ (Time.current - 1.day).past? # => true
51
+ ```
71
52
 
72
- **Guiding questions for contributors:**
53
+ ## Contributing
73
54
 
74
- - Does this make everyday code more readable or expressive?
75
- - Does it align with Ruby’s style and philosophy?
76
- - 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.
77
56
 
78
- If yes open a PR or start a discussion.
57
+ PRs welcome if the method is readable, unsurprising, and feels like Ruby.
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/object/blank"
4
+
5
+ class Array
6
+ def tidy
7
+ select(&:present?)
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "array/tidy"
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require_relative "../numeric/plus_or_minus"
5
+
6
+ class Date
7
+ include Numeric::PlusOrMinus
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require_relative "../time/tenses"
5
+
6
+ class Date
7
+ include Time::Tenses
8
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "date/plus_or_minus"
4
+ require_relative "date/tenses"
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require_relative "../numeric/plus_or_minus"
5
+
6
+ class DateTime
7
+ include Numeric::PlusOrMinus
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require_relative "../time/tenses"
5
+
6
+ class DateTime
7
+ include Time::Tenses
8
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "date_time/plus_or_minus"
4
+ require_relative "date_time/tenses"
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Niceties for the Integer class
4
- class Integer
3
+ class Numeric
5
4
  def percent_of(other)
6
5
  (other / 100.0) * to_f
7
6
  end
@@ -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,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "numeric/percent_of"
4
+ require_relative "numeric/plus_or_minus"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Object
4
+ def not_a?(klass)
5
+ !is_a?(klass)
6
+ end
7
+ end
@@ -1,11 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Niceties for the Object class
4
- class Object
5
- def not_a?(klass)
6
- !is_a?(klass)
7
- end
3
+ require "active_support/core_ext/object/try"
4
+ require "active_support/core_ext/object/blank"
8
5
 
6
+ class Object
9
7
  def try_each(*methods)
10
8
  methods.each do |method|
11
9
  result = try(method)
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "object/not_a"
4
+ require_relative "object/try_each"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../numeric/plus_or_minus"
4
+
5
+ class Time
6
+ include Numeric::PlusOrMinus
7
+ 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
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "time/plus_or_minus"
4
+ require_relative "time/tenses"
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "core_ext/object"
4
+ require_relative "core_ext/array"
5
+ require_relative "core_ext/numeric"
6
+ require_relative "core_ext/time"
7
+ require_relative "core_ext/date"
8
+ require_relative "core_ext/date_time"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Niceties
4
- VERSION = "0.2.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/niceties.rb CHANGED
@@ -1,13 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_support/core_ext/object/try"
4
- require "active_support/core_ext/object/blank"
5
3
  require_relative "niceties/version"
6
- require_relative "niceties/object"
7
- require_relative "niceties/array"
8
- require_relative "niceties/integer"
9
- require_relative "niceties/float"
4
+ require_relative "niceties/core_ext"
10
5
 
11
- # See individual class files for implementations
12
6
  module Niceties
13
7
  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.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Dawson
@@ -40,10 +40,24 @@ 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/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
47
61
  - lib/niceties/version.rb
48
62
  - sig/niceties.rbs
49
63
  homepage: https://github.com/carldaws/niceties
@@ -60,14 +74,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
74
  requirements:
61
75
  - - ">="
62
76
  - !ruby/object:Gem::Version
63
- version: 3.1.0
77
+ version: 2.7.0
64
78
  required_rubygems_version: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  requirements: []
70
- rubygems_version: 3.6.7
84
+ rubygems_version: 4.0.3
71
85
  specification_version: 4
72
86
  summary: A collection of thoughtful Ruby and Rails helpers.
73
87
  test_files: []
@@ -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