rest_my_case 1.11.7 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rest_my_case/base.rb +8 -15
  3. data/lib/rest_my_case/config/base.rb +1 -6
  4. data/lib/rest_my_case/config/general.rb +0 -4
  5. data/lib/rest_my_case/context/base.rb +3 -7
  6. data/lib/rest_my_case/context/errors/base.rb +0 -4
  7. data/lib/rest_my_case/context/errors/status.rb +0 -4
  8. data/lib/rest_my_case/context/http_status.rb +1 -5
  9. data/lib/rest_my_case/context/schema_validator/base.rb +0 -4
  10. data/lib/rest_my_case/context/schema_validator/compel.rb +0 -4
  11. data/lib/rest_my_case/context/status.rb +1 -5
  12. data/lib/rest_my_case/defense_attorney/base.rb +0 -4
  13. data/lib/rest_my_case/errors/abort.rb +1 -3
  14. data/lib/rest_my_case/errors/base.rb +1 -3
  15. data/lib/rest_my_case/errors/skip.rb +1 -3
  16. data/lib/rest_my_case/helpers.rb +0 -52
  17. data/lib/rest_my_case/http_status.rb +0 -4
  18. data/lib/rest_my_case/judge/base.rb +0 -4
  19. data/lib/rest_my_case/status.rb +3 -10
  20. data/lib/rest_my_case/trial/case.rb +0 -4
  21. data/lib/rest_my_case/trial/court.rb +4 -6
  22. data/lib/rest_my_case/validator.rb +10 -132
  23. data/lib/rest_my_case/version.rb +1 -3
  24. data/lib/rest_my_case.rb +0 -12
  25. metadata +56 -112
  26. data/.gitignore +0 -41
  27. data/.rspec +0 -4
  28. data/.rubocop.yml +0 -24
  29. data/.travis.yml +0 -13
  30. data/Gemfile +0 -4
  31. data/LICENSE +0 -22
  32. data/Rakefile +0 -7
  33. data/console +0 -13
  34. data/lib/rest_my_case/accusation_attorneys/base.rb +0 -27
  35. data/lib/rest_my_case/accusation_attorneys/custom.rb +0 -21
  36. data/lib/rest_my_case/accusation_attorneys/each.rb +0 -38
  37. data/lib/rest_my_case/accusation_attorneys/errors.rb +0 -365
  38. data/lib/rest_my_case/accusation_attorneys/format.rb +0 -64
  39. data/lib/rest_my_case/accusation_attorneys/helper_methods.rb +0 -16
  40. data/lib/rest_my_case/accusation_attorneys/length.rb +0 -86
  41. data/lib/rest_my_case/accusation_attorneys/numericality.rb +0 -94
  42. data/lib/rest_my_case/accusation_attorneys/presence.rb +0 -21
  43. data/rest_my_case.gemspec +0 -27
  44. data/spec/rest_my_case/accusation_attorneys/base_spec.rb +0 -17
  45. data/spec/rest_my_case/accusation_attorneys/each_spec.rb +0 -17
  46. data/spec/rest_my_case/accusation_attorneys/format_spec.rb +0 -71
  47. data/spec/rest_my_case/accusation_attorneys/length_spec.rb +0 -46
  48. data/spec/rest_my_case/accusation_attorneys/numericality_spec.rb +0 -126
  49. data/spec/rest_my_case/base/context_accessor_spec.rb +0 -29
  50. data/spec/rest_my_case/base/context_reader_spec.rb +0 -21
  51. data/spec/rest_my_case/base/context_writer_spec.rb +0 -23
  52. data/spec/rest_my_case/base/dependencies_spec.rb +0 -14
  53. data/spec/rest_my_case/base/invoke!_spec.rb +0 -140
  54. data/spec/rest_my_case/base/invoke_spec.rb +0 -29
  55. data/spec/rest_my_case/base/perform_spec.rb +0 -241
  56. data/spec/rest_my_case/base/required_context_spec.rb +0 -51
  57. data/spec/rest_my_case/context/base_spec.rb +0 -33
  58. data/spec/rest_my_case/context/status_spec.rb +0 -52
  59. data/spec/rest_my_case/defense_attorney/base_spec.rb +0 -130
  60. data/spec/rest_my_case/http_status_spec.rb +0 -70
  61. data/spec/rest_my_case/status_spec.rb +0 -182
  62. data/spec/rest_my_case/trial_case/court_spec.rb +0 -28
  63. data/spec/rest_my_case/validator_spec.rb +0 -334
  64. data/spec/spec_helper.rb +0 -25
  65. data/spec/support/defense_attorney.rb +0 -51
  66. data/spec/support/perform.rb +0 -67
  67. data/spec/support/required_context.rb +0 -88
  68. data/spec/support/rest_my_case_base.rb +0 -47
  69. data/spec/support/validator/models/ruby_post.rb +0 -13
  70. data/spec/support/validator/models/ruby_post_with_comments.rb +0 -19
  71. data/spec/support/validator/models/ruby_user.rb +0 -9
  72. data/spec/support/validator/usecases/custom_validator.rb +0 -21
  73. data/spec/support/validator/usecases/hierarchy_validation.rb +0 -23
  74. data/spec/support/validator/usecases/length_validator.rb +0 -19
  75. data/spec/support/validator/usecases/nested_validation.rb +0 -30
  76. data/spec/support/validator/usecases/numericality_validator.rb +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 626ba1eecdf25814ed56cafae419c38d7c4ea889
