literal_enum 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4da670aa086260311dedd8f84f66850b957cd442b66a41ffb4cb2f81f63abf2e
4
+ data.tar.gz: 45c944babb48f43b0d7baafffd349da06989f9f369a1a46467d049088a48cfd0
5
+ SHA512:
6
+ metadata.gz: c66cef636f7d009620f57684135a872789896d8261b2a908fda73480e42b55ba3d4b3b3d5e3cd4aeed1789b542e98fba0e040ba573422d7b738901450d5f8124
7
+ data.tar.gz: 5074c13457bb6e00b31e0a409135bd9d235a55864af5b7e6d5902763de0d695845b8322e2b497592005becc1230aaecafbc7c3e9fdc18c772b1588720dfb66fb
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright letItCurl
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,48 @@
1
+ # LiteralEnum
2
+ Stop using integers in your database when using enums. Use the enum type define in your model for better DX.
3
+
4
+ ## Usage
5
+
6
+ The following `class_method` is added to your arsenal and now `["pending", "accepted"]` are saved as strings in your database.
7
+
8
+ ```ruby
9
+ class Article < ApplicationRecord
10
+ # @NOTE:
11
+ # The column `status` is supposed to be of type string in your database.
12
+ literal_enum :status, ["pending", "accepted"]
13
+ end
14
+ ```
15
+
16
+ ## Installation
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem "literal_enum"
21
+ ```
22
+
23
+ And then execute:
24
+ ```bash
25
+ $ bundle
26
+ ```
27
+
28
+ Or install it yourself as:
29
+ ```bash
30
+ $ gem install literal_enum
31
+ ```
32
+
33
+ ## Contributing
34
+ Open a PR ;)
35
+
36
+ ## License
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ ## Special thanks:
40
+
41
+ Huge shout-out to [hundredwatt](https://github.com/hundredwatt)!
42
+
43
+ I was tired of this integer in the database when using enums, and he jumped into the discussion on Twitter and showed me this:
44
+ - [https://gist.github.com/hundredwatt/9742581230a320eaac008ef01e90f156](https://gist.github.com/hundredwatt/9742581230a320eaac008ef01e90f156)
45
+
46
+ After learning about Rails engines, I thought that this could be a great first gem ;)
47
+
48
+ The original `class_method` is actually from the gist by [@hundredwatt](https://gist.github.com/hundredwatt/9742581230a320eaac008ef01e90f156).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/literal_enum .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module LiteralEnum
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module LiteralEnum
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module LiteralEnum
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module LiteralEnum
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ module LiteralEnum::Binder
2
+ extend ActiveSupport::Concern
3
+
4
+ class_methods do
5
+ def literal_enum(name = nil, values = nil, **options)
6
+ if values.is_a?(Array)
7
+ values = values.map { |value| [value] * 2 }.to_h
8
+ end
9
+
10
+ enum(name, values, **options)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module LiteralEnum
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Literal enum</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "literal_enum/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ LiteralEnum::Engine.routes.draw do
2
+ end
@@ -0,0 +1,11 @@
1
+ module LiteralEnum
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace LiteralEnum
4
+
5
+ initializer "literal_enum.active_record" do
6
+ ActiveSupport.on_load :active_record do
7
+ include LiteralEnum::Binder
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module LiteralEnum
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "literal_enum/version"
2
+ require "literal_enum/engine"
3
+
4
+ module LiteralEnum
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :literal_enum do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: literal_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - letItCurl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-05-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: 7.1.3.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.1.3.2
27
+ description:
28
+ email:
29
+ - rolandlopez.developer@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/config/literal_enum_manifest.js
38
+ - app/assets/stylesheets/literal_enum/application.css
39
+ - app/controllers/literal_enum/application_controller.rb
40
+ - app/helpers/literal_enum/application_helper.rb
41
+ - app/jobs/literal_enum/application_job.rb
42
+ - app/mailers/literal_enum/application_mailer.rb
43
+ - app/models/concerns/literal_enum/binder.rb
44
+ - app/models/literal_enum/application_record.rb
45
+ - app/views/layouts/literal_enum/application.html.erb
46
+ - config/routes.rb
47
+ - lib/literal_enum.rb
48
+ - lib/literal_enum/engine.rb
49
+ - lib/literal_enum/version.rb
50
+ - lib/tasks/literal_enum_tasks.rake
51
+ homepage:
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.4.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Stop using integers in your database when using enums. Use the enum type
74
+ define in your model for better DX.
75
+ test_files: []