steroids 1.0.0 → 1.6.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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +854 -0
  3. data/Rakefile +27 -0
  4. data/app/jobs/steroids/async_service_job.rb +10 -0
  5. data/app/serializers/steroids/error_serializer.rb +35 -0
  6. data/lib/resources/quotes.yml +114 -0
  7. data/lib/steroids/controllers/methods.rb +18 -0
  8. data/lib/{concerns/controller.rb → steroids/controllers/responders_helper.rb} +20 -21
  9. data/lib/steroids/controllers/serializers_helper.rb +15 -0
  10. data/lib/steroids/engine.rb +6 -0
  11. data/lib/steroids/errors/base.rb +57 -0
  12. data/lib/steroids/errors/context.rb +70 -0
  13. data/lib/steroids/errors/quotes.rb +29 -0
  14. data/lib/steroids/errors.rb +89 -0
  15. data/lib/steroids/extensions/array_extension.rb +25 -0
  16. data/lib/steroids/extensions/class_extension.rb +141 -0
  17. data/lib/steroids/extensions/hash_extension.rb +14 -0
  18. data/lib/steroids/extensions/method_extension.rb +63 -0
  19. data/lib/steroids/extensions/module_extension.rb +32 -0
  20. data/lib/steroids/extensions/object_extension.rb +122 -0
  21. data/lib/steroids/extensions/proc_extension.rb +9 -0
  22. data/lib/steroids/logger.rb +162 -0
  23. data/lib/steroids/railtie.rb +60 -0
  24. data/lib/steroids/serializers/base.rb +7 -0
  25. data/lib/{concerns/serializer.rb → steroids/serializers/methods.rb} +3 -3
  26. data/lib/steroids/services/base.rb +181 -0
  27. data/lib/steroids/support/magic_class.rb +17 -0
  28. data/lib/steroids/support/noticable_methods.rb +134 -0
  29. data/lib/steroids/support/servicable_methods.rb +34 -0
  30. data/lib/{base/type.rb → steroids/types/base.rb} +3 -3
  31. data/lib/{base/model.rb → steroids/types/serializable_type.rb} +2 -2
  32. data/lib/steroids/version.rb +4 -0
  33. data/lib/steroids.rb +12 -0
  34. metadata +75 -34
  35. data/lib/base/class.rb +0 -15
  36. data/lib/base/error.rb +0 -87
  37. data/lib/base/hash.rb +0 -49
  38. data/lib/base/list.rb +0 -51
  39. data/lib/base/service.rb +0 -104
  40. data/lib/concern.rb +0 -130
  41. data/lib/concerns/error.rb +0 -20
  42. data/lib/concerns/model.rb +0 -9
  43. data/lib/errors/bad_request_error.rb +0 -15
  44. data/lib/errors/conflict_error.rb +0 -15
  45. data/lib/errors/forbidden_error.rb +0 -15
  46. data/lib/errors/generic_error.rb +0 -14
  47. data/lib/errors/internal_server_error.rb +0 -15
  48. data/lib/errors/not_found_error.rb +0 -15
  49. data/lib/errors/not_implemented_error.rb +0 -15
  50. data/lib/errors/unauthorized_error.rb +0 -15
  51. data/lib/errors/unprocessable_entity_error.rb +0 -15