4
- data.tar.gz: c797998649c9038af80eff4017293ee03c024f66
3
+ metadata.gz: ea10298296eae5da430a0cc46611d48e14331fb6
4
+ data.tar.gz: 5dd8951535f0913684334076278ad78262e5b056
5
5
  SHA512:
6
- metadata.gz: c296d0cad02320de30050283c5c66b7bf96118383874a22861f7111fc04bd2443e449a20bb93d4ac44e9e808eb26345933c12a3afab821e5707cb65a7d26e7b0
7
- data.tar.gz: 47818e573ff4a6b224bae82c803ee1bca29825bd09f1b77507032ef2427ac0680b840196887e7c9f889142fee9c64d9c9c93fb6375ff5af94589941d0a5b76a6
6
+ metadata.gz: fd50335309ebda4a8dcd27325749d8195003125c8cf39493f4f4c67eaa251b956dff114e52a9b97b351410b6adda8c12e3f9455058148b88cd88f6df6ddaf651
7
+ data.tar.gz: bfd7ab9fe38e64a36c4a7c4ae1b2e53108c7df15bd29256719c81a3edebbfb66998e2cc059752982ac6a56b47e3bf26ececbc71f295c9230e261f51a2708e7cd
@@ -1,7 +1,5 @@
1
1
  module RestMyCase
2
-
3
2
  class Base
4
-
5
3
  extend Config::Base
6
4
 
7
5
  def self.trial_court
@@ -24,7 +22,7 @@ module RestMyCase
24
22
  end
25
23
 
26
24
  def self.depends(*use_case_classes)
27
- dependencies.push(*use_case_classes)
25
+ dependencies.concat(use_case_classes)
28
26
  end
29
27
 
30
28
  def self.dependencies
@@ -35,7 +33,7 @@ module RestMyCase
35
33
  attributes ||= {}
36
34
 
37
35
  unless attributes.respond_to?(:to_hash)
38
- fail ArgumentError, 'Must respond_to method #to_hash'
36
+ raise ArgumentError, 'Must respond_to method #to_hash'
39
37
  end
40
38
 
41
39
  trial_court.execute([self], attributes.to_hash).context
@@ -99,15 +97,12 @@ module RestMyCase
99
97
  end
100
98
 
101
99
  def abort!
102
- abort && fail(Errors::Abort)
100
+ abort && raise(Errors::Abort)
103
101
  end
104
102
 
105
103
  def error(error_message = '')
106
- if error_message.is_a?(Hash)
107
- error_data = error_message
108
- else
109
- error_data = { message: error_message }
110
- end
104
+ error_data = \
105
+ error_message.is_a?(Hash) ? error_message : { message: error_message }
111
106
 
112
107
  error_data[:class_name] = self.class.name
113
108
 
@@ -115,7 +110,7 @@ module RestMyCase
115
110
  end
116
111
 
117
112
  def error!(error_data = '')
118
- error(error_data) && fail(Errors::Abort)
113
+ error(error_data) && raise(Errors::Abort)
119
114
  end
