serega 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) 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 +5 -5
  5. data/lib/serega/convert.rb +7 -7
  6. data/lib/serega/convert_item.rb +4 -4
  7. data/lib/serega/helpers/serializer_class_helper.rb +1 -1
  8. data/lib/serega/map.rb +2 -2
  9. data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +3 -3
  10. data/lib/serega/plugins/activerecord_preloads/lib/preloader.rb +2 -2
  11. data/lib/serega/plugins/context_metadata/context_metadata.rb +9 -13
  12. data/lib/serega/plugins/formatters/formatters.rb +11 -3
  13. data/lib/serega/plugins/hide_nil/hide_nil.rb +6 -6
  14. data/lib/serega/plugins/metadata/meta_attribute.rb +4 -4
  15. data/lib/serega/plugins/metadata/metadata.rb +3 -3
  16. data/lib/serega/plugins/metadata/validations/check_block.rb +4 -4
  17. data/lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb +3 -3
  18. data/lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb +3 -3
  19. data/lib/serega/plugins/metadata/validations/check_opts.rb +3 -3
  20. data/lib/serega/plugins/metadata/validations/check_path.rb +3 -3
  21. data/lib/serega/plugins/preloads/lib/enum_deep_freeze.rb +1 -1
  22. data/lib/serega/plugins/preloads/lib/format_user_preloads.rb +1 -1
  23. data/lib/serega/plugins/preloads/lib/main_preload_path.rb +1 -1
  24. data/lib/serega/plugins/preloads/lib/preloads_constructor.rb +2 -2
  25. data/lib/serega/plugins/preloads/preloads.rb +26 -4
  26. data/lib/serega/plugins/preloads/validations/check_opt_preload.rb +17 -0
  27. data/lib/serega/plugins/preloads/validations/check_opt_preload_path.rb +4 -4
  28. data/lib/serega/plugins/presenter/presenter.rb +6 -6
  29. data/lib/serega/plugins/root/root.rb +3 -3
  30. data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +1 -1
  31. data/lib/serega/plugins/string_modifiers/string_modifiers.rb +1 -1
  32. data/lib/serega/plugins/validate_modifiers/validate.rb +6 -3
  33. data/lib/serega/plugins/validate_modifiers/validate_modifiers.rb +1 -1
  34. data/lib/serega/plugins.rb +7 -7
  35. data/lib/serega/utils/as_json.rb +1 -1
  36. data/lib/serega/utils/enum_deep_dup.rb +1 -1
  37. data/lib/serega/utils/to_hash.rb +2 -2
  38. data/lib/serega/utils/to_json.rb +1 -1
  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 +3 -3
  44. data/lib/serega/validations/attribute/check_opt_key.rb +6 -6
  45. data/lib/serega/validations/attribute/check_opt_many.rb +3 -3
  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 +3 -3
  50. data/lib/serega/validations/check_serialize_params.rb +5 -5
  51. data/lib/serega/validations/utils/check_allowed_keys.rb +3 -3
  52. data/lib/serega/validations/utils/check_opt_is_bool.rb +3 -3
  53. data/lib/serega/validations/utils/check_opt_is_hash.rb +3 -3
  54. data/lib/serega/validations/utils/check_opt_is_string_or_symbol.rb +3 -3
  55. data/lib/serega.rb +30 -43
  56. metadata +4 -2
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
5
- module Utils
4
+ module SeregaValidations
5
+ module SeregaUtils
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,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
5
- module Utils
4
+ module SeregaValidations
5
+ module SeregaUtils
6
6
  class CheckOptIsBool
7
7
  def self.call(opts, key)
