amazing-activist 0.1.0

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: a8cba9d28e25f87ba33a43233f981a13826c716db8342fa1df5a39ed14ca59c1
4
+ data.tar.gz: bcda1343f32f2df0aec0e3aaa806f602b98410e9a54d4a79214859f21830d275
5
+ SHA512:
6
+ metadata.gz: c57c413901c0323f40a153d42a4b22be48281a1f6e76ceb2065cc94aed379a3d69238dfddde3537fa3ded78e573c3c9a168fcd76eb1c2547ed228b3b622f47cd
7
+ data.tar.gz: 9d37d65dc06c8a8784b4bc9ea4d6c1c2d72798cfea742e292e8a4d0847451dc851024a0fef330889d27d56313d944107354dc0a7dd300aed01859bc1d67383b9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Alexey Zapparov
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.adoc ADDED
@@ -0,0 +1,76 @@
1
+ = AmazingActivist
2
+
3
+ == Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ $ bundle add amazing-activist
8
+
9
+ Or install it yourself as:
10
+
11
+ $ gem install amazing-activist
12
+
13
+
14
+ == Usage
15
+
16
+ [source,ruby]
17
+ ----
18
+ class ApplicationActivity < AmazingActivist::Activity
19
+ end
20
+
21
+ class OnboardingActivity < ApplicationActivity
22
+ def call
23
+ user = User.new(params)
24
+
25
+ return failure(:invalid_params, user: user) unless user.save
26
+
27
+ success(user)
28
+ end
29
+ end
30
+
31
+ class UsersController < ApplicationController
32
+ def create
33
+ case OnboardingActivity.call(**params.require(:user).permit(:password))
34
+ in success: user
35
+ redirect_to user_bashboard_url(user)
36
+ in failure: :invalid_params, context: { user: user }
37
+ @user = user
38
+ render :new
39
+ else
40
+ head :bad_request
41
+ end
42
+ end
43
+ end
44
+ ----
45
+
46
+
47
+ == Compatibility
48
+
49
+ This library aims to support and is tested against:
50
+
51
+ * https://www.ruby-lang.org[Ruby]
52
+ ** MRI 3.0.x
53
+ ** MRI 3.1.x
54
+ ** MRI 3.2.x
55
+ ** MRI 3.3.x
56
+
57
+ If something doesn't work on one of these versions, it's a bug.
58
+
59
+ This library may inadvertently work (or seem to work) on other Ruby versions,
60
+ however support will only be provided for the versions listed above.
61
+
62
+
63
+ == Development
64
+
65
+ bundle install
66
+ bundle exec rake
67
+
68
+
69
+ == Contributing
70
+
71
+ * Fork amazing-activist
72
+ * Make your changes
73
+ * Ensure all tests pass (`bundle exec rake`)
74
+ * Send a merge request
75
+ * If we like them we'll merge them
76
+ * If we've accepted a patch, feel free to ask for commit access!
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./amazing_activist"
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./outcome"
4
+
5
+ module AmazingActivist
6
+ # Abstract activity class.
7
+ #
8
+ # == Example
9
+ #
10
+ # [source,ruby]
11
+ # ----
12
+ # class OnboardActivity < AmazingActivist::Activity
13
+ # def call
14
+ # user = User.new(params)
15
+ #
16
+ # return success(user) if user.save
17
+ #
18
+ # failure(:invalid_params, user: user)
19
+ # end
20
+ # end
21
+ # ----
22
+ class Activity
23
+ class << self
24
+ # Convenience method to initialize and immediatelly call the activity.
25
+ # @see #initialize
26
+ # @see #call
27
+ def call(...)
28
+ new(...).call
29
+ end
30
+ end
31
+
32
+ # @param params [Hash{Symbol => Object}]
33
+ def initialize(**params)
34
+ @params = params
35
+ end
36
+
37
+ # @return [Outcome::Success, Outcome::Failure]
38
+ def call
39
+ failure(:not_implemented)
40
+ end
41
+
42
+ private
43
+
44
+ # @return [Hash{Symbol => Object}]
45
+ attr_reader :params
46
+
47
+ # @param value (see Outcome::Success#initialize)
48
+ # @return [Outcome::Success]
49
+ def success(value = nil)
50
+ Outcome::Success.new(value, activity: self)
51
+ end
52
+
53
+ # @param code (see Outcome::Failure#initialize)
54
+ # @param context (see Outcome::Failure#initialize)
55
+ # @return [Outcome::Failure]
56
+ def failure(code, **context)
57
+ Outcome::Failure.new(code, activity: self, context: context)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AmazingActivist
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../unwrap_error"
4
+
5
+ module AmazingActivist
6
+ module Outcome
7
+ class Failure
8
+ # @return [Symbol]
9
+ attr_reader :code
10
+
11
+ # @return [AmazingActivist::Activity]
12
+ attr_reader :activity
13
+
14
+ # @return [Hash{Symbol => Object}]
15
+ attr_reader :context
16
+
17
+ # @param code [#to_sym]
18
+ # @param activity [AmazingActivist::Activity]
19
+ # @param context [Hash{Symbol => Object}]
20
+ def initialize(code, activity:, context:)
21
+ @code = code.to_sym
22
+ @activity = activity
23
+ @context = context
24
+ end
25
+
26
+ # @return [true]
27
+ def success?
28
+ false
29
+ end
30
+
31
+ # @return [false]
32
+ def failure?
33
+ true
34
+ end
35
+
36
+ # @api internal
37
+ # @return [Array<(:failure, Symbol, AmazingActivist::Activity, Hash{Symbol => Object})>]
38
+ def deconstruct
39
+ [:failure, @code, @activity, @context]
40
+ end
41
+
42
+ # @api internal
43
+ # @return [Hash{success: Object, activity: AmazingActivist::Activity}]
44
+ def deconstruct_keys(_)
45
+ { failure: @code, activity: @activity, context: @context }
46
+ end
47
+
48
+ # @yieldparam self [self]
49
+ def value_or
50
+ yield self
51
+ end
52
+
53
+ # @raise [UnwrapError]
54
+ def unwrap!
55
+ raise UnwrapError, self
56
+ end
57
+
58
+ def message
59
+ "#{@activity.class} failed with #{@code}"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AmazingActivist
4
+ module Outcome
5
+ class Success
6
+ # @return [AmazingActivist::Activity]
7
+ attr_reader :activity
8
+
9
+ # @param value [Object]
10
+ # @param activity [AmazingActivist::Activity]
11
+ def initialize(value, activity:)
12
+ @value = value
13
+ @activity = activity
14
+ end
15
+
16
+ # @return [true]
17
+ def success?
18
+ true
19
+ end
20
+
21
+ # @return [false]
22
+ def failure?
23
+ false
24
+ end
25
+
26
+ # @api internal
27
+ # @return [Array<(:success, Object, AmazingActivist::Activity)>]
28
+ def deconstruct
29
+ [:success, @value, @activity]
30
+ end
31
+
32
+ # @api internal
33
+ # @return [Hash{success: Object, activity: AmazingActivist::Activity}]
34
+ def deconstruct_keys(_)
35
+ { success: @value, activity: @activity }
36
+ end
37
+
38
+ # @return [Object]
39
+ def value_or
40
+ @value
41
+ end
42
+
43
+ # @return [Object]
44
+ def unwrap!
45
+ @value
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./outcome/success"
4
+ require_relative "./outcome/failure"
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./error"
4
+
5
+ module AmazingActivist
6
+ class UnwrapError < Error
7
+ # @return [Outcome::Failure]
8
+ attr_reader :failure
9
+
10
+ # @param failure [Outcome::Failure]
11
+ def initialize(failure)
12
+ @failure = failure
13
+
14
+ super(failure.message)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AmazingActivist
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./amazing_activist/activity"
4
+ require_relative "./amazing_activist/error"
5
+ require_relative "./amazing_activist/outcome"
6
+ require_relative "./amazing_activist/version"
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amazing-activist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Zapparov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-01-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Another take on Command Pattern.
14
+ email:
15
+ - alexey@zapparov.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE.txt
21
+ - README.adoc
22
+ - lib/amazing-activist.rb
23
+ - lib/amazing_activist.rb
24
+ - lib/amazing_activist/activity.rb
25
+ - lib/amazing_activist/error.rb
26
+ - lib/amazing_activist/outcome.rb
27
+ - lib/amazing_activist/outcome/failure.rb
28
+ - lib/amazing_activist/outcome/success.rb
29
+ - lib/amazing_activist/unwrap_error.rb
30
+ - lib/amazing_activist/version.rb
31
+ homepage: https://github.com/ixti/amazing-activist
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/ixti/amazing-activist
36
+ source_code_uri: https://github.com/ixti/amazing-activist/tree/v0.1.0
37
+ bug_tracker_uri: https://github.com/ixti/amazing-activist/issues
38
+ changelog_uri: https://github.com/ixti/amazing-activist/blob/v0.1.0/CHANGES.md
39
+ rubygems_mfa_required: 'true'
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.0.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.5.4
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Your friendly neighborhood activist.
59
+ test_files: []