120
115
 
121
116
  def skip
@@ -123,7 +118,7 @@ module RestMyCase
123
118
  end
124
119
 
125
120
  def skip!
126
- skip && fail(Errors::Skip)
121
+ skip && raise(Errors::Skip)
127
122
  end
128
123
 
129
124
  def validate_context(schema = self.class.required_context_schema)
@@ -135,9 +130,7 @@ module RestMyCase
135
130
  end
136
131
 
137
132
  def validate_context!(schema = self.class.required_context)
138
- validate_context(schema) && fail(Errors::Abort)
133
+ validate_context(schema) && raise(Errors::Abort)
139
134
  end
140
-
141
135
  end
142
-
143
136
  end
@@ -1,12 +1,7 @@
1
1
  module RestMyCase
2
2
  module Config
3
-
4
3
  module Base
5
-
6
- attr_accessor :parent_dependencies_first,
7
- :silence_dependencies_abort
8
-
4
+ attr_accessor :parent_dependencies_first, :silence_dependencies_abort
9
5
  end
10
-
11
6
  end
12
7
  end
@@ -1,8 +1,6 @@
1
1
  module RestMyCase
2
2
  module Config
3
-
4
3
  class General
5
-
6
4
  include Base
7
5
 
8
6
  def initialize
@@ -29,8 +27,6 @@ module RestMyCase
29
27
  custom_config
30
28
  end
31
29
  end
32
-
33
30
  end
34
-
35
31
  end
36
32
  end
@@ -3,10 +3,8 @@ require 'rest_my_case/context/schema_validator/base'
3
3
 
4
4
  module RestMyCase
5
5
  module Context
6
-
7
6
  class Base < OpenStruct
8
-
9
- alias_method :attributes, :marshal_dump
7
+ alias attributes marshal_dump
10
8
 
11
9
  if defined?(ActiveModel) && defined?(ActiveModel::Serialization)
12
10
  include ActiveModel::Serialization
@@ -40,11 +38,9 @@ module RestMyCase
40
38
  errors.empty?
41
39
  end
42
40
 
43
- alias_method :ok?, :valid?
44
-
45
- alias_method :success?, :ok?
41
+ alias ok? valid?
46
42
 
43
+ alias success? ok?
47
44
  end
48
-
49
45
  end
50
46
  end
@@ -1,9 +1,7 @@
1
1
  module RestMyCase
2
2
  module Context
3
3
  module Errors
4
-
5
4
  class Base < Array
6
-
7
5
  def initialize(context)
8
6
  super()
9
7
 
@@ -13,9 +11,7 @@ module RestMyCase
13
11
  def add(error)
14
12
  push(error)
15
13
  end
16
-
17
14
  end
18
-
19
15
  end
20
16
  end
21
17
  end
@@ -1,9 +1,7 @@
1
1
  module RestMyCase
2
2
  module Context
3
3
  module Errors
4
-
5
4
  class Status < Base
6
-
7
5
  attr_reader :last_known_error
8
6
 
9
7
  def add(error)
@@ -20,9 +18,7 @@ module RestMyCase
20
18
 
21
19
  @last_known_error = error
22
20
  end
23
-
24
21
  end
25
-
26
22
  end
27
23
  end
28
24
  end
@@ -1,8 +1,6 @@
1
1
  module RestMyCase
2
2
  module Context
3
-
4
3
  class HttpStatus < Status
5
-
6
4
  RAILS_HTTP_STATUS = {
7
5
  continue: 100,
8
6
  switching_protocols: 101,
@@ -53,7 +51,7 @@ module RestMyCase
53
51
  http_version_not_supported: 505,
54
52
  insufficient_storage: 507,
55
53
  not_extended: 510
56
- }
54
+ }.freeze
57
55
 
58
56
  def http_status
59
57
  RAILS_HTTP_STATUS[status.to_sym]
@@ -70,8 +68,6 @@ module RestMyCase
70
68
  { message: 'unkown error' }
71
69
  end
72
70
  end
73
-
74
71
  end
75
-
76
72
  end
77
73
  end
@@ -1,9 +1,7 @@
1
1
  module RestMyCase
2
2
  module Context
3
3
  module SchemaValidator
4
-
5
4
  class Base
