Module: Alba::Resource

Extended by:
ClassMethods
Includes:
InstanceMethods
Defined in:
lib/alba/resource.rb

Overview

This module represents what should be serialized

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Instance Attribute Summary

Attributes included from InstanceMethods

#object, #params

Class Method Summary collapse

Methods included from ClassMethods

association, attribute, attributes, collection_key, helper, inherited, layout, meta, method_added, nested_attribute, on_error, on_nil, prefer_object_method!, prefer_resource_method!, root_key, root_key!, root_key_for_collection, trait, transform_keys, transform_keys!

Methods included from InstanceMethods

#as_json, #initialize, #serializable_hash, #serialize, #to_json

Class Method Details

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

‘setup` method is meta-programmatically defined here for performance.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/alba/resource.rb', line 25

def self.included(base) # rubocop:disable Metrics/MethodLength
  super
  base.class_eval do
    # Initialize
    setup_method_body = +'private def _setup;'
    INTERNAL_VARIABLES.each do |name, initial|
      instance_variable_set(:"@#{name}", initial.dup) unless instance_variable_defined?(:"@#{name}")
      setup_method_body << "@#{name} = self.class.#{name};"
    end
    setup_method_body << 'end'
    class_eval(setup_method_body, __FILE__, __LINE__ + 1)
    define_method(:encode, Alba.encoder)
  end
  base.include InstanceMethods
  base.extend ClassMethods
end