serega 0.1.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/serega/attribute.rb +17 -4
  4. data/lib/serega/config.rb +69 -27
  5. data/lib/serega/convert.rb +7 -7
  6. data/lib/serega/convert_item.rb +4 -4
  7. data/lib/serega/errors.rb +12 -0
  8. data/lib/serega/helpers/serializer_class_helper.rb +1 -1
  9. data/lib/serega/json/adapter.rb +17 -0
  10. data/lib/serega/json/json.rb +17 -0
  11. data/lib/serega/json/oj.rb +17 -0
  12. data/lib/serega/map.rb +25 -15
  13. data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +3 -3
  14. data/lib/serega/plugins/activerecord_preloads/lib/preloader.rb +2 -2
  15. data/lib/serega/plugins/context_metadata/context_metadata.rb +34 -11
  16. data/lib/serega/plugins/formatters/formatters.rb +35 -6
  17. data/lib/serega/plugins/hide_nil/hide_nil.rb +7 -7
  18. data/lib/serega/plugins/metadata/meta_attribute.rb +5 -5
  19. data/lib/serega/plugins/metadata/metadata.rb +30 -5
  20. data/lib/serega/plugins/metadata/validations/check_block.rb +4 -4
  21. data/lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb +3 -3
  22. data/lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb +3 -3
  23. data/lib/serega/plugins/metadata/validations/check_opts.rb +3 -3
  24. data/lib/serega/plugins/metadata/validations/check_path.rb +3 -3
  25. data/lib/serega/plugins/preloads/lib/enum_deep_freeze.rb +1 -1
  26. data/lib/serega/plugins/preloads/lib/format_user_preloads.rb +1 -1
  27. data/lib/serega/plugins/preloads/lib/main_preload_path.rb +1 -1
  28. data/lib/serega/plugins/preloads/lib/preloads_constructor.rb +2 -2
  29. data/lib/serega/plugins/preloads/preloads.rb +63 -5
  30. data/lib/serega/plugins/preloads/validations/check_opt_preload.rb +17 -0
  31. data/lib/serega/plugins/preloads/validations/check_opt_preload_path.rb +4 -4
  32. data/lib/serega/plugins/presenter/presenter.rb +6 -6
  33. data/lib/serega/plugins/root/root.rb +48 -8
  34. data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +1 -1
  35. data/lib/serega/plugins/string_modifiers/string_modifiers.rb +7 -7
  36. data/lib/serega/plugins.rb +7 -7
  37. data/lib/serega/utils/enum_deep_dup.rb +1 -1
  38. data/lib/serega/utils/to_hash.rb +2 -2
  39. data/lib/serega/validations/attribute/check_block.rb +3 -3
  40. data/lib/serega/validations/attribute/check_name.rb +3 -3
  41. data/lib/serega/validations/attribute/check_opt_const.rb +5 -5
  42. data/lib/serega/validations/attribute/check_opt_delegate.rb +57 -0
  43. data/lib/serega/validations/attribute/check_opt_hide.rb +2 -2
  44. data/lib/serega/validations/attribute/check_opt_key.rb +5 -5
  45. data/lib/serega/validations/attribute/check_opt_many.rb +2 -2
  46. data/lib/serega/validations/attribute/check_opt_serializer.rb +3 -3
  47. data/lib/serega/validations/attribute/check_opt_value.rb +7 -7
  48. data/lib/serega/validations/check_attribute_params.rb +4 -3
  49. data/lib/serega/validations/check_initiate_params.rb +23 -10
  50. data/lib/serega/validations/check_serialize_params.rb +16 -10
  51. data/lib/serega/{plugins/validate_modifiers/validate.rb → validations/initiate/check_modifiers.rb} +5 -5
  52. data/lib/serega/validations/utils/check_allowed_keys.rb +2 -2
  53. data/lib/serega/validations/utils/check_opt_is_bool.rb +2 -2
  54. data/lib/serega/validations/utils/check_opt_is_hash.rb +2 -2
  55. data/lib/serega/validations/utils/check_opt_is_string_or_symbol.rb +2 -2
  56. data/lib/serega.rb +52 -72
  57. metadata +9 -6
  58. data/lib/serega/plugins/validate_modifiers/validate_modifiers.rb +0 -44
  59. data/lib/serega/utils/as_json.rb +0 -35
  60. data/lib/serega/utils/to_json.rb +0 -22
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Attribute
6
6
  class CheckBlock