6
-
7
5
  def initialize(context)
8
6
  @context = context
9
7
  end
@@ -19,9 +17,7 @@ module RestMyCase
19
17
 
20
18
  Helpers.blank?(errors) ? nil : errors
21
19
  end
22
-
23
20
  end
24
-
25
21
  end
26
22
  end
27
23
  end
@@ -3,9 +3,7 @@ require 'compel'
3
3
  module RestMyCase
4
4
  module Context
5
5
  module SchemaValidator
6
-
7
6
  class Base
8
-
9
7
  def initialize(context)
10
8
  @context = context
11
9
  end
@@ -28,9 +26,7 @@ module RestMyCase
28
26
  schema.each { |key| new_schema[key] = Compel.any.required }
29
27
  end
30
28
  end
31
-
32
29
  end
33
-
34
30
  end
35
31
  end
36
32
  end
@@ -2,9 +2,7 @@ require 'rest_my_case/context/errors/status'
2
2
 
3
3
  module RestMyCase
4
4
  module Context
5
-
6
5
  class Status < Base
7
-
8
6
  class StatusString < String
9
7
 
10
8
  def method_missing(method, *args, &block)
@@ -31,10 +29,8 @@ module RestMyCase
31
29
  end
32
30
 
33
31
  def status=(_)
34
- fail 'status is a reserved keyword which cannot be set'
32
+ raise 'status is a reserved keyword which cannot be set'
35
33
  end
36
-
37
34
  end
38
-
39
35
  end
40
36
  end
@@ -1,8 +1,6 @@
1
1
  module RestMyCase
2
2
  module DefenseAttorney
3
-
4
3
  class Base
5
-
6
4
  def initialize(trial_case)
7
5
  @trial_case = trial_case
8
6
  @trial_case.use_cases = []
@@ -36,8 +34,6 @@ module RestMyCase
36
34
  use_case_class.dependencies | dependencies(use_case_class.superclass)
37
35
  end
38
36
  end
39
-
40
37
  end
41
-
42
38
  end
43
39
  end
@@ -1,7 +1,5 @@
1
1
  module RestMyCase
2
2
  module Errors
3
-
4
- class Abort < Base; end
5
-
3
+ Abort = Class.new(Base)
6
4
  end
7
5
  end
@@ -1,8 +1,6 @@
1
1
  module RestMyCase
2
2
  module Errors
3
-
4
- class Base < StandardError; end
5
-
3
+ Base = Class.new(StandardError)
6
4
  end
7
5
  end
8
6
 
@@ -1,7 +1,5 @@
1
1
  module RestMyCase
2
2
  module Errors
3
-
4
- class Skip < Base; end
5
-
3
+ Skip = Class.new(Base)
6
4
  end
7
5
  end
@@ -1,15 +1,7 @@
1
1
  module RestMyCase
2
-
3
2
  module Helpers
4
-
5
3
  module_function
6
4
 
7
- def super_method(object, method_name, *args)
8
- return nil unless object.superclass.respond_to? method_name
9
-
10
- object.superclass.send method_name, *args
11
- end
12
-
13
5
  def blank?(object)
14
6
  if object.is_a?(String)
15
7
  object !~ /[^[:space:]]/
@@ -17,49 +9,5 @@ module RestMyCase
17
9
  object.respond_to?(:empty?) ? object.empty? : !object
18
10
  end
19
11
  end
20
-
21
- def marked_for_destruction?(object)
22
- return false unless object.respond_to?(:marked_for_destruction?)
23
-
24
- object.marked_for_destruction?
25
- end
26
-
27
- def extract_options!(array)
28
- if array.last.is_a?(Hash) && array.last.instance_of?(Hash)
29
- array.pop
30
- else
31
- {}
32
- end
33
- end
34
-
35
- def symbolyze_keys(hash)
36
- {}.tap do |symbolyzed_hash|
37
- hash.each do |key, value|
38
- symbolyzed_hash[key.to_sym] = value
39
- end
40
- end
41
- end
42
-
43
- def except(hash, *keys)
44
- hash = hash.dup
45
- keys.each { |key| hash.delete(key) }
46
- hash
47
- end
48
-
49
- def slice(hash, *keys)
50
- keys.each_with_object({}) do |key, sliced_hash|
51
- sliced_hash[key] = hash[key]
52
- end
53
- end
54
-
55
- def call_proc_or_method(base, proc_or_method, object = nil)
56
- if proc_or_method.is_a?(Proc)
57
- base.instance_exec(object, &proc_or_method)
58
- else
59
- base.send(proc_or_method, object)
60
- end
61
- end
62
-
63
12
  end