data/lib/base/hash.rb DELETED
@@ -1,49 +0,0 @@
1
- module Steroids
2
- module Base
3
- class Hash < Steroids::Base::Class
4
- #include Enumerable # should implement each
5
-
6
- attr_accessor :context
7
-
8
- def initialize
9
- @context = {}
10
- end
11
-
12
- def add(object = {})
13
- @context.merge!(object)
14
- end
15
-
16
- def <<(object)
17
- @context.merge!(object)
18
- end
19
-
20
- def to_json(*_args)
21
- @context.to_json
22
- end
23
-
24
- def to_hash
25
- @context.to_hash
26
- end
27
-
28
- def merge(object)
29
- @context.merge(object)
30
- end
31
-
32
- def merge!(object)
33
- @context.merge!(object)
34
- end
35
-
36
- def include?(key)
37
- @context.include?(key)
38
- end
39
-
40
- def empty?
41
- @context.empty?
42
- end
43
-
44
- def [](key)
45
- @context[key]
46
- end
47
- end
48
- end
49
- end
data/lib/base/list.rb DELETED
@@ -1,51 +0,0 @@
1
- module Steroids
2
- module Base
3
- class List < Steroids::Base::Class
4
- #include Enumerable # should implement each
5
-
6
- attr_accessor :errors
7
-
8
- def initialize
9
- @errors = []
10
- end
11
-
12
- def add(object = [])
13
- add_error(object)
14
- end
15
-
16
- def <<(object = [])
17
- add_error(object)
18
- end
19
-
20
- def to_ary
21
- @errors
22
- end
23
-
24
- def to_a
25
- to_ary
26
- end
27
-
28
- def any?
29
- @errors.any?
30
- end
31
-
32
- def empty?
33
- @errors.empty?
34
- end
35
-
36
- private
37
-
38
- def add_error(object = [])
39
- if object.is_a? ActiveModel::Errors
40
- formatted = object.to_a.map { |item| item.gsub(/"/, '\'') }
41
- elsif object.is_a?(Array)
42
- formatted = object
43
- elsif object.is_a?(String)
44
- formatted = [object]
45
- end
46
- formatted.each { |item| Utilities::Logger.push(item) }
47
- @errors.concat(formatted)
48
- end
49
- end
50
- end
51
- end
data/lib/base/service.rb DELETED
@@ -1,104 +0,0 @@
1
- module Steroids
2
- module Base
3
- class Service < Steroids::Base::Class
4
- include Steroids::Concerns::Error
5
-
6
- @@wrap_in_transaction = true
7
- @@skip_callbacks = false
8
-
9
- def call(options = {})
10
- @output = nil
11
- @force = options[:force] ||= false
12
- @skip_callbacks = options[:skip_callbacks] || @@skip_callbacks ||= false
13
- @@wrap_in_transaction ? ActiveRecord::Base.transaction { process_service } : process_service
14
- ensure!
15
- @output
16
- rescue StandardError => e
17
- ActiveRecord::Rollback
18
- ensure!
19
- rescue_output = rescue!(e)
20
- raise e unless rescue_output
21
-
22
- Utilities::Logger.push(e)
23
- rescue_output
24
- end
25
-
26
- protected
27
-
28
- def process
29
- true
30
- end
31
-
32
- def ensure!
33
- true
34
- end
35
-
36
- def rescue!(_exception)
37
- false
38
- end
39
-
40
- private
41
-
42
- def process_service
43
- run_before_callbacks unless @skip_callbacks
44
- @output = process
45
- run_after_callbacks unless @skip_callbacks
46
- if errors? && !@force
47
- raise Steroids::Errors::GenericError.new(
48
- message: 'Oops, something went wrong',
49
- errors: errors
50
- )
51
- end
52
- end
53
-
54
- def exit(message: nil)
55
- unless @force
56
- raise Steroids::Errors::InternalServerError.new(
57
- message: message,
58
- errors: errors
59
- )
60
- end
61
- end
62
-
63
- def drop(message: nil)
64
- unless @force
65
- raise Steroids::Errors::BadRequestError.new(
66
- message: message,
67
- errors: errors
68
- )
69
- end
70
- end
71
-
72
- def run_before_callbacks
73
- if self.class.before_callbacks.is_a?(Array)
74
- self.class.before_callbacks.each do |callback|
75
- send(callback)
76
- end
77
- end
78
- end
79
-
80
- def run_after_callbacks
81
- if self.class.after_callbacks.is_a?(Array)
82
- self.class.after_callbacks.each do |callback|
83
- send(callback)
84
- end
85
- end
86
- end
87
-
88
- class << self
89
- attr_accessor :before_callbacks
90
- attr_accessor :after_callbacks
91
-
92
- def before_process(method)
93
- @before_callbacks ||= []
94
- @before_callbacks << method
95
- end
96
-
97
- def after_process(method)
98
- @after_callbacks ||= []
99
- @after_callbacks << method
100
- end
101
- end
102
- end
103
- end
104
- end
data/lib/concern.rb DELETED
@@ -1,130 +0,0 @@
1
- module Steroids
2
- module Concern
3
- # ----------------------------------------------------------------------- #
4
- # Multiple Included Blocks Error #
5
- # ----------------------------------------------------------------------- #
6
-
7
- class MultipleIncludedBlocks < StandardError #:nodoc:
8
- def initialize
9
- super "Cannot define multiple 'included' blocks for a Concern"
10
- end
11
- end
12
-
13
- # ----------------------------------------------------------------------- #
14
- # Extended #
15
- # ----------------------------------------------------------------------- #
16
-
17
- def self.extended(base)
18
- base.instance_variable_set(:@_dependencies, [])
19
- end
20
-
21
- # ----------------------------------------------------------------------- #
22
- # Concern: Append features #
23
- # ----------------------------------------------------------------------- #
24
-
25
- def get_proxy(base_class, instance, scope)
26
- _self = self
27
- proxy_name = "#{base_class.name.gsub('::', '') + SecureRandom.hex + @scope.to_s.classify}Proxy"
28
- unless _self.const_defined?(proxy_name)
29
- proxy_class = Class.new base_class do
30
- self.table_name = base_class.table_name
31
-
32
- define_method :initialize do |instance|
33
- super(instance.attributes)
34
- end
35
-
36
- instance.attributes.each do |key, _attribute|
37
- define_method key do
38
- instance.read_attribute(key)
39
- end
40
-
41
- define_method "#{key}=" do |value|
42
- instance.write_attribute(key, value)
43
- end
44
- end
45
-
46
- native_methods = Class.instance_methods
47
- active_methods = ActiveRecord::Base.instance_methods
48
- own_methods = instance_methods - base_class.instance_methods
49
-
50
- instance_methods.each do |method|
51
- next unless !native_methods.include?(method) && !own_methods.include?(method) &&
52
- !active_methods.include?(method) && method != scope
53
-
54
- define_method method do |*args|
55
- instance.send(method, *args)
56
- end
57
- end
58
- end
59
- _self.const_set(proxy_name, proxy_class)
60
- end
61
- _self.const_get(proxy_name)
62
- end
63
-
64
- def append_features(base_class)
65
- _self = self
66
- if base_class.instance_variable_defined?(:@_dependencies)
67
- base_class.instance_variable_get(:@_dependencies) << self
68
- false
69
- else
70
- _scope_name = @_scope_name
71
- _dependencies = @_dependencies
72
- _included_block = @_included_block
73
-
74
- # ----------------------------------------------------------------------- #
75
- # Ruby native concern handlers #
76
- # ----------------------------------------------------------------------- #
77
-
78
- return false if base_class < self
79
-
80
- super
81
-
82
- # ----------------------------------------------------------------------- #
83
- # Defining the concern scope as a method #
84
- # ----------------------------------------------------------------------- #
85
-
86
- define_method @_scope_name do
87
- instance = self
88
-
89
- proxy = _self.get_proxy(base_class, instance, _scope_name)
90
-
91
- # ----------------------------------------------------------------------- #
92
- # Proc & Dependency injection #
93
- # ----------------------------------------------------------------------- #
94
-
95
- _dependencies.each { |dependency| proxy.include(dependency) }
96
- if _self.const_defined?(:ClassMethods)
97
- proxy.extend const_get(:ClassMethods)
98
- end
99
- if _self.instance_variable_defined?(:@_included_block)
100
- proxy.class_eval(&_included_block)
101
- end
102
-
103
- # ----------------------------------------------------------------------- #
104
- # Rails & Ruby native concern handlers #
105
- # ----------------------------------------------------------------------- #
106
-
107
- @proxy ||= proxy.new(instance)
108
- end
109
- end
110
- end
111
-
112
- def concern(scope, &block)
113
- if instance_variable_defined?(:@_included_block)
114
- if @_included_block.source_location != block.source_location
115
- raise MultipleIncludedBlocks
116
- end
117
- else
118
- @_scope_name = scope
119
- @_included_block = block
120
- end
121
- end
122
-
123
- def class_methods(&class_methods_module_definition)
124
- mod = const_defined?(:ClassMethods, false) ?
125
- const_get(:ClassMethods) :
126
- const_set(:ClassMethods, Module.new)
127
- mod.module_eval(&class_methods_module_definition)
128
- end
129
- end
130
- end
@@ -1,20 +0,0 @@
1
- module Steroids
2
- module Concerns
3
- module Error
4
- extend ActiveSupport::Concern
5
- included do
6
- def errors?
7
- errors.any?
8
- end
9
-
10
- def errors
11
- @errors ||= Steroids::Base::List.new
12
- end
13
-
14
- class << self
15
- attr_accessor :errors
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,9 +0,0 @@
1
- module Steroids
2
- module Concerns
3
- module Model
4
- extend ActiveSupport::Concern
5
- included do
6
- end
7
- end
8
- end
9
- end
@@ -1,15 +0,0 @@
1
- module Steroids
2
- module Errors
3
- class BadRequestError < Steroids::Base::Error
4
- def initialize(options = {})
5
- options[:message] ||= 'Something went wrong (Bad request)'
6
- super(
7
- {
8
- status: :bad_request,
9
- key: :bad_request
10
- }.merge(options)
11
- )
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module Steroids
2
- module Errors
3
- class ConflictError < Steroids::Base::Error
4
- def initialize(options = {})
5
- options[:message] ||= "Something went wrong (Internal conflict)"
6
- super(
7
- {
8
- status: :conflict,
9
- key: :conflict
10
- }.merge(options)
11
- )
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module Steroids
2
- module Errors
3
- class ForbiddenError < Steroids::Base::Error
4
- def initialize(options = {})
5
- options[:message] ||= "Your request was denied (Forbidden)"
6
- super(
7
- {
8
- status: :forbidden,
9
- key: :forbidden
10
- }.merge(options)
11
- )
12
- end
13
- end
14
- end
15
- end
@@ -1,14 +0,0 @@
1
- module Steroids
2
- module Errors
3
- class GenericError < Steroids::Base::Error
4
- def initialize(options = {})
5
- super(
6
- {
7
- message: options[:message] || "Something went wrong (Generic error)",
8
- status: options[:status] ? Rack::Utils.status_code(status) : false
9
- }.merge(options)
10
- )
11
- end
12
- end
13
- end
14
- end
@@ -1,15 +0,0 @@
1
- module Steroids
2
- module Errors
3
- class InternalServerError < Steroids::Base::Error
4
- def initialize(options = {})
5
- options[:message] ||= "Something went wrong (Internal error)"
6
- super(
7
- {
8
- status: :internal_server_error,
9
- key: :server_error
10
- }.merge(options)
11
- )
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module Steroids
2
- module Errors
3
- class NotFoundError < Steroids::Base::Error
4
- def initialize(options = {})
5
- super(
6
- {
7
- message: options[:message] || "We couldn't find what you were looking for",
8
- status: :not_found,
9
- key: :not_found
10
- }.merge(options)
11
- )
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module Steroids
2
- module Errors
3
- class NotImplementedError < Steroids::Base::Error
4
- def initialize(options = {})
5
- options[:message] ||= "This feature hasn't been implemented yet"
6
- super(
7
- {
8
- status: :not_implemented,
9
- key: :not_implemented
10
- }.merge(options)
11
- )
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module Steroids
2
- module Errors
3
- class UnauthorizedError < Steroids::Base::Error
4
- def initialize(options = {})
5
- options[:message] ||= 'You shall not pass! (Unauthorized)'
6
- super(
7
- {
8
- status: :unauthorized,
9
- key: :unauthorized
10
- }.merge(options)
11
- )
12
- end
13
- end
14
- end
15
- end
@@ -1,15 +0,0 @@
1
- module Steroids
2
- module Errors
3
- class UnprocessableEntityError < Steroids::Base::Error
4
- def initialize(options = {})
5
- options[:message] ||= "We couldn't understand your request (Unprocessable entity)"
6
- super(
7
- {
8
- status: :unprocessable_entity,
9
- key: :unprocessable_entity
10
- }.merge(options)
11
- )
12
- end
13
- end
14
- end
15
- end