action_man 0.0.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: 58b0715b75d357e16c37d7330d31a8350547f60e3d4f978f0ae2523b9416b3d3
4
+ data.tar.gz: 10115fc4353cacb4010a4598a4a3235cf9c6fdff234d609fc1181457bdc7a803
5
+ SHA512:
6
+ metadata.gz: 4d16a79aac084ca498070fcec78bfc8500bb2b627d6cf060056d0e3024433b4f0b6622ce64e8f26c9f62352f8b93eb6bd9ce72a45af53146e5b80dcc32d37a20
7
+ data.tar.gz: ec417a86bf70edb7a154696750220a6ab86244cd3ec1a9a823a2d6547ebadb65c9d6d24aac02d6d9eb3edda6e7e65bab93a29fbfb88f671014f603ab8ecb5ac4
data/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ # This file is for unifying the coding style for different editors and IDEs
2
+ # editorconfig.org
3
+
4
+ root = true
5
+
6
+ [*]
7
+ charset = utf-8
8
+ indent_size = 2
9
+ indent_style = space
10
+ insert_final_newline = true
11
+ max_line_length = 120
12
+ trim_trailing_whitespace = true
data/.envrc ADDED
@@ -0,0 +1 @@
1
+ PATH_add bin
data/.rubocop.yml ADDED
@@ -0,0 +1,55 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+
5
+ inherit_mode:
6
+ merge:
7
+ - Exclude
8
+ - Include
9
+ - Prefixes
10
+
11
+ AllCops:
12
+ DisplayCopNames: true
13
+ Exclude:
14
+ - "**/bin/**/*"
15
+ - "**/gemfiles/**/*"
16
+ - "**/Appraisals"
17
+ NewCops: enable
18
+ TargetRubyVersion: 3.1
19
+
20
+ Layout/IndentationConsistency:
21
+ EnforcedStyle: indented_internal_methods
22
+ Layout/SpaceAroundEqualsInParameterDefault:
23
+ EnforcedStyle: no_space
24
+
25
+ Metrics/ClassLength:
26
+ Enabled: false
27
+ CountAsOne:
28
+ - array
29
+ - hash
30
+ - heredoc
31
+ Metrics/MethodLength:
32
+ CountAsOne:
33
+ - array
34
+ - hash
35
+ - heredoc
36
+ Metrics/ModuleLength:
37
+ CountAsOne:
38
+ - array
39
+ - hash
40
+ - heredoc
41
+
42
+ Minitest/MultipleAssertions:
43
+ Max: 5
44
+
45
+ Style/Documentation:
46
+ Enabled: false
47
+ Style/StringLiterals:
48
+ Enabled: true
49
+ EnforcedStyle: double_quotes
50
+ Style/StringLiteralsInInterpolation:
51
+ Enabled: true
52
+ EnforcedStyle: double_quotes
53
+
54
+ Layout/LineLength:
55
+ Max: 120
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.2.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.0.1] - 2023-10-10
4
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "activesupport"
8
+ gem "minitest"
9
+ gem "rake"
10
+ gem "rubocop"
11
+ gem "rubocop-minitest"
12
+ gem "rubocop-rake"
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ action_man (0.0.1)
5
+ activesupport (>= 6.1, < 8.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (7.1.0)
11
+ base64
12
+ bigdecimal
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ connection_pool (>= 2.2.5)
15
+ drb
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ mutex_m
19
+ tzinfo (~> 2.0)
20
+ ast (2.4.2)
21
+ base64 (0.1.1)
22
+ bigdecimal (3.1.4)
23
+ concurrent-ruby (1.2.2)
24
+ connection_pool (2.4.1)
25
+ drb (2.1.1)
26
+ ruby2_keywords
27
+ i18n (1.14.1)
28
+ concurrent-ruby (~> 1.0)
29
+ json (2.6.3)
30
+ language_server-protocol (3.17.0.3)
31
+ minitest (5.20.0)
32
+ mutex_m (0.1.2)
33
+ parallel (1.23.0)
34
+ parser (3.2.2.4)
35
+ ast (~> 2.4.1)
36
+ racc
37
+ racc (1.7.1)
38
+ rainbow (3.1.1)
39
+ rake (13.0.6)
40
+ regexp_parser (2.8.1)
41
+ rexml (3.2.6)
42
+ rubocop (1.56.4)
43
+ base64 (~> 0.1.1)
44
+ json (~> 2.3)
45
+ language_server-protocol (>= 3.17.0)
46
+ parallel (~> 1.10)
47
+ parser (>= 3.2.2.3)
48
+ rainbow (>= 2.2.2, < 4.0)
49
+ regexp_parser (>= 1.8, < 3.0)
50
+ rexml (>= 3.2.5, < 4.0)
51
+ rubocop-ast (>= 1.28.1, < 2.0)
52
+ ruby-progressbar (~> 1.7)
53
+ unicode-display_width (>= 2.4.0, < 3.0)
54
+ rubocop-ast (1.29.0)
55
+ parser (>= 3.2.1.0)
56
+ rubocop-minitest (0.32.2)
57
+ rubocop (>= 1.39, < 2.0)
58
+ rubocop-rake (0.6.0)
59
+ rubocop (~> 1.0)
60
+ ruby-progressbar (1.13.0)
61
+ ruby2_keywords (0.0.5)
62
+ tzinfo (2.0.6)
63
+ concurrent-ruby (~> 1.0)
64
+ unicode-display_width (2.5.0)
65
+
66
+ PLATFORMS
67
+ arm64-darwin-22
68
+ x86_64-linux
69
+
70
+ DEPENDENCIES
71
+ action_man!
72
+ activesupport
73
+ minitest
74
+ rake
75
+ rubocop
76
+ rubocop-minitest
77
+ rubocop-rake
78
+
79
+ BUNDLED WITH
80
+ 2.4.20
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Javier Aranda
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,40 @@
1
+ # ActionMan
2
+
3
+ ![CI](https://github.com/javierav/action_man/workflows/CI/badge.svg)
4
+
5
+ Simple actions for your models
6
+
7
+ ## Status
8
+
9
+ > :warning: **This project is still experimental, use with caution!**
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "action_man"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```shell
22
+ bundle install
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
30
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`.
33
+
34
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
35
+ which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to
36
+ [rubygems.org](https://rubygems.org).
37
+
38
+ ## License
39
+
40
+ Copyright © 2023 Javier Aranda. Released under the terms of the [MIT license](LICENSE).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/callbacks"
4
+ require "active_support/core_ext/array/wrap"
5
+ require_relative "not_executable"
6
+ require_relative "result"
7
+
8
+ module ActionMan
9
+ class Base
10
+ include ActiveSupport::Callbacks
11
+
12
+ RESULT_TYPES = %i[success failure].freeze
13
+
14
+ class_attribute :transaction, default: true
15
+ class_attribute :exception_if_not_executable, default: false
16
+
17
+ attr_reader :model, :args, :params, :result
18
+
19
+ define_callbacks :execute
20
+
21
+ class << self
22
+ def before(...)
23
+ set_callback(:execute, :before, ...)
24
+ end
25
+
26
+ def after(*filters, &)
27
+ set_options_for_callbacks!(filters)
28
+ set_callback(:execute, :after, *filters, &)
29
+ end
30
+
31
+ def after_success(*filters, &)
32
+ set_options_for_callbacks!(filters, on: :success)
33
+ set_callback(:execute, :after, *filters, &)
34
+ end
35
+
36
+ def after_failure(*filters, &)
37
+ set_options_for_callbacks!(filters, on: :failure)
38
+ set_callback(:execute, :after, *filters, &)
39
+ end
40
+
41
+ def param(name)
42
+ define_method(name) do
43
+ params[name]
44
+ end
45
+ end
46
+
47
+ def params(*names)
48
+ names.each { |name| param(name) }
49
+ end
50
+
51
+ def model(name)
52
+ define_method(name) do
53
+ model
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def set_options_for_callbacks!(args, enforced_options={})
60
+ options = args.extract_options!.merge!(enforced_options)
61
+ args << options
62
+
63
+ return unless options[:on]
64
+
65
+ fire_on = Array.wrap(options[:on])
66
+ assert_valid_execute_on(fire_on)
67
+ options[:if] = [
68
+ -> { fire_on.any? { |on| on.to_sym == result.status.to_sym } },
69
+ *options[:if]
70
+ ]
71
+ end
72
+
73
+ def assert_valid_execute_on(actions)
74
+ return unless (actions - RESULT_TYPES).any?
75
+
76
+ raise ArgumentError, ":on conditions for after callbacks have to be one of #{RESULT_TYPES}"
77
+ end
78
+ end
79
+
80
+ def initialize(model)
81
+ @model = model
82
+ @params = {}
83
+ end
84
+
85
+ def executable?
86
+ true
87
+ end
88
+
89
+ def run(params={})
90
+ @params = params
91
+
92
+ if executable?
93
+ run_execute
94
+ elsif exception_if_not_executable
95
+ raise NotExecutable
96
+ else
97
+ Result.failure
98
+ end
99
+ end
100
+
101
+ private
102
+
103
+ def execute
104
+ raise NotImplementedError
105
+ end
106
+
107
+ def success!(params={})
108
+ throw :finish, Result.success(params:)
109
+ end
110
+
111
+ def failure!(errors={})
112
+ throw :finish, Result.failure(errors:)
113
+ end
114
+
115
+ def finish!(value)
116
+ value ? success! : failure!
117
+ end
118
+
119
+ def run_execute
120
+ with_transaction do
121
+ run_callbacks(:execute) do
122
+ run_with_catch
123
+ end
124
+ end
125
+
126
+ result
127
+ end
128
+
129
+ def with_transaction(&block)
130
+ if transaction && defined?(ActiveRecord::Base)
131
+ ActiveRecord::Base.transaction do
132
+ block.call
133
+
134
+ raise ActiveRecord::Rollback if result.failure?
135
+ end
136
+ else
137
+ block.call
138
+ end
139
+ end
140
+
141
+ def run_with_catch
142
+ response = catch :finish do
143
+ execute
144
+ end
145
+
146
+ @result = response.is_a?(Result) ? response : Result.success(output: response)
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMan
4
+ module Model
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def action(action_name, class_name=nil)
9
+ define_method(action_name) do |params|
10
+ (class_name || self.class.action_class(action_name)).constantize.new(self).run(params)
11
+ end
12
+
13
+ define_method("#{action_name}?") do
14
+ (class_name || self.class.action_class(action_name)).constantize.new(self).executable?
15
+ end
16
+ end
17
+
18
+ def action_class(action_name)
19
+ "#{name.pluralize}::#{action_name.to_s.camelcase}Action"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMan
4
+ class NotExecutable < StandardError
5
+ end
6
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMan
4
+ class Result
5
+ attr_reader :status, :params, :errors, :output
6
+
7
+ class << self
8
+ def success(params: {}, output: nil)
9
+ new(:success, params:, output:)
10
+ end
11
+
12
+ def failure(errors: {}, output: nil)
13
+ new(:failure, errors:, output:)
14
+ end
15
+ end
16
+
17
+ def initialize(status, params: {}, errors: {}, output: nil)
18
+ @status = status.to_sym
19
+ @params = params
20
+ @errors = errors
21
+ @output = output
22
+ end
23
+
24
+ def success?
25
+ @status == :success
26
+ end
27
+
28
+ def failure?
29
+ @status == :failure
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionMan
4
+ module VERSION
5
+ MAJOR = 0
6
+ MINOR = 0
7
+ TINY = 1
8
+ PRE = nil
9
+
10
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
11
+ end
12
+
13
+ def self.version
14
+ VERSION::STRING
15
+ end
16
+
17
+ def self.gem_version
18
+ Gem::Version.new VERSION::STRING
19
+ end
20
+ end
data/lib/action_man.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "action_man/base"
4
+ require_relative "action_man/model"
5
+ require_relative "action_man/version"
6
+
7
+ module ActionMan
8
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_man
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Javier Aranda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '6.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8.0'
33
+ description:
34
+ email:
35
+ - aranda@hey.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.md
41
+ files:
42
+ - ".editorconfig"
43
+ - ".envrc"
44
+ - ".rubocop.yml"
45
+ - ".tool-versions"
46
+ - CHANGELOG.md
47
+ - Gemfile
48
+ - Gemfile.lock
49
+ - LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - lib/action_man.rb
53
+ - lib/action_man/base.rb
54
+ - lib/action_man/model.rb
55
+ - lib/action_man/not_executable.rb
56
+ - lib/action_man/result.rb
57
+ - lib/action_man/version.rb
58
+ homepage: https://github.com/javierav/action_man
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ homepage_uri: https://github.com/javierav/action_man
63
+ source_code_uri: https://github.com/javierav/action_man/tree/v0.0.1
64
+ changelog_uri: https://github.com/javierav/action_man/blob/v0.0.1/CHANGELOG.md
65
+ rubygems_mfa_required: 'true'
66
+ post_install_message:
67
+ rdoc_options:
68
+ - "--charset=UTF-8"
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3.1'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.4.10
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Simple actions for your models
86
+ test_files: []