Module: Serega::ClassMethods
- Included in:
- Serega
- Defined in:
- lib/serega.rb
Overview
Serializers class methods
Instance Attribute Summary collapse
-
#config ⇒ SeregaConfig
readonly
Returns current config.
Instance Method Summary collapse
-
#attribute(name, **opts, &block) ⇒ Serega::SeregaAttribute
Adds attribute.
-
#attributes ⇒ Hash
Lists attributes.
-
#batch(name, value = nil, &block) ⇒ #call
Defines a batch loader.
-
#batch_loaders ⇒ Hash
Lists defined batch loaders.
-
#call(object, opts = nil) ⇒ Hash
(also: #to_h)
Serializes provided object to Hash.
-
#plugin(name, **opts) ⇒ class<Module>
Enables plugin for current serializer.
-
#plugin_used?(name) ⇒ Boolean
Checks plugin is used.
-
#preload_with(value = nil, &block) ⇒ #call?
Registers (or returns) the handler used to preload an attribute's associations onto the records gathered during serialization.
-
#to_data(object, opts = nil) ⇒ Data, ...
Serializes provided object to a tree of Ruby Data objects.
Instance Attribute Details
#config ⇒ SeregaConfig (readonly)
Returns current config
99 100 101 |
# File 'lib/serega.rb', line 99 def config @config end |
Instance Method Details
#attribute(name, **opts, &block) ⇒ Serega::SeregaAttribute
Adds attribute
Patched in:
- plugin :presenter (additionally adds method in Presenter class)
175 176 177 178 |
# File 'lib/serega.rb', line 175 def attribute(name, **opts, &block) attribute = self::SeregaAttribute.new(name: name, opts: opts, block: block) attributes[attribute.name] = attribute end |
#attributes ⇒ Hash
Lists attributes
150 151 152 |
# File 'lib/serega.rb', line 150 def attributes @attributes ||= {} end |
#batch(name, value = nil, &block) ⇒ #call
Defines a batch loader
208 209 210 211 212 213 |
# File 'lib/serega.rb', line 208 def batch(name, value = nil, &block) raise SeregaError, "Batch loader must be defined with a callable value or block" if (value && block) || (!value && !block) batch_loader = self::SeregaEngineLoader.new(name: name, block: value || block) batch_loaders[batch_loader.name] = batch_loader end |
#batch_loaders ⇒ Hash
Lists defined batch loaders
159 160 161 |
# File 'lib/serega.rb', line 159 def batch_loaders @batch_loaders ||= {} end |
#call(object, opts = nil) ⇒ Hash Also known as: to_h
Serializes provided object to Hash
261 262 263 264 265 266 |
# File 'lib/serega.rb', line 261 def call(object, opts = nil) opts = opts&.transform_keys(&:to_sym) modifiers_opts = init_modifier_opts(opts) serialize_opts = init_serialize_opts(opts) new(modifiers_opts).to_h(object, serialize_opts) end |
#plugin(name, **opts) ⇒ class<Module>
Enables plugin for current serializer
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/serega.rb', line 108 def plugin(name, **opts) raise SeregaError, "This plugin is already loaded" if plugin_used?(name) plugin = SeregaPlugins.find_plugin(name) # We split loading of plugin to three parts - before_load, load, after_load: # # - **before_load_plugin** usually used to check requirements and to load additional plugins # - **load_plugin** usually used to include plugin modules # - **after_load_plugin** usually used to add config options plugin.before_load_plugin(self, **opts) if plugin.respond_to?(:before_load_plugin) plugin.load_plugin(self, **opts) if plugin.respond_to?(:load_plugin) plugin.after_load_plugin(self, **opts) if plugin.respond_to?(:after_load_plugin) # Store attached plugins, so we can check it is loaded later config.plugins << (plugin.respond_to?(:plugin_name) ? plugin.plugin_name : plugin) plugin end |
#plugin_used?(name) ⇒ Boolean
Checks plugin is used
135 136 137 138 139 140 141 142 143 |
# File 'lib/serega.rb', line 135 def plugin_used?(name) plugin_name = case name when Module then name.respond_to?(:plugin_name) ? name.plugin_name : name else name end config.plugins.include?(plugin_name) end |
#preload_with(value = nil, &block) ⇒ #call?
Registers (or returns) the handler used to preload an attribute's associations onto the records gathered during serialization.
The handler is called once per preloaded attribute with the gathered objects and that attribute's preloads. ORM plugins register a handler that performs the actual eager loading.
234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/serega.rb', line 234 def preload_with(value = nil, &block) return @preload_with if value.nil? && block.nil? raise SeregaError, "preload_with accepts a single callable or a block, not both" if value && block handler = value || block raise SeregaError, "preload_with value must be a Proc or respond to #call" if !handler.is_a?(Proc) && !handler.respond_to?(:call) signature = SeregaUtils::MethodSignature.call(handler, pos_limit: 2) raise SeregaError, "preload_with handler must accept two positional arguments: (objects, preloads)" unless signature == "2" @preload_with = handler end |
#to_data(object, opts = nil) ⇒ Data, ...
Serializes provided object to a tree of Ruby Data objects
282 283 284 285 286 287 |
# File 'lib/serega.rb', line 282 def to_data(object, opts = nil) opts = opts&.transform_keys(&:to_sym) modifiers_opts = init_modifier_opts(opts) serialize_opts = init_serialize_opts(opts) new(modifiers_opts).to_data(object, serialize_opts) end |