delayed_crud 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bd40962c309572ce01973c6ee3f7ab8c76b5ceb46aa59e8019e8cbf5102493ca
4
+ data.tar.gz: 43902ed47039a512a205236902bec343634d72c7aac9747ad0903efbdb078a76
5
+ SHA512:
6
+ metadata.gz: e6f5927e32e11a6278c328a0b59868672acaff72cd91dfd5b0e1951440851fb065a700a8c9168815ecee1d271f405ff974e4faacaaaef3ce6b53e6fa44c6c24f
7
+ data.tar.gz: ed880f6215e19602ea851d88d929b2f01a61ea057c527d66b7c11e70fda0741168e4b8e84ff345f51e6da9e3663bcfa742e2cd69e25ab84e1f1cc5469f948469
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Metrics/ClassLength:
17
+ Max: 200
18
+
19
+ Metrics/MethodLength:
20
+ Max: 20
21
+
22
+ Metrics/AbcSize:
23
+ Max: 20
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.1] - 2024-02-24
4
+
5
+ - Updaate readme
6
+
7
+ ## [0.1.0] - 2024-02-24
8
+
9
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at slmusayev@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Suleyman Musayev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # DelayedCrud
2
+
3
+ The `delayed_crud` gem simplifies database CRUD operations in your Ruby on Rails applications by utilizing preconfigured Sidekiq background jobs.
4
+
5
+ ## Installation
6
+ To install the gem run the following command in the terminal:
7
+
8
+ $ bundle add delayed_crud
9
+
10
+ If bundler is not being used to manage dependencies, install the gem by executing:
11
+
12
+ $ gem install delayed_crud
13
+
14
+ ## Usage
15
+
16
+ This gem provides three convenient methods for common CRUD operations, each accepting timer arguments with keywords `:in` and `:after`. Please note that the timer starts counting down as soon as the code line is executed. Since the gem relies on `sidekiq`, the execution timing might not be precise.
17
+
18
+ ### delayed_create
19
+ The `delayed_create` method can be used on a model instance and accepts instance attributes in a hash along with a timer argument.
20
+
21
+ ```ruby
22
+ Customer.delayed_create({ status: :new }, in: 10.minutes.from_now)
23
+ ```
24
+
25
+ ### delayed_update
26
+ The `delayed_update` method works on an existing record and accepts update attributes in a hash along with a timer argument.
27
+
28
+ ```ruby
29
+ HeatedDiscussion.delayed_update({ frozen: false }, after: 24.hours)
30
+ ```
31
+
32
+ ### delayed_destroy
33
+ The `delayed_destroy` method can be used on an existing record and accepts only a timer argument. The record will be destroyed once the timer expires.
34
+
35
+ ```ruby
36
+ TrialAccount.delayed_destroy(in: 1.week.from_now)
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ The best way to contribute would be to fork this repo, create a new branch from main, to merge the branch into main of the fork once the new code is in place and then open a pull request to merge forked main into the main of the present repo.
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/msuliq/delayed_crud. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/msuliq/delayed_crud/blob/main/CODE_OF_CONDUCT.md).
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
48
+
49
+ ## Code of Conduct
50
+
51
+ Everyone interacting in the DelayedCrud project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/msuliq/delayed_crud/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/delayed_crud/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "delayed_crud"
7
+ spec.version = DelayedCrud::VERSION
8
+ spec.authors = ["Suleyman Musayev"]
9
+ spec.email = ["slmusayev@gmail.com"]
10
+
11
+ spec.summary = "The `delayed_crud` gem provides method to perform database CRUD
12
+ operations in Ruby on Rails applications as preconfigured `sidekiq` background job."
13
+ spec.description = "This gem provides three convenient methods for common CRUD operations:
14
+ `delayed_create`, `delayed_update` and `delayed_destroy` as preconfigured `sidekiq`
15
+ background job for Ruby on Rails applications."
16
+
17
+ spec.homepage = "https://github.com/msuliq/delayed_crud"
18
+ spec.license = "MIT"
19
+ spec.required_ruby_version = ">= 3.0.0"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/msuliq/delayed_crud"
23
+ spec.metadata["changelog_uri"] = "https://github.com/msuliq/delayed_crud/blob/main/CHANGELOG.md"
24
+ spec.metadata["rubygems_mfa_required"] = "true"
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(__dir__) do
29
+ `git ls-files -z`.split("\x0").reject do |f|
30
+ (File.expand_path(f) == __FILE__) ||
31
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
32
+ end
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ # Uncomment to register a new dependency of your gem
39
+ spec.add_dependency "rails", "~> 5.0"
40
+ spec.add_dependency "sidekiq", "~> 2.15"
41
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delayed_crud/worker/sidekiq_worker"
4
+
5
+ module DelayedCrud
6
+ module BackgroundJobHandler
7
+ module SidekiqHandler # :nodoc:
8
+ def self.delayed_create(record, attributes, options)
9
+ DelayedCrud::Worker::SidekiqWorker.perform_with_delay(operation: :create, record: record,
10
+ attributes: attributes, options: options)
11
+ end
12
+
13
+ def self.delayed_update(record, attributes, options)
14
+ DelayedCrud::Worker::SidekiqWorker.perform_with_delay(operation: :update, record: record,
15
+ attributes: attributes, options: options)
16
+ end
17
+
18
+ def self.delayed_destroy(record, options)
19
+ DelayedCrud::Worker::SidekiqWorker.perform_with_delay(operation: :destroy, record: record, options: options)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dir[File.expand_path("background_job_handler/*.rb", __dir__)].sort.each do |file|
4
+ require_relative file
5
+ end
6
+
7
+ module DelayedCrud
8
+ module BackgroundJobHandler # :nodoc:
9
+ module_function
10
+
11
+ def delayed_create(record, attributes, options = {})
12
+ handler_instance.delayed_create(record, attributes, options)
13
+ end
14
+
15
+ def delayed_update(record, attributes, options = {})
16
+ handler_instance.delayed_update(record, attributes, options)
17
+ end
18
+
19
+ def delayed_destroy(record, options = {})
20
+ handler_instance.delayed_destroy(record, options)
21
+ end
22
+
23
+ def handler_instance
24
+ handler_class = "#{DelayedCrud.queue_adapter.to_s.capitalize}Handler"
25
+ DelayedCrud::BackgroundJobHandler.const_get(handler_class)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq"
4
+
5
+ module DelayedCrud
6
+ module Job # :nodoc:
7
+ module SidekiqJob # :nodoc:
8
+ class DelayedCrudJob # :nodoc:
9
+ include Sidekiq::Worker
10
+
11
+ sidekiq_options queue: :default, tags: ["delayed_crud_job"]
12
+
13
+ def perform(class_name, record_id, operation, attributes = {})
14
+ record = record_id ? class_name.constantize.find(record_id) : class_name.constantize
15
+
16
+ case operation.to_sym
17
+ when :create, :update
18
+ record.send(operation, attributes)
19
+ when :destroy
20
+ record.send(operation)
21
+ else
22
+ raise ArgumentError, "Unsupported operation: #{operation}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delayed_crud/background_job_handler"
4
+
5
+ module DelayedCrud
6
+ module ModelExtension # :nodoc:
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ def delayed_create(attributes, options = {})
11
+ DelayedCrud::BackgroundJobHandler.delayed_create(new, attributes, options)
12
+ end
13
+ end
14
+
15
+ def delayed_update(options = {})
16
+ background_job_handler.delayed_update(self, options)
17
+ end
18
+
19
+ def delayed_destroy(options = {})
20
+ background_job_handler.delayed_destroy(self, options)
21
+ end
22
+
23
+ private
24
+
25
+ def background_job_handler
26
+ @background_job_handler ||= DelayedCrud::BackgroundJobHandler
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delayed_crud/model_extension"
4
+
5
+ module DelayedCrud
6
+ class Railtie < Rails::Railtie # :nodoc:
7
+ # Include the model extension into ActiveRecord::Base
8
+ ActiveSupport.on_load(:active_record) do
9
+ ActiveRecord::Base.include DelayedCrud::ModelExtension
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DelayedCrud
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sidekiq"
4
+ require "delayed_crud/job/sidekiq_job"
5
+
6
+ module DelayedCrud
7
+ module Worker
8
+ module SidekiqWorker # :nodoc:
9
+ def self.perform_with_delay(operation:, record:, attributes: {}, options: {})
10
+ raise ArgumentError, "Missing operation for a delayed transaction" if operation.nil?
11
+
12
+ delay = DelayedCrud.calculate_delay(options[:in] || options[:after])
13
+
14
+ record_id = record.try(:id)
15
+ serialized_attributes = attributes.transform_keys(&:to_s)
16
+ class_name = record_id ? record.class.name : record.name
17
+
18
+ Sidekiq::Client.enqueue_in(delay.seconds, DelayedCrud::Job::SidekiqJob::DelayedCrudJob, class_name, record_id,
19
+ operation.to_s, serialized_attributes)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delayed_crud/worker/sidekiq_worker"
4
+
5
+ module DelayedCrud
6
+ module Worker; end # :nodoc:
7
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delayed_crud/version"
4
+ require "delayed_crud/railtie" if defined?(Rails::Railtie)
5
+
6
+ module DelayedCrud # :nodoc:
7
+ class << self
8
+ attr_accessor :queue_adapter
9
+ end
10
+
11
+ def self.configure
12
+ yield self
13
+ end
14
+
15
+ def self.setup
16
+ return if queue_adapter
17
+
18
+ self.queue_adapter = :sidekiq
19
+ end
20
+
21
+ def self.calculate_delay(time_specification)
22
+ raise ArgumentError, "Cannot perform transaction without time specification" unless time_specification
23
+
24
+ if time_specification.respond_to?(:from_now)
25
+ delay = (time_specification.from_now - Time.now).to_i
26
+ elsif time_specification.respond_to?(:to_i)
27
+ delay = (time_specification - Time.now).to_i
28
+ else
29
+ raise ArgumentError, "Invalid time specification: #{time_specification}"
30
+ end
31
+
32
+ unless delay.positive?
33
+ raise ArgumentError,
34
+ "Cannot perform #{operation} with negative time specification: #{options[:in] || options[:after]}"
35
+ end
36
+
37
+ delay
38
+ end
39
+
40
+ class Error < StandardError; end
41
+ end
42
+
43
+ DelayedCrud.setup
@@ -0,0 +1,4 @@
1
+ module DelayedCrud
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_crud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Suleyman Musayev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-24 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: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.15'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.15'
41
+ description: |-
42
+ This gem provides three convenient methods for common CRUD operations:
43
+ `delayed_create`, `delayed_update` and `delayed_destroy` as preconfigured `sidekiq`
44
+ background job for Ruby on Rails applications.
45
+ email:
46
+ - slmusayev@gmail.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".rubocop.yml"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - delayed_crud.gemspec
58
+ - lib/delayed_crud.rb
59
+ - lib/delayed_crud/background_job_handler.rb
60
+ - lib/delayed_crud/background_job_handler/sidekiq_handler.rb
61
+ - lib/delayed_crud/job/sidekiq_job.rb
62
+ - lib/delayed_crud/model_extension.rb
63
+ - lib/delayed_crud/railtie.rb
64
+ - lib/delayed_crud/version.rb
65
+ - lib/delayed_crud/worker.rb
66
+ - lib/delayed_crud/worker/sidekiq_worker.rb
67
+ - sig/delayed_crud.rbs
68
+ homepage: https://github.com/msuliq/delayed_crud
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ homepage_uri: https://github.com/msuliq/delayed_crud
73
+ source_code_uri: https://github.com/msuliq/delayed_crud
74
+ changelog_uri: https://github.com/msuliq/delayed_crud/blob/main/CHANGELOG.md
75
+ rubygems_mfa_required: 'true'
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 3.0.0
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.5.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: The `delayed_crud` gem provides method to perform database CRUD operations
95
+ in Ruby on Rails applications as preconfigured `sidekiq` background job.
96
+ test_files: []