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
-
#as_json(object, opts = nil) ⇒ Hash
Serializes provided object as JSON.
-
#attribute(name, **opts, &block) ⇒ Serega::SeregaAttribute
Adds attribute.
-
#attributes ⇒ Hash
Lists attributes.
-
#call(object, opts = nil) ⇒ Hash
Serializes provided object to Hash.
-
#plugin(name, **opts) ⇒ class<Module>
Enables plugin for current serializer.
-
#plugin_used?(name) ⇒ Boolean
Checks plugin is used.
- #to_h(object, opts = nil) ⇒ Object
-
#to_json(object, opts = nil) ⇒ String
Serializes provided object to JSON string.
Instance Attribute Details
#config ⇒ SeregaConfig (readonly)
Returns current config
72 73 74 |
# File 'lib/serega.rb', line 72 def config @config end |
Instance Method Details
#as_json(object, opts = nil) ⇒ Hash
Serializes provided object as JSON
250 251 252 |
# File 'lib/serega.rb', line 250 def as_json(object, opts = nil) config.from_json.call(to_json(object, opts)) end |
#attribute(name, **opts, &block) ⇒ Serega::SeregaAttribute
Adds attribute
179 180 181 182 |
# File 'lib/serega.rb', line 179 def attribute(name, **opts, &block) attribute = self::SeregaAttribute.new(name: name, opts: opts, block: block) attributes[attribute.name] = attribute end |
#attributes ⇒ Hash
Lists attributes
166 167 168 |
# File 'lib/serega.rb', line 166 def attributes @attributes ||= {} end |
#call(object, opts = nil) ⇒ Hash
Serializes provided object to Hash
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/serega.rb', line 198 def call(object, opts = nil) opts ||= FROZEN_EMPTY_HASH initiate_keys = config.initiate_keys if opts.empty? modifiers_opts = FROZEN_EMPTY_HASH serialize_opts = nil else serialize_opts = opts.except(*initiate_keys) modifiers_opts = opts.slice(*initiate_keys) end new(modifiers_opts).to_h(object, serialize_opts) end |
#plugin(name, **opts) ⇒ class<Module>
Enables plugin for current serializer
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/serega.rb', line 124 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
151 152 153 154 155 156 157 158 159 |
# File 'lib/serega.rb', line 151 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 |
#to_h(object, opts = nil) ⇒ Object
214 215 216 |
# File 'lib/serega.rb', line 214 def to_h(object, opts = nil) call(object, opts) end |
#to_json(object, opts = nil) ⇒ String
Serializes provided object to JSON string
232 233 234 |
# File 'lib/serega.rb', line 232 def to_json(object, opts = nil) config.to_json.call(to_h(object, opts)) end |