7
7
  class << self
@@ -22,7 +22,7 @@ class Serega
22
22
  # @param opts [Proc] Attribute opts, we will check :value option
23
23
  # @param block [Proc] Block that returns serialized attribute value
24
24
  #
25
- # @raise [Error] Error that block has invalid arguments
25
+ # @raise [SeregaError] SeregaError that block has invalid arguments
26
26
  #
27
27
  # @return [void]
28
28
  #
@@ -38,7 +38,7 @@ class Serega
38
38
  params = block.parameters
39
39
  return if (params.count <= 2) && params.all? { |par| par[0] == :opt }
40
40
 
41
- raise Error, block_error
41
+ raise SeregaError, block_error
42
42
  end
43
43
 
44
44
  def block_error
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Attribute
6
6
  class CheckName
7
7
  FORMAT_ONE_CHAR = /\A[a-zA-Z0-9]\z/
@@ -17,7 +17,7 @@ class Serega
17
17
  #
18
18
  # @param name [String, Symbol] Attribute name
19
19
  #
20
- # @raise [Error] when name has invalid format
20
+ # @raise [SeregaError] when name has invalid format
21
21
  # @return [void]
22
22
  #
23
23
  def call(name)
@@ -32,7 +32,7 @@ class Serega
32
32
 
33
33
  return if valid
34
34
 
35
- raise Error, message(name)
35
+ raise SeregaError, message(name)
36
36
  end
37
37
 
38
38
  private
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Attribute
6
6
  class CheckOptConst
7
7
  #
@@ -9,7 +9,7 @@ class Serega
9
9
  #
10
10
  # @param opts [Hash] Attribute options
11
11
  #
12
- # @raise [Error] Attribute validation error
12
+ # @raise [SeregaError] Attribute validation error
13
13
  #
14
14
  # @return [void]
15
15
  #
@@ -23,9 +23,9 @@ class Serega
23
23
  private
24
24
 
25
25
  def check_usage_with_other_params(opts, block)
26
- raise Error, "Option :const can not be used together with option :key" if opts.key?(:key)
27
- raise Error, "Option :const can not be used together with option :value" if opts.key?(:value)
28
- raise Error, "Option :const can not be used together with block" if block
26
+ raise SeregaError, "Option :const can not be used together with option :key" if opts.key?(:key)
27
+ raise SeregaError, "Option :const can not be used together with option :value" if opts.key?(:value)
28
+ raise SeregaError, "Option :const can not be used together with block" if block
29
29
  end
30
30
  end
31
31
  end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Serega
4
+ module SeregaValidations
5
+ module Attribute
6
+ class CheckOptDelegate
7
+ #
8
+ # Checks attribute :delegate option
9
+ # It must have :to option and can have :optional allow_nil option
10
+ #
11
+ # @param opts [Hash] Attribute options
12
+ #
13
+ # @raise [SeregaError] Attribute validation error
14
+ #
15
+ # @return [void]
16
+ #
17
+ class << self
18
+ def call(opts, block = nil)
19
+ return unless opts.key?(:delegate)
20
+
21
+ check_opt_delegate(opts)
22
+ check_usage_with_other_params(opts, block)
23
+ end
24
+
25
+ private
26
+
27
+ def check_opt_delegate(opts)
28
+ Utils::CheckOptIsHash.call(opts, :delegate)
29
+
30
+ delegate_opts = opts[:delegate]
31
+ check_opt_delegate_to(delegate_opts)
32
+ check_opt_delegate_allow_nil(delegate_opts)
33
+ end
34
+
35
+ def check_opt_delegate_to(delegate_opts)
36
+ to_exist = delegate_opts.key?(:to)
37
+ raise SeregaError, "Option :delegate must have a :to option" unless to_exist
38
+
39
+ Utils::CheckOptIsStringOrSymbol.call(delegate_opts, :to)
40
+ end
41
+
42
+ def check_opt_delegate_allow_nil(delegate_opts)
43
+ return unless delegate_opts.key?(:allow_nil)
44
+
45
+ Utils::CheckOptIsBool.call(delegate_opts, :allow_nil)
46
+ end
47
+
48
+ def check_usage_with_other_params(opts, block)
49
+ raise SeregaError, "Option :delegate can not be used together with option :const" if opts.key?(:const)
50
+ raise SeregaError, "Option :delegate can not be used together with option :value" if opts.key?(:value)
51
+ raise SeregaError, "Option :delegate can not be used together with block" if block
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Attribute
6
6
  class CheckOptHide
