Module: Serega::ClassMethods
- Included in:
- Serega
- Defined in:
- lib/serega.rb
Overview
Core serializer class methods
Instance Attribute Summary collapse
-
#config ⇒ Config
readonly
Current serializer config.
Instance Method Summary collapse
- #as_json(object, opts = FROZEN_EMPTY_HASH) ⇒ Object
-
#attribute(name, **opts, &block) ⇒ Serega::Attribute
Adds attribute.
-
#attributes ⇒ Hash
Lists attributes.
-
#plugin(name, **opts) ⇒ class<Module>
Enables plugin for current serializer.
-
#plugin_used?(name) ⇒ Boolean
Checks plugin is used.
-
#relation(name, serializer:, **opts, &block) ⇒ Serega::Attribute
Adds attribute with forced :serializer option.
- #to_h(object, opts = FROZEN_EMPTY_HASH) ⇒ Object
- #to_json(object, opts = FROZEN_EMPTY_HASH) ⇒ Object
Instance Attribute Details
#config ⇒ Config (readonly)
Returns current serializer config.
56 57 58 |
# File 'lib/serega.rb', line 56 def config @config end |
Instance Method Details
#as_json(object, opts = FROZEN_EMPTY_HASH) ⇒ Object
180 181 182 183 |
# File 'lib/serega.rb', line 180 def as_json(object, opts = FROZEN_EMPTY_HASH) initiate_keys = config[:initiate_keys] new(opts.slice(*initiate_keys)).as_json(object, opts.except(*initiate_keys)) end |
#attribute(name, **opts, &block) ⇒ Serega::Attribute
Adds attribute
151 152 153 154 |
# File 'lib/serega.rb', line 151 def attribute(name, **opts, &block) attribute = self::Attribute.new(name: name, opts: opts, block: block) attributes[attribute.name] = attribute end |
#attributes ⇒ Hash
Lists attributes
138 139 140 |
# File 'lib/serega.rb', line 138 def attributes @attributes ||= {} end |
#plugin(name, **opts) ⇒ class<Module>
Enables plugin for current serializer
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/serega.rb', line 96 def plugin(name, **opts) raise Error, "This plugin is already loaded" if plugin_used?(name) plugin = Plugins.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
123 124 125 126 127 128 129 130 131 |
# File 'lib/serega.rb', line 123 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 |
#relation(name, serializer:, **opts, &block) ⇒ Serega::Attribute
Adds attribute with forced :serializer option
166 167 168 |
# File 'lib/serega.rb', line 166 def relation(name, serializer:, **opts, &block) attribute(name, serializer: serializer, **opts, &block) end |
#to_h(object, opts = FROZEN_EMPTY_HASH) ⇒ Object
170 171 172 173 |
# File 'lib/serega.rb', line 170 def to_h(object, opts = FROZEN_EMPTY_HASH) initiate_keys = config[:initiate_keys] new(opts.slice(*initiate_keys)).to_h(object, opts.except(*initiate_keys)) end |
#to_json(object, opts = FROZEN_EMPTY_HASH) ⇒ Object
175 176 177 178 |
# File 'lib/serega.rb', line 175 def to_json(object, opts = FROZEN_EMPTY_HASH) initiate_keys = config[:initiate_keys] new(opts.slice(*initiate_keys)).to_json(object, opts.except(*initiate_keys)) end |