64
-
65
13
  end
@@ -1,9 +1,7 @@
1
1
  require 'rest_my_case/context/http_status'
2
2
 
3
3
  module RestMyCase
4
-
5
4
  module HttpStatus
6
-
7
5
  include Status
8
6
 
9
7
  module ClassMethods
@@ -16,7 +14,5 @@ module RestMyCase
16
14
  def self.included(parent_class)
17
15
  parent_class.extend ClassMethods
18
16
  end
19
-
20
17
  end
21
-
22
18
  end
@@ -1,8 +1,6 @@
1
1
  module RestMyCase
2
2
  module Judge
3
-
4
3
  class Base
5
-
6
4
  def initialize(trial_case)
7
5
  @trial_case = trial_case
8
6
  @performed_use_cases = []
@@ -77,8 +75,6 @@ module RestMyCase
77
75
  rescue Errors::Abort
78
76
  @use_case_that_aborted = use_case
79
77
  end
80
-
81
78
  end
82
-
83
79
  end
84
80
  end
@@ -1,9 +1,7 @@
1
1
  require 'rest_my_case/context/status'
2
2
 
3
3
  module RestMyCase
4
-
5
4
  module Status
6
-
7
5
  module ClassMethods
8
6
  def trial_court
9
7
  @trial_court ||= Trial::Court.new \
@@ -20,11 +18,8 @@ module RestMyCase
20
18
  end
21
19
 
22
20
  def failure(status, error_message = nil)
23
- if error_message.is_a?(Hash)
24
- error_data = error_message
25
- else
26
- error_data = { message: error_message }
27
- end
21
+ error_data = \
22
+ error_message.is_a?(Hash) ? error_message : { message: error_message }
28
23
 
29
24
  error_data[:status] = status
30
25
 
@@ -32,9 +27,7 @@ module RestMyCase
32
27
  end
33
28
 
34
29
  def failure!(status, error = nil)
35
- failure(status, error) && fail(Errors::Abort)
30
+ failure(status, error) && raise(Errors::Abort)
36
31
  end
37
-
38
32
  end
39
-
40
33
  end
@@ -1,8 +1,6 @@
1
1
  module RestMyCase
2
2
  module Trial
3
-
4
3
  class Case
5
-
6
4
  attr_accessor :use_cases, :should_abort, :defendant_child
7
5
 
8
6
  attr_reader :context,
@@ -33,8 +31,6 @@ module RestMyCase
33
31
  def build_defendant(last_ancestor, use_case_classes)
34
32
  Class.new(last_ancestor) { depends(*use_case_classes) }
35
33
  end
36
-
37
34
  end
38
-
39
35
  end
40
36
  end
@@ -1,9 +1,9 @@
1
1
  module RestMyCase
2
2
  module Trial
3
-
4
- Court = Struct.new \
5
- :judge_class, :defense_attorney_class, :last_ancestor, :context_class do
6
-
3
+ Court = Struct.new :judge_class,
4
+ :defense_attorney_class,
5
+ :last_ancestor,
6
+ :context_class do
7
7
  def execute(use_case_classes, attributes = {})
8
8
  trial_case = Case.new \
9
9
  last_ancestor, context_class, use_case_classes, attributes
@@ -14,8 +14,6 @@ module RestMyCase
14
14
 
15
15
  trial_case
16
16
  end
17
-
18
17
  end
19
-
20
18
  end
21
19
  end
@@ -1,147 +1,25 @@
1
- module RestMyCase
1
+ require 'object_attorney'
2
2
 
3
+ module RestMyCase
3
4
  class Validator < Base
5
+ include ObjectAttorney
4
6
 
5
- module ClassMethods
6
-
7
- attr_writer :validators
8
-
9
- def target_options
10
- @target_options || Helpers.super_method(self, :target_options)
11
- end
12
-
13
- def target(target_name, target_options = {})
14
- @target_options = target_options || {}
15
- @target_options[:name] = target_name
16
- end
7
+ def self.target(name, options = {})
8
+ defend(name, options)
17
9
 
