niceties 0.3.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: a864755de9a566ad580394e72820ea1f9d824b5f5a54997494198ad984b50c7b
4
- data.tar.gz: 1dad3eb338a74c7ea1eef8b486a5dac9d188ca0483fde5c2cac12abeb06d7c0e
3
+ metadata.gz: 0a9ad3ee3ee46696a6666807b885e63a91aa712b498fc1a40e5a15504c12802d
4
+ data.tar.gz: e53b14269f544e47be8cf5fe1824faf37320460e7549fb60eb9b88ed1ba1ddc1
5
5
  SHA512:
6
- metadata.gz: 7adf9811df2dc492c67768bb5b20076f6651c543231ce94794174b765d88d32e259af2867397ebcf4dcd9c7cada88d892522325045dc38dc7b6bfa2658988052
7
- data.tar.gz: ea59ade372a556e0455b5847f0c905816f908ea0cc206d0034e1c6159cf2cc912ae48f97ddf1a01e11c2c4b4eadeeda68c491ad40ac174f82af83d3bd9e6ca68
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,87 +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)
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
33
+ ### Numeric
56
34
 
57
- # Numeric.plus_or_minus
58
- 5.plus_or_minus(2) => 3..7 # instead of (5-2)..(5+2)
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
59
40
  ```
60
41
 
61
- ### Time
42
+ ### Time, Date, DateTime
62
43
 
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
-
71
- ## 🫂 Contributing
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
72
48
 
73
- Niceties is open to anyone with an idea that makes Ruby or Rails feel better to use.
74
-
75
- If you’ve ever written a helper method and thought:
76
-
77
- > _"This is too small for a gem, but I wish it lived somewhere..."_
78
-
79
- That’s exactly what Niceties is for.
49
+ (Time.current + 1.day).future? # => true
50
+ (Time.current - 1.day).past? # => true
51
+ ```
80
52
 
81
- **Guiding questions for contributors:**
53
+ ## Contributing
82
54
 
83
- - Does this make everyday code more readable or expressive?
84
- - Does it align with Ruby’s style and philosophy?
85
- - 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.
86
56
 
87
- 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"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Numeric
4
+ def percent_of(other)
5
+ (other / 100.0) * to_f
6
+ end
7
+ 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
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/object/try"
4
+ require "active_support/core_ext/object/blank"
5
+
6
+ class Object
7
+ def try_each(*methods)
8
+ methods.each do |method|
9
+ result = try(method)
10
+ return result unless result.nil?
11
+ end
12
+
13
+ nil
14
+ end
15
+
16
+ def coalesce(*messages)
17
+ *methods, fallback = messages
18
+
19
+ methods.each do |method|
20
+ result = try(method)
21
+ return result if result.present?
22
+ end
23
+
24
+ fallback.is_a?(Symbol) ? try(fallback) : fallback
25
+ end
26
+ end
@@ -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.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/niceties.rb CHANGED
@@ -1,41 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "date"
4
- require "active_support/core_ext/object/try"
5
- require "active_support/core_ext/object/blank"
6
3
  require_relative "niceties/version"
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"
4
+ require_relative "niceties/core_ext"
12
5
 
13
- # See included modules for implementations
14
6
  module Niceties
15
7
  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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carl Dawson
@@ -40,11 +40,24 @@ files:
40
40
  - bin/console
41
41
  - bin/setup
42
42
  - lib/niceties.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
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
48
61
  - lib/niceties/version.rb
49
62
  - sig/niceties.rbs
50
63
  homepage: https://github.com/carldaws/niceties
@@ -61,14 +74,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
74
  requirements:
62
75
  - - ">="
63
76
  - !ruby/object:Gem::Version
64
- version: 3.1.0
77
+ version: 2.7.0
65
78
  required_rubygems_version: !ruby/object:Gem::Requirement
66
79
  requirements:
67
80
  - - ">="
68
81
  - !ruby/object:Gem::Version
69
82
  version: '0'
70
83
  requirements: []
71
- rubygems_version: 3.6.7
84
+ rubygems_version: 4.0.3
72
85
  specification_version: 4
73
86
  summary: A collection of thoughtful Ruby and Rails helpers.
74
87
  test_files: []
@@ -1,10 +0,0 @@
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
@@ -1,10 +0,0 @@
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
@@ -1,18 +0,0 @@
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
data/lib/niceties/tidy.rb DELETED
@@ -1,10 +0,0 @@
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
@@ -1,26 +0,0 @@
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