plutonium 0.14.1 → 0.15.0.pre.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README copy.md +1 -1
- data/README.md +1 -1
- data/app/assets/plutonium.css +1 -1
- data/app/views/{application → plutonium}/_resource_header.html copy.erb +1 -1
- data/app/views/{application → plutonium}/_resource_header.html.erb +1 -1
- data/app/views/{application → plutonium}/_resource_sidebar.html.erb +2 -0
- data/app/views/resource/_resource_details.html.erb +1 -36
- data/app/views/resource/_resource_form.html.erb +1 -5
- data/app/views/resource/_resource_table.html.erb +315 -85
- data/app/views/resource/edit.html.erb +1 -5
- data/app/views/resource/index.html.erb +1 -5
- data/app/views/resource/new.html.erb +1 -5
- data/app/views/resource/show.html.erb +1 -5
- data/config/initializers/pagy.rb +1 -0
- data/config/initializers/rabl.rb +27 -20
- data/gemfiles/rails_7.gemfile.lock +5 -1
- data/lib/generators/pu/core/install/templates/app/controllers/plutonium_controller.rb.tt +2 -0
- data/lib/generators/pu/core/install/templates/app/controllers/resource_controller.rb.tt +21 -1
- data/lib/generators/pu/core/install/templates/app/definitions/resource_definition.rb.tt +2 -0
- data/lib/generators/pu/core/install/templates/app/models/resource_record.rb.tt +0 -2
- data/lib/generators/pu/core/install/templates/config/initializers/plutonium.rb +1 -8
- data/lib/generators/pu/eject/shell/shell_generator.rb +2 -2
- data/lib/generators/pu/lib/plutonium_generators/concerns/actions.rb +1 -1
- data/lib/generators/pu/lib/plutonium_generators/concerns/logger.rb +1 -1
- data/lib/generators/pu/lib/plutonium_generators/generator.rb +5 -3
- data/lib/generators/pu/lib/plutonium_generators/model_generator_base.rb +26 -2
- data/lib/generators/pu/pkg/{feature/feature_generator.rb → package/package_generator.rb} +4 -4
- data/lib/generators/pu/pkg/{feature → package}/templates/app/controllers/resource_controller.rb.tt +0 -2
- data/lib/generators/pu/pkg/package/templates/app/definitions/resource_definition.rb.tt +4 -0
- data/lib/generators/pu/pkg/package/templates/app/query_objects/resource_query_object.rb.tt +4 -0
- data/lib/generators/pu/pkg/{app/app_generator.rb → portal/portal_generator.rb} +10 -8
- data/lib/generators/pu/pkg/{app → portal}/templates/app/controllers/concerns/controller.rb.tt +3 -7
- data/lib/generators/pu/pkg/{app → portal}/templates/app/controllers/dashboard_controller.rb.tt +1 -1
- data/lib/generators/pu/pkg/portal/templates/app/controllers/plutonium_controller.rb.tt +5 -0
- data/lib/generators/pu/pkg/{app/templates/app/controllers/controller.rb.tt → portal/templates/app/controllers/resource_controller.rb.tt} +1 -1
- data/lib/generators/pu/pkg/portal/templates/app/definitions/resource_definition.rb.tt +4 -0
- data/lib/generators/pu/pkg/{app → portal}/templates/app/views/package/dashboard/index.html.erb +2 -1
- data/lib/generators/pu/res/conn/conn_generator.rb +78 -3
- data/lib/generators/pu/res/conn/templates/app/controllers/resource_controller.rb.tt +1 -1
- data/lib/generators/pu/res/conn/templates/app/definitions/resource_definition.rb.tt +3 -0
- data/lib/generators/pu/res/conn/templates/app/policies/resource_policy.rb.tt +29 -1
- data/lib/generators/pu/res/conn/templates/app/presenters/resource_presenter.rb.tt +1 -1
- data/lib/generators/pu/res/conn/templates/app/query_objects/resource_query_object.rb.tt +1 -1
- data/lib/generators/pu/res/model/model_generator.rb +0 -7
- data/lib/generators/pu/res/model/templates/model.rb.tt +4 -1
- data/lib/generators/pu/res/scaffold/scaffold_generator.rb +22 -4
- data/lib/generators/pu/res/scaffold/templates/controller.rb.tt +0 -1
- data/lib/generators/pu/res/scaffold/templates/definition.rb.tt +4 -0
- data/lib/generators/pu/res/scaffold/templates/policy.rb.tt +2 -2
- data/lib/generators/pu/rodauth/templates/app/controllers/rodauth_controller.rb.tt +1 -1
- data/lib/generators/pu/rodauth/templates/app/rodauth/account_rodauth_plugin.rb.tt +270 -0
- data/lib/plutonium/action/README.md +0 -0
- data/lib/plutonium/action/base.rb +103 -0
- data/lib/plutonium/action/interactive.rb +117 -0
- data/lib/plutonium/action/route_options.rb +65 -0
- data/lib/plutonium/action/simple.rb +8 -0
- data/lib/plutonium/auth.rb +1 -1
- data/lib/plutonium/configuration.rb +0 -8
- data/lib/plutonium/core/actions/collection.rb +1 -1
- data/lib/plutonium/core/associations/renderers/factory.rb +3 -1
- data/lib/plutonium/core/controller.rb +110 -0
- data/lib/plutonium/core/controllers/authorizable.rb +12 -35
- data/lib/plutonium/core/controllers/bootable.rb +38 -7
- data/lib/plutonium/core/controllers/entity_scoping.rb +6 -2
- data/lib/plutonium/core/fields/renderers/association_renderer.rb +1 -1
- data/lib/plutonium/core/ui/collection.rb +1 -1
- data/lib/plutonium/core/ui/detail.rb +1 -1
- data/lib/plutonium/core/ui/form.rb +1 -1
- data/lib/plutonium/definition/actions.rb +50 -0
- data/lib/plutonium/definition/base.rb +92 -0
- data/lib/plutonium/definition/config_attr.rb +30 -0
- data/lib/plutonium/definition/defineable_props.rb +96 -0
- data/lib/plutonium/definition/search.rb +21 -0
- data/lib/plutonium/engine/validator.rb +30 -0
- data/lib/plutonium/engine.rb +25 -0
- data/lib/plutonium/helpers/form_helper.rb +1 -3
- data/lib/plutonium/interaction/README.md +369 -0
- data/lib/plutonium/interaction/base.rb +75 -0
- data/lib/plutonium/interaction/concerns/presentable.rb +61 -0
- data/lib/plutonium/interaction/concerns/workflow_dsl.rb +82 -0
- data/lib/plutonium/interaction/outcome.rb +129 -0
- data/lib/plutonium/interaction/response/base.rb +63 -0
- data/lib/plutonium/interaction/response/null.rb +33 -0
- data/lib/plutonium/interaction/response/redirect.rb +30 -0
- data/lib/plutonium/interaction/response/render.rb +28 -0
- data/lib/plutonium/lib/bit_flags.rb +70 -9
- data/lib/plutonium/{config → lib}/overlayed_hash.rb +1 -1
- data/lib/plutonium/lib/smart_cache.rb +171 -0
- data/lib/plutonium/models/has_cents.rb +170 -0
- data/lib/plutonium/{pkg/base.rb → package/engine.rb} +10 -2
- data/lib/plutonium/{application → portal}/controller.rb +3 -11
- data/lib/plutonium/{application → portal}/dynamic_controllers.rb +4 -4
- data/lib/plutonium/portal/engine.rb +15 -0
- data/lib/plutonium/railtie.rb +33 -1
- data/lib/plutonium/reloader.rb +5 -5
- data/lib/plutonium/resource/controller.rb +51 -34
- data/lib/plutonium/resource/controllers/authorizable.rb +128 -0
- data/lib/plutonium/{core → resource}/controllers/crud_actions.rb +23 -22
- data/lib/plutonium/resource/controllers/defineable.rb +26 -0
- data/lib/plutonium/{core → resource}/controllers/interactive_actions.rb +12 -12
- data/lib/plutonium/resource/controllers/presentable.rb +41 -0
- data/lib/plutonium/resource/controllers/queryable.rb +44 -0
- data/lib/plutonium/resource/definition.rb +6 -0
- data/lib/plutonium/resource/policy.rb +25 -13
- data/lib/plutonium/resource/query_object.rb +50 -51
- data/lib/plutonium/resource/record.rb +6 -89
- data/lib/plutonium/resource/register.rb +82 -0
- data/lib/plutonium/routing/mapper_extensions.rb +1 -1
- data/lib/plutonium/routing/resource_registration.rb +1 -1
- data/lib/plutonium/routing/route_set_extensions.rb +6 -18
- data/lib/plutonium/ui/action_button.rb +125 -0
- data/lib/plutonium/ui/breadcrumbs.rb +163 -0
- data/lib/plutonium/ui/component/base.rb +13 -0
- data/lib/plutonium/ui/component/behaviour.rb +38 -0
- data/lib/plutonium/ui/component/kit.rb +31 -0
- data/lib/plutonium/ui/component/methods.rb +54 -0
- data/lib/plutonium/ui/display/base.rb +25 -0
- data/lib/plutonium/ui/display/component/association.rb +26 -0
- data/lib/plutonium/ui/display/resource.rb +77 -0
- data/lib/plutonium/ui/display/theme.rb +27 -0
- data/lib/plutonium/ui/dyna_frame/content.rb +20 -0
- data/lib/plutonium/ui/empty_card.rb +20 -0
- data/lib/plutonium/ui/form/base.rb +37 -0
- data/lib/plutonium/ui/form/resource.rb +75 -0
- data/lib/plutonium/ui/form/theme.rb +42 -0
- data/lib/plutonium/ui/page/base.rb +112 -0
- data/lib/plutonium/ui/page/edit.rb +23 -0
- data/lib/plutonium/ui/page/index.rb +27 -0
- data/lib/plutonium/ui/page/new.rb +23 -0
- data/lib/plutonium/ui/page/show.rb +27 -0
- data/lib/plutonium/ui/page_header.rb +49 -0
- data/lib/plutonium/ui/table/base.rb +13 -0
- data/lib/plutonium/ui/table/components/pagy_info.rb +70 -0
- data/lib/plutonium/ui/table/components/pagy_page_info.rb +70 -0
- data/lib/plutonium/ui/table/components/pagy_pagination.rb +105 -0
- data/lib/plutonium/ui/table/components/scopes_bar.rb +136 -0
- data/lib/plutonium/ui/table/components/search_bar.rb +158 -0
- data/lib/plutonium/ui/table/display_theme.rb +21 -0
- data/lib/plutonium/ui/table/resource.rb +98 -0
- data/lib/plutonium/ui/table/theme.rb +35 -0
- data/lib/plutonium/ui.rb +9 -0
- data/lib/plutonium/version.rb +5 -1
- data/lib/plutonium.rb +14 -1
- data/package-lock.json +19 -22
- data/package.json +4 -4
- data/src/css/plutonium.css +15 -0
- data/tailwind.options.js +11 -3
- metadata +218 -81
- data/lib/generators/pu/core/install/templates/app/presenters/resource_presenter.rb.tt +0 -2
- data/lib/generators/pu/core/install/templates/app/query_objects/resource_query_object.rb.tt +0 -2
- data/lib/generators/pu/pkg/feature/templates/app/query_objects/resource_query_object.rb.tt +0 -4
- data/lib/plutonium/concerns/resource_validatable.rb +0 -34
- data/lib/plutonium/config.rb +0 -9
- data/lib/plutonium/core/controllers/base.rb +0 -101
- data/lib/plutonium/core/controllers/presentable.rb +0 -65
- data/lib/plutonium/core/controllers/queryable.rb +0 -28
- data/lib/plutonium/pkg/app.rb +0 -35
- data/lib/plutonium/pkg/concerns/resource_validatable.rb +0 -36
- data/lib/plutonium/pkg/feature.rb +0 -18
- data/lib/plutonium/policy/initializer.rb +0 -22
- data/lib/plutonium/policy/scope.rb +0 -19
- data/lib/plutonium/pundit/context.rb +0 -18
- data/lib/plutonium/pundit/policy_finder.rb +0 -25
- data/lib/plutonium/resource/policy_context.rb +0 -5
- data/lib/plutonium/resource_register.rb +0 -83
- data/lib/plutonium/smart_cache.rb +0 -151
- /data/app/views/{application → plutonium}/_flash.html.erb +0 -0
- /data/app/views/{application → plutonium}/_flash_alerts.html.erb +0 -0
- /data/app/views/{application → plutonium}/_flash_toasts.html.erb +0 -0
- /data/lib/generators/pu/pkg/{app/templates/app/views/package → package/templates}/.keep +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/app/interactions/resource_interaction.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/app/models/resource_record.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/app/policies/resource_policy.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/app/presenters/resource_presenter.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature → package}/templates/lib/engine.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/app/policies/resource_policy.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/app/presenters/resource_presenter.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/app/query_objects/resource_query_object.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{feature/templates → portal/templates/app/views/package}/.keep +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/config/routes.rb.tt +0 -0
- /data/lib/generators/pu/pkg/{app → portal}/templates/lib/engine.rb.tt +0 -0
@@ -0,0 +1,129 @@
|
|
1
|
+
module Plutonium
|
2
|
+
module Interaction
|
3
|
+
# Base class for interaction outcomes.
|
4
|
+
#
|
5
|
+
# This class provides a common interface for both successful and failed outcomes
|
6
|
+
# of an interaction.
|
7
|
+
#
|
8
|
+
# @abstract Subclass and override {#and_then}, {#map}, and {#to_response} to implement
|
9
|
+
class Outcome
|
10
|
+
# @return [Array<Array(String, Symbol)>] Messages associated with the outcome.
|
11
|
+
attr_reader :messages
|
12
|
+
|
13
|
+
# Checks if the outcome is successful.
|
14
|
+
#
|
15
|
+
# @return [Boolean] true if the outcome is a Success, false otherwise.
|
16
|
+
def success?
|
17
|
+
is_a?(Success)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Checks if the outcome is a failure.
|
21
|
+
#
|
22
|
+
# @return [Boolean] true if the outcome is a Failure, false otherwise.
|
23
|
+
def failure?
|
24
|
+
is_a?(Failure)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Adds a message to the outcome.
|
28
|
+
#
|
29
|
+
# @param msg [String] The message to add.
|
30
|
+
# @param type [Symbol] The type of the message (e.g., :notice, :error).
|
31
|
+
# @return [self]
|
32
|
+
def with_message(msg, type = :notice)
|
33
|
+
@messages ||= []
|
34
|
+
@messages << [msg, type]
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
# Sets the response for the outcome.
|
39
|
+
#
|
40
|
+
# @param response [Plutonium::Interaction::Response::Base] The response to set.
|
41
|
+
#
|
42
|
+
# @abstract
|
43
|
+
# @raise [NotImplementedError] if not implemented in subclass.
|
44
|
+
def with_response(response)
|
45
|
+
raise NotImplementedError, "#{self.class} must implement #with_response"
|
46
|
+
end
|
47
|
+
|
48
|
+
# Chains another operation to be executed if this outcome is successful.
|
49
|
+
#
|
50
|
+
# @abstract
|
51
|
+
# @raise [NotImplementedError] if not implemented in subclass.
|
52
|
+
def and_then
|
53
|
+
raise NotImplementedError, "#{self.class} must implement #and_then"
|
54
|
+
end
|
55
|
+
|
56
|
+
# Converts the outcome to a response object.
|
57
|
+
#
|
58
|
+
# @abstract
|
59
|
+
# @raise [NotImplementedError] if not implemented in subclass.
|
60
|
+
def to_response
|
61
|
+
raise NotImplementedError, "#{self.class} must implement #to_response"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Represents a successful outcome of an interaction.
|
66
|
+
class Success < Outcome
|
67
|
+
# @return [Object] The value wrapped by this successful outcome.
|
68
|
+
attr_reader :value
|
69
|
+
|
70
|
+
# @param value [Object] The value to be wrapped in this successful outcome.
|
71
|
+
def initialize(value)
|
72
|
+
@value = value
|
73
|
+
end
|
74
|
+
|
75
|
+
# Chains another operation to be executed with the value of this outcome.
|
76
|
+
#
|
77
|
+
# @yield [Object] The value wrapped by this outcome.
|
78
|
+
# @return [Outcome] The result of the yielded block.
|
79
|
+
def and_then
|
80
|
+
yield value
|
81
|
+
end
|
82
|
+
|
83
|
+
# Sets the response for this successful outcome.
|
84
|
+
#
|
85
|
+
# @param response [Plutonium::Interaction::Response::Base] The response to set.
|
86
|
+
# @return [self]
|
87
|
+
def with_response(response)
|
88
|
+
@to_response = nil
|
89
|
+
@response = response
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
# Converts this successful outcome to a response object.
|
94
|
+
#
|
95
|
+
# @return [Plutonium::Interaction::Response::Base] The response object.
|
96
|
+
def to_response
|
97
|
+
@to_response ||= begin
|
98
|
+
@response ||= Response::Null.new(value)
|
99
|
+
@response.with_flash(messages)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Represents a failed outcome of an interaction.
|
105
|
+
class Failure < Outcome
|
106
|
+
# @return [ActiveModel::Errors, Array<String>] The errors associated with this failure.
|
107
|
+
attr_reader :errors
|
108
|
+
|
109
|
+
# @param errors [ActiveModel::Errors, Array<String>] The errors to be wrapped in this failure.
|
110
|
+
def initialize(errors)
|
111
|
+
@errors = errors
|
112
|
+
end
|
113
|
+
|
114
|
+
# Returns self without executing the given block, propagating the failure.
|
115
|
+
#
|
116
|
+
# @return [self]
|
117
|
+
def and_then
|
118
|
+
self
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns self without setting a response.
|
122
|
+
#
|
123
|
+
# @return [self]
|
124
|
+
def with_response(response)
|
125
|
+
self
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Plutonium
|
2
|
+
module Interaction
|
3
|
+
module Response
|
4
|
+
# Base class for interaction responses.
|
5
|
+
#
|
6
|
+
# This class provides common functionality for handling flash messages
|
7
|
+
# and processing responses in controllers.
|
8
|
+
#
|
9
|
+
# @abstract Subclass and override {#execute} to implement
|
10
|
+
# specific response behavior.
|
11
|
+
class Base
|
12
|
+
# @return [Array<Array(String, Symbol)>] Flash messages associated with the response.
|
13
|
+
attr_reader :flash
|
14
|
+
|
15
|
+
# Initializes a new Response::Base instance.
|
16
|
+
def initialize
|
17
|
+
@flash = []
|
18
|
+
end
|
19
|
+
|
20
|
+
# Processes the response in the context of a controller.
|
21
|
+
#
|
22
|
+
# @param controller [ActionController::Base] The controller instance.
|
23
|
+
# @yield [Object] Executed if the response doesn't handle its own rendering.
|
24
|
+
# @return [void]
|
25
|
+
def process(controller, &)
|
26
|
+
set_flash(controller)
|
27
|
+
execute(controller, &)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Adds flash messages to the response.
|
31
|
+
#
|
32
|
+
# @param messages [Array<Array(String, Symbol)>] The messages to add.
|
33
|
+
# @return [self]
|
34
|
+
def with_flash(messages)
|
35
|
+
@flash.concat(messages) unless messages.blank?
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
# Sets flash messages in the controller.
|
42
|
+
#
|
43
|
+
# @param controller [ActionController::Base] The controller instance.
|
44
|
+
# @return [void]
|
45
|
+
def set_flash(controller)
|
46
|
+
@flash.each do |message, type|
|
47
|
+
controller.flash[type] = message
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Executes the response logic.
|
52
|
+
#
|
53
|
+
# @abstract
|
54
|
+
# @param controller [ActionController::Base] The controller instance.
|
55
|
+
# @yield [Object] Executed if the response doesn't handle its own rendering.
|
56
|
+
# @raise [NotImplementedError] if not implemented in subclass.
|
57
|
+
def execute(controller, &)
|
58
|
+
raise NotImplementedError, "#{self.class} must implement #execute"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Plutonium
|
2
|
+
module Interaction
|
3
|
+
module Response
|
4
|
+
# Represents a null response, which doesn't perform any specific action.
|
5
|
+
#
|
6
|
+
# This class is used when an interaction doesn't produce a specific response
|
7
|
+
# type but still needs to wrap a result value.
|
8
|
+
class Null < Base
|
9
|
+
# @return [Object] The result value wrapped by this null response.
|
10
|
+
attr_reader :result
|
11
|
+
|
12
|
+
# Initializes a new Null response.
|
13
|
+
#
|
14
|
+
# @param result [Object] The result value to be wrapped.
|
15
|
+
def initialize(result)
|
16
|
+
super()
|
17
|
+
@result = result
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# Executes the null response by yielding the result value.
|
23
|
+
#
|
24
|
+
# @param controller [ActionController::Base] The controller instance (unused).
|
25
|
+
# @yield [Object] The result value wrapped by this response.
|
26
|
+
# @return [void]
|
27
|
+
def execute(controller, &)
|
28
|
+
yield @result
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Plutonium
|
2
|
+
module Interaction
|
3
|
+
module Response
|
4
|
+
# Represents a redirect response.
|
5
|
+
#
|
6
|
+
# This class is used to perform redirects as a result of an interaction.
|
7
|
+
class Redirect < Base
|
8
|
+
# Initializes a new Redirect response.
|
9
|
+
#
|
10
|
+
# @param path [String, Symbol] The path or named route to redirect to.
|
11
|
+
# @param options [Hash] Additional options to pass to the redirect_to method.
|
12
|
+
def initialize(path, options = {})
|
13
|
+
super()
|
14
|
+
@path = path
|
15
|
+
@options = options
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
# Executes the redirect response.
|
21
|
+
#
|
22
|
+
# @param controller [ActionController::Base] The controller instance.
|
23
|
+
# @return [void]
|
24
|
+
def execute(controller)
|
25
|
+
controller.redirect_to @path, @options
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Plutonium
|
2
|
+
module Interaction
|
3
|
+
module Response
|
4
|
+
# Represents a render response.
|
5
|
+
#
|
6
|
+
# This class is used to render views as a result of an interaction.
|
7
|
+
class Render < Base
|
8
|
+
# Initializes a new Render response.
|
9
|
+
#
|
10
|
+
# @param options [Hash] Options to pass to the render method.
|
11
|
+
def initialize(options = {})
|
12
|
+
super()
|
13
|
+
@options = options
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
# Executes the render response.
|
19
|
+
#
|
20
|
+
# @param controller [ActionController::Base] The controller instance.
|
21
|
+
# @return [void]
|
22
|
+
def execute(controller)
|
23
|
+
controller.render @options
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -1,35 +1,96 @@
|
|
1
1
|
module Plutonium
|
2
2
|
module Lib
|
3
|
+
# The BitFlags class provides a convenient way to work with bit flags.
|
4
|
+
# It allows setting, checking, and extracting flags using a more
|
5
|
+
# readable interface. It supports both symbols and strings as flags.
|
6
|
+
#
|
7
|
+
# @example Usage
|
8
|
+
# flags = BitFlags.new(:read, 'write', :execute)
|
9
|
+
# value = flags[:read, 'write'] # => 3
|
10
|
+
# flags.set?(value, :read) # => true
|
11
|
+
# flags.set?(value, 'execute') # => false
|
12
|
+
# flags.extract(value) # => [:read, :write]
|
13
|
+
#
|
3
14
|
class BitFlags
|
4
|
-
|
15
|
+
# @return [Array<Symbol>] An array of all defined flags.
|
16
|
+
attr_reader :flags
|
5
17
|
|
18
|
+
# @return [Array<Integer>] An array of all defined bit values.
|
19
|
+
attr_reader :indices
|
20
|
+
|
21
|
+
# Initializes a new BitFlags object with the given flags.
|
22
|
+
#
|
23
|
+
# @param flags [Array<Symbol, String>] The flags to be used in this BitFlags object.
|
24
|
+
# @example
|
25
|
+
# BitFlags.new(:read, 'write', :execute)
|
6
26
|
def initialize(*flags)
|
7
|
-
@
|
27
|
+
@flags = normalize_flags(flags).uniq.freeze
|
28
|
+
@indices = @flags.each_index.map { |index| 1 << index }.freeze
|
29
|
+
@map = @flags.zip(@indices).to_h.freeze
|
30
|
+
@all_bits = calculate_all_bits
|
8
31
|
end
|
9
32
|
|
33
|
+
# Checks if the given value has all the specified flags set.
|
34
|
+
#
|
35
|
+
# @param value [Integer] The value to check against.
|
36
|
+
# @param flags [Array<Symbol, String>] The flags to check for.
|
37
|
+
# @return [Boolean] True if all specified flags are set and valid, false otherwise.
|
38
|
+
# @example
|
39
|
+
# flags.set?(3, :read, 'write') # => true
|
10
40
|
def set?(value, *flags)
|
11
|
-
|
41
|
+
normalized_flags = normalize_flags(flags)
|
42
|
+
return false if normalized_flags.any? { |flag| !@map.key?(flag) }
|
43
|
+
check = bits(*normalized_flags)
|
12
44
|
value & check == check
|
13
45
|
end
|
14
46
|
|
47
|
+
# Extracts the flags that are set in the given value.
|
48
|
+
#
|
49
|
+
# @param value [Integer] The value to extract flags from.
|
50
|
+
# @return [Array<Symbol>] An array of flags that are set in the value.
|
51
|
+
# @example
|
52
|
+
# flags.extract(3) # => [:read, :write]
|
15
53
|
def extract(value)
|
16
|
-
|
54
|
+
value &= @all_bits
|
55
|
+
@map.select { |_, bit| value & bit != 0 }.keys
|
17
56
|
end
|
18
57
|
|
58
|
+
# Returns the bit value for the given flags.
|
59
|
+
#
|
60
|
+
# @param flags [Array<Symbol, String>] The flags to get the bit value for.
|
61
|
+
# @return [Integer] The combined bit value of the given flags.
|
62
|
+
# @example
|
63
|
+
# flags[:read, 'write'] # => 3
|
19
64
|
def [](*flags)
|
20
65
|
bits(*flags)
|
21
66
|
end
|
22
67
|
|
68
|
+
# Calculates the combined bit value for the given flags.
|
69
|
+
#
|
70
|
+
# @param flags [Array<Symbol, String>] The flags to calculate the bit value for.
|
71
|
+
# @return [Integer] The combined bit value of the given flags.
|
72
|
+
# @example
|
73
|
+
# flags.bits(:read, 'write') # => 3
|
23
74
|
def bits(*flags)
|
24
|
-
|
75
|
+
normalized_flags = normalize_flags(flags)
|
76
|
+
normalized_flags.sum { |flag| @map[flag] || 0 }
|
77
|
+
end
|
78
|
+
|
79
|
+
# Calculates the sum of all bit values.
|
80
|
+
#
|
81
|
+
# @return [Integer] The sum of all bit values.
|
82
|
+
def sum
|
83
|
+
@all_bits
|
25
84
|
end
|
26
85
|
|
27
|
-
|
28
|
-
|
86
|
+
private
|
87
|
+
|
88
|
+
def calculate_all_bits
|
89
|
+
@indices.inject(:|) || 0
|
29
90
|
end
|
30
91
|
|
31
|
-
def
|
32
|
-
|
92
|
+
def normalize_flags(flags)
|
93
|
+
flags.map(&:to_sym)
|
33
94
|
end
|
34
95
|
end
|
35
96
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Plutonium
|
4
|
-
module
|
4
|
+
module Lib
|
5
5
|
# OverlayedHash provides a hash-like structure that overlays values on top of a base hash.
|
6
6
|
#
|
7
7
|
# This class allows you to create a new hash-like object that uses a base hash for default values,
|
@@ -0,0 +1,171 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Plutonium
|
4
|
+
module Lib
|
5
|
+
# The SmartCache module provides flexible caching mechanisms for classes and objects,
|
6
|
+
# allowing for both inline caching and method-level memoization.
|
7
|
+
#
|
8
|
+
# This module is designed to optimize performance by caching results
|
9
|
+
# when class caching is enabled (typically in production),
|
10
|
+
# while ensuring fresh results when caching is disabled (typically in development).
|
11
|
+
#
|
12
|
+
# This implementation is thread-safe.
|
13
|
+
#
|
14
|
+
# @example Including SmartCache in a class
|
15
|
+
# class MyClass
|
16
|
+
# include Plutonium::Lib::SmartCache
|
17
|
+
#
|
18
|
+
# def my_method(arg)
|
19
|
+
# cache_unless_reloading("my_method_#{arg}") { expensive_operation(arg) }
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# def another_method(arg)
|
23
|
+
# # Method implementation
|
24
|
+
# end
|
25
|
+
# memoize_unless_reloading :another_method
|
26
|
+
# end
|
27
|
+
module SmartCache
|
28
|
+
extend ::ActiveSupport::Concern
|
29
|
+
|
30
|
+
# Class-level thread-local storage for force caching (useful for testing)
|
31
|
+
thread_mattr_accessor :force_caching
|
32
|
+
|
33
|
+
included do
|
34
|
+
mattr_accessor :_smart_cache, instance_writer: false, default: Concurrent::Map.new
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# Caches the result of the given block unless class caching is disabled.
|
40
|
+
#
|
41
|
+
# @param cache_key [String] A unique key to identify the cached result
|
42
|
+
# @yield The block whose result will be cached
|
43
|
+
# @return [Object] The result of the block, either freshly computed or from cache
|
44
|
+
#
|
45
|
+
# @example Using cache_unless_reloading inline
|
46
|
+
# def fetch_user_data(user_id)
|
47
|
+
# cache_unless_reloading("user_data_#{user_id}") do
|
48
|
+
# UserDataService.fetch(user_id)
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
def cache_unless_reloading(cache_key, &block)
|
52
|
+
return yield unless should_smart_cache?
|
53
|
+
|
54
|
+
self.class._smart_cache.compute_if_absent(cache_key.to_sym) { yield }
|
55
|
+
end
|
56
|
+
|
57
|
+
# Flushes the smart cache for the specified keys or all keys if none are specified.
|
58
|
+
#
|
59
|
+
# @param keys [Array<Symbol, String>, Symbol, String] The cache key(s) to flush
|
60
|
+
# @return [void]
|
61
|
+
#
|
62
|
+
# @example Flushing specific cache keys
|
63
|
+
# flush_smart_cache([:user_data, :product_list])
|
64
|
+
#
|
65
|
+
# @example Flushing all cache keys
|
66
|
+
# flush_smart_cache
|
67
|
+
def flush_smart_cache(keys = nil)
|
68
|
+
self.class.flush_smart_cache(keys)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Determines whether caching should be performed based on the current Rails configuration
|
72
|
+
# or the force_caching flag.
|
73
|
+
#
|
74
|
+
# @return [Boolean] true if caching should be performed, false otherwise
|
75
|
+
def should_smart_cache?
|
76
|
+
Plutonium::Lib::SmartCache.force_caching.nil? ? Rails.application.config.cache_classes : Plutonium::Lib::SmartCache.force_caching
|
77
|
+
end
|
78
|
+
|
79
|
+
class_methods do
|
80
|
+
# Memoizes the result of the specified method unless class caching is disabled.
|
81
|
+
#
|
82
|
+
# @param method_name [Symbol] The name of the method to memoize
|
83
|
+
# @return [void]
|
84
|
+
#
|
85
|
+
# @example Memoizing a method
|
86
|
+
# class User
|
87
|
+
# include Plutonium::Lib::SmartCache
|
88
|
+
#
|
89
|
+
# def expensive_full_name_calculation
|
90
|
+
# # Complex name calculation
|
91
|
+
# end
|
92
|
+
# memoize_unless_reloading :expensive_full_name_calculation
|
93
|
+
# end
|
94
|
+
def memoize_unless_reloading(method_name)
|
95
|
+
return if method_defined?(:"#{method_name}_without_memoization")
|
96
|
+
|
97
|
+
original_method = instance_method(method_name)
|
98
|
+
alias_method :"#{method_name}_without_memoization", method_name
|
99
|
+
|
100
|
+
define_method(method_name) do |*args|
|
101
|
+
if should_smart_cache?
|
102
|
+
cache = self.class._smart_cache.compute_if_absent(method_name.to_sym) { Concurrent::Map.new }
|
103
|
+
if args.empty?
|
104
|
+
cache.compute_if_absent(:no_args) { original_method.bind_call(self) }
|
105
|
+
else
|
106
|
+
cache.compute_if_absent(args.hash.to_s) { original_method.bind_call(self, *args) }
|
107
|
+
end
|
108
|
+
else
|
109
|
+
original_method.bind_call(self, *args)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Flushes the smart cache for the specified keys or all keys if none are specified.
|
115
|
+
#
|
116
|
+
# @param keys [Array<Symbol, String>, Symbol, String] The cache key(s) to flush
|
117
|
+
# @return [void]
|
118
|
+
#
|
119
|
+
# @example Flushing specific cache keys
|
120
|
+
# flush_smart_cache([:user_data, :product_list])
|
121
|
+
#
|
122
|
+
# @example Flushing all cache keys
|
123
|
+
# flush_smart_cache
|
124
|
+
def flush_smart_cache(keys = nil)
|
125
|
+
keys = Array(keys).map(&:to_sym)
|
126
|
+
if keys.present?
|
127
|
+
keys.each { |key| _smart_cache.delete(key) }
|
128
|
+
else
|
129
|
+
_smart_cache.clear
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# Configuration:
|
136
|
+
# The caching behavior is controlled by the Rails configuration option config.cache_classes
|
137
|
+
# or can be overridden using the SmartCache.force_caching flag:
|
138
|
+
#
|
139
|
+
# - When false (typical in development):
|
140
|
+
# - Classes are reloaded on each request.
|
141
|
+
# - cache_unless_reloading always yields fresh results.
|
142
|
+
# - memoize_unless_reloading always calls the original method.
|
143
|
+
#
|
144
|
+
# - When true (typical in production):
|
145
|
+
# - Classes are cached.
|
146
|
+
# - cache_unless_reloading uses cached results.
|
147
|
+
# - memoize_unless_reloading uses memoized results, caching for each unique set of arguments.
|
148
|
+
#
|
149
|
+
# Best Practices:
|
150
|
+
# - Use meaningful and unique cache keys to avoid collisions.
|
151
|
+
# - Be mindful of memory usage, especially with large cached results.
|
152
|
+
# - Consider cache expiration strategies for long-running processes.
|
153
|
+
# - Use cache_unless_reloading for fine-grained control within methods.
|
154
|
+
# - Use memoize_unless_reloading for entire methods, especially those with expensive computations.
|
155
|
+
#
|
156
|
+
# Thread Safety:
|
157
|
+
# - This implementation is thread-safe.
|
158
|
+
# - It uses Concurrent::Map from the concurrent-ruby gem for thread-safe caching.
|
159
|
+
#
|
160
|
+
# Testing:
|
161
|
+
# - In your test environment, you can control caching behavior explicitly using SmartCache.force_caching.
|
162
|
+
# - Example:
|
163
|
+
# describe "MyClass" do
|
164
|
+
# before { Plutonium::Lib::SmartCache.force_caching = true }
|
165
|
+
# after { Plutonium::Lib::SmartCache.force_caching = nil }
|
166
|
+
# it "caches results" do
|
167
|
+
# # Test caching behavior
|
168
|
+
# end
|
169
|
+
# end
|
170
|
+
end
|
171
|
+
end
|