7
7
  #
@@ -9,7 +9,7 @@ class Serega
9
9
  #
10
10
  # @param opts [Hash] Attribute options
11
11
  #
12
- # @raise [Error] Error that option has invalid value
12
+ # @raise [SeregaError] SeregaError that option has invalid value
13
13
  #
14
14
  # @return [void]
15
15
  #
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Attribute
6
6
  class CheckOptKey
7
7
  #
@@ -9,7 +9,7 @@ class Serega
9
9
  #
10
10
  # @param opts [Hash] Attribute options
11
11
  #
12
- # @raise [Error] Error that option has invalid value
12
+ # @raise [SeregaError] SeregaError that option has invalid value
13
13
  #
14
14
  # @return [void]
15
15
  #
@@ -24,9 +24,9 @@ class Serega
24
24
  private
25
25
 
26
26
  def check_usage_with_other_params(opts, block)
27
- raise Error, "Option :key can not be used together with option :const" if opts.key?(:const)
28
- raise Error, "Option :key can not be used together with option :value" if opts.key?(:value)
29
- raise Error, "Option :key can not be used together with block" if block
27
+ raise SeregaError, "Option :key can not be used together with option :const" if opts.key?(:const)
28
+ raise SeregaError, "Option :key can not be used together with option :value" if opts.key?(:value)
29
+ raise SeregaError, "Option :key can not be used together with block" if block
30
30
  end
31
31
  end
32
32
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Attribute
6
6
  class CheckOptMany
7
7
  #
@@ -9,7 +9,7 @@ class Serega
9
9
  #
10
10
  # @param opts [Hash] Attribute options
11
11
  #
12
- # @raise [Error] Error that option has invalid value
12
+ # @raise [SeregaError] SeregaError that option has invalid value
13
13
  #
14
14
  # @return [void]
15
15
  #
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Attribute
6
6
  class CheckOptSerializer
7
7
  class << self
@@ -10,7 +10,7 @@ class Serega
10
10
  #
11
11
  # @param opts [Hash] Attribute options
12
12
  #
13
- # @raise [Error] Error that option has invalid value
13
+ # @raise [SeregaError] SeregaError that option has invalid value
14
14
  #
15
15
  # @return [void]
16
16
  #
@@ -20,7 +20,7 @@ class Serega
20
20
  value = opts[:serializer]
21
21
  return if valid_serializer?(value)
22
22
 
23
- raise Error, "Invalid option :serializer => #{value.inspect}." \
23
+ raise SeregaError, "Invalid option :serializer => #{value.inspect}." \
24
24
  " Can be a Serega subclass, a String or a Proc without arguments"
25
25
  end
26
26
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Attribute
6
6
  class CheckOptValue
7
7
  #
@@ -9,7 +9,7 @@ class Serega
9
9
  #
10
10
  # @param opts [Hash] Attribute options
11
11
  #
12
- # @raise [Error] Error that option has invalid value
12
+ # @raise [SeregaError] SeregaError that option has invalid value
13
13
  #
14
14
  # @return [void]
15
15
  #
@@ -24,13 +24,13 @@ class Serega
24
24
  private
25
25
 
26
26
  def check_usage_with_other_params(opts, block)