8
8
  return unless opts.key?(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,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
5
- module Utils
4
+ module SeregaValidations
5
+ module SeregaUtils
6
6
  class CheckOptIsHash
7
7
  def self.call(opts, key)
8
8
  return unless opts.key?(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,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Serega
4
- module Validations
5
- module Utils
4
+ module SeregaValidations
5
+ module SeregaUtils
6
6
  class CheckOptIsStringOrSymbol
7
7
  def self.call(opts, key)
8
8
  return unless opts.key?(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
data/lib/serega.rb CHANGED
@@ -5,7 +5,7 @@ require_relative "serega/version"
5
5
  # Parent class for your serializers
6
6
  class Serega
7
7
  # A generic exception Serega uses.
8
- class Error < StandardError; end
8
+ class SeregaError < StandardError; end
9
9
 
10
10
  # @return [Hash] frozen hash
11
11
  FROZEN_EMPTY_HASH = {}.freeze
@@ -29,6 +29,7 @@ require_relative "serega/validations/attribute/check_block"
29
29
  require_relative "serega/validations/attribute/check_name"
30
30
  require_relative "serega/validations/attribute/check_opt_const"
31
31
  require_relative "serega/validations/attribute/check_opt_hide"
32
+ require_relative "serega/validations/attribute/check_opt_delegate"
32
33
  require_relative "serega/validations/attribute/check_opt_key"
33
34
  require_relative "serega/validations/attribute/check_opt_many"
34
35
  require_relative "serega/validations/attribute/check_opt_serializer"
@@ -44,55 +45,55 @@ require_relative "serega/map"
44
45
  require_relative "serega/plugins"
45
46
 
46
47
  class Serega
47
- @config = Config.new(
48
+ @config = SeregaConfig.new(
48
49
  {
49
50
  plugins: [],
50
51
  initiate_keys: %i[only with except],
51
- attribute_keys: %i[key value serializer many hide const],
52
+ attribute_keys: %i[key value serializer many hide const delegate],
52
53
  serialize_keys: %i[context many],
53
54
  max_cached_map_per_serializer_count: 50,
54
- to_json: ->(data) { Utils::ToJSON.call(data) }
55
+ to_json: ->(data) { SeregaUtils::ToJSON.call(data) }
55
56
  }
56
57
  )
57
58
 
58
- check_attribute_params_class = Class.new(Validations::CheckAttributeParams)
59
+ check_attribute_params_class = Class.new(SeregaValidations::CheckAttributeParams)
59
60
  check_attribute_params_class.serializer_class = self
60
61
  const_set(:CheckAttributeParams, check_attribute_params_class)
61
62
 
62
- check_initiate_params_class = Class.new(Validations::CheckInitiateParams)
63
+ check_initiate_params_class = Class.new(SeregaValidations::CheckInitiateParams)
63
64
  check_initiate_params_class.serializer_class = self
64
65
  const_set(:CheckInitiateParams, check_initiate_params_class)
65
66
 
66
- check_serialize_params_class = Class.new(Validations::CheckSerializeParams)
67
+ check_serialize_params_class = Class.new(SeregaValidations::CheckSerializeParams)
67
68
  check_serialize_params_class.serializer_class = self
68
69
  const_set(:CheckSerializeParams, check_serialize_params_class)
69
70
 
70
71
  # Core serializer class methods
71
72
  module ClassMethods
72
- # @return [Config] current serializer config
73
+ # @return [SeregaConfig] current serializer config
73
74
  attr_reader :config
74
75
 
75
76
  private def inherited(subclass)
76
- config_class = Class.new(self::Config)
77
+ config_class = Class.new(self::SeregaConfig)
77
78
  config_class.serializer_class = subclass
78
- subclass.const_set(:Config, config_class)
79
- subclass.instance_variable_set(:@config, subclass::Config.new(config.opts))
79
+ subclass.const_set(:SeregaConfig, config_class)
80
+ subclass.instance_variable_set(:@config, subclass::SeregaConfig.new(config.opts))
80
81
 
81
- attribute_class = Class.new(self::Attribute)
82
+ attribute_class = Class.new(self::SeregaAttribute)
82
83
  attribute_class.serializer_class = subclass
83
- subclass.const_set(:Attribute, attribute_class)
84
+ subclass.const_set(:SeregaAttribute, attribute_class)
84
85
 
85
- map_class = Class.new(self::Map)
86
+ map_class = Class.new(self::SeregaMap)
86
87
  map_class.serializer_class = subclass
87
- subclass.const_set(:Map, map_class)
88
+ subclass.const_set(:SeregaMap, map_class)
88
89
 
89
- convert_class = Class.new(self::Convert)
90
+ convert_class = Class.new(self::SeregaConvert)
90
91
  convert_class.serializer_class = subclass
91
- subclass.const_set(:Convert, convert_class)
92
+ subclass.const_set(:SeregaConvert, convert_class)
92
93
 
93
- convert_item_class = Class.new(self::ConvertItem)
94
+ convert_item_class = Class.new(self::SeregaConvertItem)
94
95
  convert_item_class.serializer_class = subclass
95
- subclass.const_set(:ConvertItem, convert_item_class)
96
+ subclass.const_set(:SeregaConvertItem, convert_item_class)
96
97
 
97
98
  check_attribute_params_class = Class.new(self::CheckAttributeParams)
98
99
  check_attribute_params_class.serializer_class = subclass
@@ -123,9 +124,9 @@ class Serega
123
124
  # @return [class<Module>] Loaded plugin module
124
125
  #
125
126
  def plugin(name, **opts)
126
- raise Error, "This plugin is already loaded" if plugin_used?(name)
127
+ raise SeregaError, "This plugin is already loaded" if plugin_used?(name)
127
128
 
128
- plugin = Plugins.find_plugin(name)
129
+ plugin = SeregaPlugins.find_plugin(name)
129
130
 
130
131
  # We split loading of plugin to three parts - before_load, load, after_load:
131
132
  #
@@ -175,27 +176,13 @@ class Serega
175
176
  # @param opts [Hash] Options to serialize attribute
176
177
  # @param block [Proc] Custom block to find attribute value. Accepts object and context.
177
178
  #
178
- # @return [Serega::Attribute] Added attribute
179
+ # @return [Serega::SeregaAttribute] Added attribute
179
180
  #
180
181
  def attribute(name, **opts, &block)
181
- attribute = self::Attribute.new(name: name, opts: opts, block: block)
182
+ attribute = self::SeregaAttribute.new(name: name, opts: opts, block: block)
182
183
  attributes[attribute.name] = attribute
183
184
  end
184
185
 
185
- #
186
- # Adds attribute with forced :serializer option
187
- #
188
- # @param name [Symbol] Attribute name. Attribute value will be found by executing `object.<name>`
189
- # @param serializer [Serega, Proc] Specifies nested serializer for relationship
190
- # @param opts [Hash] Options for attribute serialization
191
- # @param block [Proc] Custom block to find attribute value. Accepts object and context.
192
- #
193
- # @return [Serega::Attribute] Added attribute
194
- #
195
- def relation(name, serializer:, **opts, &block)
196
- attribute(name, serializer: serializer, **opts, &block)
197
- end
198
-
199
186
  def call(object, opts = FROZEN_EMPTY_HASH)
200
187
  initiate_keys = config[:initiate_keys]
201
188
  new(opts.slice(*initiate_keys)).to_h(object, opts.except(*initiate_keys))
@@ -247,7 +234,7 @@ class Serega
247
234
  self.class::CheckSerializeParams.call(opts)
248
235
  opts[:context] ||= {}
249
236
 
250
- self.class::Convert.call(object, **opts, map: map)
237
+ self.class::SeregaConvert.call(object, **opts, map: map)
251
238
  end
252
239
 
253
240
  # @see #call
@@ -278,20 +265,20 @@ class Serega
278
265
  #
279
266
  def as_json(object, opts = FROZEN_EMPTY_HASH)
280
267
  hash = to_h(object, opts)
281
- Utils::AsJSON.call(hash, to_json: self.class.config[:to_json])
268
+ SeregaUtils::AsJSON.call(hash, to_json: self.class.config[:to_json])
282
269
  end
283
270
 
284
271
  private
285
272
 
286
273
  def map
287
- @map ||= self.class::Map.call(opts)
274
+ @map ||= self.class::SeregaMap.call(opts)
288
275
  end
289
276
 
290
277
  def prepare_modifiers(opts)
291
278
  {
292
- only: Utils::ToHash.call(opts[:only]),
293
- except: Utils::ToHash.call(opts[:except]),
294
- with: Utils::ToHash.call(opts[:with])
279
+ only: SeregaUtils::ToHash.call(opts[:only]),
280
+ except: SeregaUtils::ToHash.call(opts[:except]),
281
+ with: SeregaUtils::ToHash.call(opts[:with])
295
282
  }
296
283
  end
297
284
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Glushkov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-25 00:00:00.000000000 Z
11
+ date: 2022-08-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -43,6 +43,7 @@ files:
43
43
  - lib/serega/plugins/preloads/lib/main_preload_path.rb
44
44
  - lib/serega/plugins/preloads/lib/preloads_constructor.rb
45
45
  - lib/serega/plugins/preloads/preloads.rb
46
+ - lib/serega/plugins/preloads/validations/check_opt_preload.rb
46
47
  - lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
47
48
  - lib/serega/plugins/presenter/presenter.rb
48
49
  - lib/serega/plugins/root/root.rb
@@ -57,6 +58,7 @@ files:
57
58
  - lib/serega/validations/attribute/check_block.rb
58
59
  - lib/serega/validations/attribute/check_name.rb
59
60
  - lib/serega/validations/attribute/check_opt_const.rb
61
+ - lib/serega/validations/attribute/check_opt_delegate.rb
60
62
  - lib/serega/validations/attribute/check_opt_hide.rb
61
63
  - lib/serega/validations/attribute/check_opt_key.rb
62
64
  - lib/serega/validations/attribute/check_opt_many.rb