18
- def validators
19
- @validators ||= Hash.new { |hash, key| hash[key] = [] }
10
+ if defendant_options[:in].present?
11
+ context_reader defendant_options[:in]
12
+ else
13
+ context_reader name
20
14
  end
21
-
22
- def validate(*args, &block)
23
- validators[nil] << AccusationAttorneys::Custom.new(args, &block)
24
- end
25
-
26
- def validates_with(*args, &block)
27
- options = Helpers.extract_options!(args)
28
-
29
- options[:class] = self
30
-
31
- args.each do |klass|
32
- store_validators_by_attribute klass.new(options, &block)
33
- end
34
- end
35
-
36
- protected ######################## PROTECTED #############################
37
-
38
- def store_validators_by_attribute(validator)
39
- validator.attributes.each do |attribute|
40
- validators[attribute.to_sym] << validator
41
- end
42
- end
43
-
44
- # Copy validators on inheritance.
45
- def inherited(base)
46
- dup = validators.dup
47
- base.validators = dup.each { |k, v| dup[k] = v.dup }
48
- super
49
- end
50
-
51
15
  end
52
16
 
53
- ######################## INSTANCE METHODS BELLOW ###########################
54
-
55
- extend ClassMethods
56
-
57
17
  trial_court.last_ancestor = Validator
58
18
 
59
19
  self.silence_dependencies_abort = true
60
20
 
61
- extend AccusationAttorneys::HelperMethods
62
-
63
- def target_options
64
- self.class.target_options || {}
65
- end
66
-
67
- def parent_target
68
- @parent_target ||=
69
- target_options[:in] ? get_target(target_options[:in]) : nil
70
- end
71
-
72
- def target
73
- return nil if target_options[:name].nil?
74
-
75
- target_content = get_target(target_options[:name])
76
-
77
- if parent_target
78
- extend_errors_if_necessary(parent_target)
79
-
80
- target_content ||= parent_target.send(target_options[:name])
81
- end
82
-
83
- target_content
84
- end
85
-
86
21
  def perform
87
- targets = [*target]
88
-
89
- return if Helpers.blank?(targets) || all_validations_green?(targets)
90
-
91
- parent_target.errors.add(target_options[:name], :invalid) if parent_target
92
-
93
- error('unprocessable_entity')
22
+ error('unprocessable_entity') if invalid?
94
23
  end
95
-
96
- protected ######################## PROTECTED ###############################
97
-
98
- def get_target(method)
99
- respond_to?(method) ? send(method) : context.send(method)
100
- end
101
-
102
- def all_validations_green?(targets)
103
- targets.map do |object_to_validate|
104
- extend_errors_if_necessary(object_to_validate)
105
-
106
- if Helpers.marked_for_destruction?(object_to_validate)
107
- true
108
- else
109
- run_validations(object_to_validate)
110
-
111
- object_to_validate.errors.empty?
112
- end
113
- end.all?
114
- end
115
-
116
- private ########################### PRIVATE ################################
117
-
118
- def run_validations(object_to_validate)
119
- self.class.validators.values.flatten.uniq.each do |validator|
120
- next if validator_condition_fails(validator, object_to_validate)
121
-
122
- validator.base = self
123
-
124
- validator.validate object_to_validate
125
- end
126
- end
127
-
128
- def extend_errors_if_necessary(object_to_validate)
129
- return true if object_to_validate.respond_to?(:errors)
130
-
131
- object_to_validate.instance_eval do
132
- def errors
133
- @errors ||= AccusationAttorneys::Errors.new(self)
134
- end
135
- end
136
- end
137
-
138
- def validator_condition_fails(validator, object_to_validate)
139
- return false unless validator.options.key?(:if)
140
-
141
- !Helpers.call_proc_or_method \
142
- self, validator.options[:if], object_to_validate
143
- end
144
-
145
24
  end
146
-
147
25
  end
@@ -1,5 +1,3 @@
1
1
  module RestMyCase
2
-
3
- VERSION = '1.11.7'
4
-
2
+ VERSION = '2.0.0'.freeze
5
3
  end