27
- raise Error, "Option :value can not be used together with option :key" if opts.key?(:key)
28
- raise Error, "Option :value can not be used together with option :const" if opts.key?(:const)
29
- raise Error, "Option :value can not be used together with block" if block
27
+ raise SeregaError, "Option :value can not be used together with option :key" if opts.key?(:key)
28
+ raise SeregaError, "Option :value can not be used together with option :const" if opts.key?(:const)
29
+ raise SeregaError, "Option :value can not be used together with block" if block
30
30
  end
31
31
 
32
32
  def check_proc(value)
33
- raise Error, value_error unless value.is_a?(Proc)
33
+ raise SeregaError, value_error unless value.is_a?(Proc)
34
34
 
35
35
  params = value.parameters
36
36
 
@@ -40,7 +40,7 @@ class Serega
40
40
  return
41
41
  end
42
42
 
43
- raise Error, value_error
43
+ raise SeregaError, value_error
44
44
  end
45
45
 
46
46
  def value_error
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  class CheckAttributeParams
6
6
  module InstanceMethods
7
7
  attr_reader :name, :opts, :block
@@ -28,6 +28,7 @@ class Serega
28
28
  Utils::CheckAllowedKeys.call(opts, allowed_opts_keys)
29
29
 
30
30
  Attribute::CheckOptConst.call(opts, block)
31
+ Attribute::CheckOptDelegate.call(opts, block)
31
32
  Attribute::CheckOptHide.call(opts)
32
33
  Attribute::CheckOptKey.call(opts, block)
33
34
  Attribute::CheckOptMany.call(opts)
@@ -40,12 +41,12 @@ class Serega
40
41
  end
41
42
 
42
43
  def allowed_opts_keys
43
- self.class.serializer_class.config[:attribute_keys]
44
+ self.class.serializer_class.config.attribute_keys
44
45
  end
45
46
  end
46
47
 
47
48
  include InstanceMethods
48
- extend Serega::Helpers::SerializerClassHelper
49
+ extend Serega::SeregaHelpers::SerializerClassHelper
49
50
  end
50
51
  end
51
52
  end
@@ -1,26 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  class CheckInitiateParams
6
- module ClassMethods
7
- def call(opts)
8
- check_opts(opts)
6
+ module InstanceMethods
7
+ attr_reader :opts
8
+
9
+ def initialize(opts)
10
+ @opts = opts
11
+ end
12
+
13
+ def validate
14
+ check_allowed_keys
15
+ check_modifiers
9
16
  end
10
17
 
11
18
  private
12
19
 
13
- def check_opts(opts)
14
- Utils::CheckAllowedKeys.call(opts, allowed_opts_keys)
20
+ def check_allowed_keys
21
+ Utils::CheckAllowedKeys.call(opts, serializer_class.config.initiate_keys)
22
+ end
23
+
24
+ def check_modifiers
25
+ Initiate::CheckModifiers.call(serializer_class, opts[:only])
26
+ Initiate::CheckModifiers.call(serializer_class, opts[:except])
27
+ Initiate::CheckModifiers.call(serializer_class, opts[:with])
15
28
  end
16
29
 
17
- def allowed_opts_keys
18
- serializer_class.config[:initiate_keys]
30
+ def serializer_class
31
+ self.class.serializer_class
19
32
  end
20
33
  end
21
34
 
22
- extend ClassMethods
23
- extend Serega::Helpers::SerializerClassHelper
35
+ include InstanceMethods
36
+ extend Serega::SeregaHelpers::SerializerClassHelper
24
37
  end
25
38
  end
26
39
  end
@@ -1,29 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  class CheckSerializeParams
6
- module ClassMethods
7
- def call(opts)
8
- check_opts(opts)
6
+ module InstanceMethods
7
+ attr_reader :opts
8
+
9
+ def initialize(opts)
10
+ @opts = opts
11
+ end
12
+
13
+ def validate
14
+ check_opts
9
15
  end
10
16
 
11
17
  private
12
18
 
13
- def check_opts(opts)
14
- Utils::CheckAllowedKeys.call(opts, allowed_opts_keys)
19
+ def check_opts
20
+ Utils::CheckAllowedKeys.call(opts, serializer_class.config.serialize_keys)
15
21
 
