statusable 0.1.0.pre

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 25c9dd86e72394b2b8e82391af2c925c3b2ab2bab6db1fc2c6620c2d8d51f502
4
+ data.tar.gz: f04aa28c9f999d8594f632d5892bb1ed8aa11c7db6dfb89f064930dcca17ba93
5
+ SHA512:
6
+ metadata.gz: 5a45c11415d8f4122f3a11da376c2ec68b8f502d255386efe96b957fbb1535e52bdc4a16a199c548c0ee8145969894b3be1712094d564742f6897b653e65e004
7
+ data.tar.gz: 516c253ee89fd0251e3b4cf9ccb4af45c2447ffbebf97abc4b5beddf58d0fe6066301360172c18f822e889445556dea0def970a70eb24d39df5e8aee082aa24c
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Paul DobbinSchmaltz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Statusable
2
+
3
+ Statusable adds a `has_statuses` macro for defining common status-related methods for use with ActiveRecord objects / Relations.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ class MyModel < ApplicationRecord
9
+ has_statuses(%w[
10
+ Pending
11
+ Running
12
+ Completed
13
+ ])
14
+ end
15
+ ```
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem "statusable"
23
+ ```
24
+
25
+ And then execute:
26
+ ```bash
27
+ $ bundle
28
+ ```
29
+
30
+ Or install it yourself as:
31
+ ```bash
32
+ $ gem install statusable
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pdobb/statusable.
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. Or, run `rake` to run the tests plus linters as well as `yard` (to confirm proper YARD documentation practices). You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`.
44
+
45
+ To release a new version, update the version number in `version.rb` and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
4
+
5
+ require "bundler/gem_tasks"
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Statusable::HasStatuses adds a `has_statuses` macro for defining common
4
+ # status-related methods around the passed in list of status names for the given
5
+ # {#col_name}.
6
+ #
7
+ # Optional validations exist as well. By default, inclusion is required, but
8
+ # presence is not.
9
+ module Statusable::HasStatuses
10
+ extend ActiveSupport::Concern
11
+
12
+ class_methods do # rubocop:disable Metrics/BlockLength
13
+ # rubocop:disable Metrics/AbcSize
14
+ # rubocop:disable Metrics/MethodLength
15
+ # rubocop:disable Metrics/PerceivedComplexity
16
+ # rubocop:disable Naming/PredicateName
17
+ def has_statuses(
18
+ *args,
19
+ col_name: "status",
20
+ arel_column: nil,
21
+ validate_inclusion: true,
22
+ validate_presence: false)
23
+ statuses = args.flatten.freeze
24
+
25
+ arel_column ||= arel_table[col_name]
26
+ pluralized_column_name = col_name.to_s.pluralize
27
+
28
+ humanized_statuses_list =
29
+ statuses.to_sentence(
30
+ two_words_connector: " or ",
31
+ last_word_connector: ", or ").
32
+ freeze
33
+
34
+ if validate_inclusion
35
+ validates col_name,
36
+ inclusion: {
37
+ in: statuses,
38
+ allow_blank: true,
39
+ message: "must be one of #{humanized_statuses_list}",
40
+ }
41
+ end
42
+
43
+ if validate_presence
44
+ validates col_name,
45
+ presence: true
46
+ end
47
+
48
+ # .for_status(<status>)
49
+ scope "for_#{col_name}",
50
+ ->(status) {
51
+ where(
52
+ if status.is_a?(Array)
53
+ arel_column.in(status)
54
+ else
55
+ arel_column.eq(status)
56
+ end)
57
+ }
58
+
59
+ # .not_for_status(<status>)
60
+ scope "not_for_#{col_name}",
61
+ ->(status) {
62
+ where(
63
+ if status.is_a?(Array)
64
+ arel_column.not_in(status)
65
+ else
66
+ arel_column.not_eq(status)
67
+ end)
68
+ }
69
+
70
+ # .by_status_asc
71
+ scope "by_#{col_name}_asc",
72
+ -> { order(arel_column) }
73
+
74
+ # .by_status_desc
75
+ scope "by_#{col_name}_desc",
76
+ -> { order(arel_column.desc) }
77
+
78
+ # .group_by_status
79
+ scope "group_by_#{col_name}",
80
+ -> { group(arel_column) }
81
+
82
+ # Class method to get a humanized list of statuses.
83
+ # .humanized_statuses_list
84
+ define_singleton_method(:"humanized_#{pluralized_column_name}_list") do
85
+ humanized_statuses_list
86
+ end
87
+
88
+ # Instance method to get a humanized list of statuses.
89
+ # #humanized_statuses_list
90
+ define_method(:"humanized_#{pluralized_column_name}_list") do
91
+ humanized_statuses_list
92
+ end
93
+
94
+ # Class method to get all options for <col_name>.
95
+ # .status_options
96
+ define_singleton_method(:"#{col_name}_options") do
97
+ statuses
98
+ end
99
+
100
+ # Instance method to get all options for <col_name>.
101
+ # #status_options
102
+ define_method(:"#{col_name}_options") do
103
+ statuses
104
+ end
105
+
106
+ statuses.each do |status| # rubocop:disable Metrics/BlockLength
107
+ status_name = status.parameterize.underscore
108
+
109
+ # .for_status_ready
110
+ scope "for_#{col_name}_#{status_name}",
111
+ -> { where(arel_column.eq(status)) }
112
+
113
+ # .not_for_status_ready
114
+ scope "not_for_#{col_name}_#{status_name}",
115
+ -> { where(arel_column.not_eq(status)) }
116
+
117
+ # Class method to get <status_name> value. Obviates the need for
118
+ # defining a constant.
119
+ # .status_ready # => "Ready"
120
+ define_singleton_method(:"#{col_name}_#{status_name}") do
121
+ status
122
+ end
123
+
124
+ # Instance method to get <status_name> value. Obviates the need for
125
+ # defining a constant.
126
+ # #status_ready # => "Ready"
127
+ define_method(:"#{col_name}_#{status_name}") do
128
+ status
129
+ end
130
+
131
+ # @return [self]
132
+ # #set_status_ready
133
+ define_method(:"set_#{col_name}_#{status_name}") do
134
+ public_send(:"#{col_name}=", status)
135
+
136
+ self
137
+ end
138
+
139
+ # #set_status_ready!
140
+ # @return [self]
141
+ define_method(:"set_#{col_name}_#{status_name}!") do
142
+ public_send(:"set_#{col_name}_#{status_name}")
143
+ save!
144
+
145
+ self
146
+ end
147
+
148
+ # #status_ready?
149
+ define_method(:"#{col_name}_#{status_name}?") do
150
+ public_send(col_name) == status
151
+ end
152
+
153
+ # #not_status_ready?
154
+ define_method(:"not_#{col_name}_#{status_name}?") do
155
+ public_send(col_name) != status
156
+ end
157
+
158
+ # #status?(<a_status>)
159
+ define_method(:"#{col_name}?") do |a_status|
160
+ public_send(col_name) == a_status
161
+ end
162
+ end
163
+ end
164
+ # rubocop:enable Naming/PredicateName
165
+ # rubocop:enable Metrics/PerceivedComplexity
166
+ # rubocop:enable Metrics/MethodLength
167
+ # rubocop:enable Metrics/AbcSize
168
+ end
169
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Statusable::Railtie < Rails::Railtie
4
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Statusable
4
+ VERSION = "0.1.0.pre"
5
+ end
data/lib/statusable.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Statusable is the base namespace for all modules/classes related to the
4
+ # statusable gem.
5
+ module Statusable
6
+ end
7
+
8
+ require "statusable/version"
9
+ require "statusable/railtie"
10
+
11
+ require "statusable/has_statuses"
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statusable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Paul DobbinSchmaltz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: gemwork
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: puma
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Statusable adds a `has_statuses` macro for defining common status-related
70
+ methods for use with ActiveRecord objects / Relations.
71
+ email:
72
+ - p.dobbinschmaltz@icloud.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - MIT-LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - lib/statusable.rb
81
+ - lib/statusable/has_statuses.rb
82
+ - lib/statusable/railtie.rb
83
+ - lib/statusable/version.rb
84
+ homepage: https://github.com/pdobb/statusable
85
+ licenses:
86
+ - MIT
87
+ metadata:
88
+ bug_tracker_uri: https://github.com/pdobb/statusable/issues
89
+ changelog_uri: https://github.com/pdobb/statusable/releases
90
+ source_code_uri: https://github.com/pdobb/statusable
91
+ homepage_uri: https://github.com/pdobb/statusable
92
+ rubygems_mfa_required: 'true'
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '2.7'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.5.17
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Adds a `has_statuses` macro for defining common status-related ActiveRecord
112
+ / Relation methods.
113
+ test_files: []