16
22
  Utils::CheckOptIsHash.call(opts, :context)
17
23
  Utils::CheckOptIsBool.call(opts, :many)
18
24
  end
19
25
 
20
- def allowed_opts_keys
21
- serializer_class.config[:serialize_keys]
26
+ def serializer_class
27
+ self.class.serializer_class
22
28
  end
23
29
  end
24
30
 
25
- extend ClassMethods
26
- extend Serega::Helpers::SerializerClassHelper
31
+ include InstanceMethods
32
+ extend Serega::SeregaHelpers::SerializerClassHelper
27
33
  end
28
34
  end
29
35
  end
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Plugins
5
- module ValidateModifiers
6
- class Validate
4
+ module SeregaValidations
5
+ module Initiate
6
+ class CheckModifiers
7
7
  class << self
8
8
  def call(serializer_class, fields)
9
9
  return unless fields
@@ -29,14 +29,14 @@ class Serega
29
29
  def raise_error(name, prev_names)
30
30
  field_name = field_name(name, prev_names)
31
31
 
32
- raise Serega::Error, "Attribute #{field_name} not exists"
32
+ raise Serega::AttributeNotExist, "Attribute #{field_name} not exists"
33
33
  end
34
34
 
35
35
  def raise_nested_error(name, prev_names, nested_fields)
36
36
  field_name = field_name(name, prev_names)
37
37
  first_nested = nested_fields.keys.first
38
38
 
39
- raise Serega::Error, "Attribute #{field_name} is not a relation to add '#{first_nested}' attribute"
39
+ raise Serega::AttributeNotExist, "Attribute #{field_name} has no :serializer option specified to add nested '#{first_nested}' attribute"
40
40
  end
41
41
 
42
42
  def field_name(name, prev_names)
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Utils
6
6
  class CheckAllowedKeys
7
7
  def self.call(opts, allowed_keys)
8
8
  opts.each_key do |key|
9
9
  next if allowed_keys.include?(key)
10
10
 
11
- raise Error, "Invalid option #{key.inspect}. Allowed options are: #{allowed_keys.map(&:inspect).join(", ")}"
11
+ raise SeregaError, "Invalid option #{key.inspect}. Allowed options are: #{allowed_keys.map(&:inspect).join(", ")}"
12
12
  end
13
13
  end
14
14
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Utils
6
6
  class CheckOptIsBool
7
7
  def self.call(opts, key)
@@ -10,7 +10,7 @@ class Serega
10
10
  value = opts[key]
11
11
  return if value.equal?(true) || value.equal?(false)
12
12
 
13
- raise Error, "Invalid option #{key.inspect} => #{value.inspect}. Must have a boolean value"
13
+ raise SeregaError, "Invalid option #{key.inspect} => #{value.inspect}. Must have a boolean value"
14
14
  end
15
15
  end
16
16
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Utils
6
6
  class CheckOptIsHash
7
7
  def self.call(opts, key)
@@ -10,7 +10,7 @@ class Serega
10
10
  value = opts[key]
11
11
  return if value.is_a?(Hash)
12
12
 
13
- raise Error, "Invalid option #{key.inspect} => #{value.inspect}. Must have a Hash value"
13
+ raise SeregaError, "Invalid option #{key.inspect} => #{value.inspect}. Must have a Hash value"
14
14
  end
15
15
  end
16
16
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
4
+ module SeregaValidations
5
5
  module Utils
6
6
  class CheckOptIsStringOrSymbol
7
7
  def self.call(opts, key)
@@ -10,7 +10,7 @@ class Serega
10
10
  value = opts[key]
11
11
  return if value.is_a?(String) || value.is_a?(Symbol)
12
12
 
13
- raise Error, "Invalid option #{key.inspect} => #{value.inspect}. Must be a String or a Symbol"
13
+ raise SeregaError, "Invalid option #{key.inspect} => #{value.inspect}. Must be a String or a Symbol"
14
14
  end
15
15
  end
16
16
  end