Module: Alba::Resource
- Extended by:
- ClassMethods
- Includes:
- InstanceMethods
- Defined in:
- lib/alba/resource.rb,
sig/alba/resource.rbs
Overview
Main Resource module that gets included into resource classes
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Instance Attribute Summary collapse
-
#_attributes ⇒ generic_hash
included
from ClassMethods
Internal attribute accessors.
-
#_collection_key ⇒ Alba::key_type
included
from ClassMethods
Returns the value of attribute _collection_key.
-
#_helper ⇒ Module?
included
from ClassMethods
Returns the value of attribute _helper.
-
#_key ⇒ Alba::key_type
included
from ClassMethods
Returns the value of attribute _key.
-
#_key_for_collection ⇒ Alba::key_type
included
from ClassMethods
Returns the value of attribute _key_for_collection.
-
#_key_transformation_cascade ⇒ Boolean
included
from ClassMethods
Returns the value of attribute _key_transformation_cascade.
-
#_layout ⇒ Object
included
from ClassMethods
Returns the value of attribute _layout.
-
#_meta ⇒ Object
included
from ClassMethods
Returns the value of attribute _meta.
-
#_on_error ⇒ Symbol?
included
from ClassMethods
Returns the value of attribute _on_error.
-
#_on_nil ⇒ Symbol?
included
from ClassMethods
Returns the value of attribute _on_nil.
-
#_resource_methods ⇒ Array[Symbol]
included
from ClassMethods
Returns the value of attribute _resource_methods.
-
#_select_arity ⇒ Integer?
included
from ClassMethods
Returns the value of attribute _select_arity.
-
#_traits ⇒ Hash[Symbol, untyped]
included
from ClassMethods
Returns the value of attribute _traits.
-
#_transform_type ⇒ Alba::transform_type
included
from ClassMethods
Returns the value of attribute _transform_type.
-
#_transforming_root_key ⇒ Boolean
included
from ClassMethods
Returns the value of attribute _transforming_root_key.
-
#object ⇒ Object
included
from InstanceMethods
readonly
Returns the value of attribute object.
-
#params ⇒ generic_hash
included
from InstanceMethods
readonly
Returns the value of attribute params.
Class Method Summary collapse
-
.association(name, condition = nil, resource: nil, serializer: nil, source: nil, key: nil, with_traits: nil, params: EMPTY_HASH, **options, &block) ⇒ Object
(also: #one, #many, #has_one, #has_many)
extended
from ClassMethods
Association DSL methods.
-
.attribute(name = nil, if: nil, **name_with_type, &block) ⇒ void
extended
from ClassMethods
Set an attribute with the given block.
-
.attributes(*attrs, if: nil, **attrs_with_types) ⇒ void
extended
from ClassMethods
DSL Methods for defining attributes.
- .belongs_to ⇒ Object extended from ClassMethods
-
.collection_key(key) ⇒ void
extended
from ClassMethods
Sets key for collection serialization.
-
.helper(mod = @_helper || Module.new, &block) ⇒ void
extended
from ClassMethods
Helper methods.
-
.ignoring { ... } ⇒ void
extended
from ClassMethods
Ignoring attributes.
-
.included(base) ⇒ void
Module inclusion hook.
- .inherited(subclass) ⇒ Object extended from ClassMethods private
-
.layout(file: nil, inline: nil) ⇒ void
extended
from ClassMethods
Set layout.
-
.meta(key = :meta, &block) ⇒ void
extended
from ClassMethods
Meta and params.
-
.method_added(method_name) ⇒ Object
extended
from ClassMethods
This
method_addedis used for defining "resource methods". -
.nested_attribute(name, **options) { ... } ⇒ void
(also: #nested)
extended
from ClassMethods
Nested attributes.
-
.on_error(handler = nil, &block) ⇒ void
extended
from ClassMethods
Set error handler If this is set it's used as a error handler overriding global one.
-
.on_nil(&block) ⇒ void
extended
from ClassMethods
Set nil handler.
-
.prefer_object_method! ⇒ void
extended
from ClassMethods
DSL for alias, purely for readability.
-
.prefer_resource_method! ⇒ void
extended
from ClassMethods
DSL for alias, purely for readability.
-
.root_key(key, key_for_collection = nil) ⇒ void
extended
from ClassMethods
Set root key.
-
.root_key! ⇒ void
extended
from ClassMethods
Set root key to true.
-
.root_key_for_collection(key) ⇒ void
extended
from ClassMethods
Set root key for collection.
-
.select ⇒ Boolean
extended
from ClassMethods
Selecting attributes.
-
.trait(name) { ... } ⇒ void
extended
from ClassMethods
Traits.
-
.transform_keys(type, root: true, cascade: true) ⇒ void
extended
from ClassMethods
Transform and configuration.
-
.transform_keys!(type) ⇒ Object
extended
from ClassMethods
Transform keys as specified type AFTER the class is defined Note that this is an experimental API and may be removed/changed.
-
.typed_attribute ⇒ void
extended
from ClassMethods
Type checking.
Instance Method Summary collapse
- #_setup ⇒ void included from InstanceMethods
-
#as_json(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Hash
included
from InstanceMethods
Returns a Hash corresponding InstanceMethods#serialize.
- #assoc_name_from_params ⇒ Symbol, String included from InstanceMethods
- #association_editor ⇒ Module? included from InstanceMethods
- #attributes_from_association ⇒ [Alba::key_type, untyped] included from InstanceMethods
- #conditional_attribute? ⇒ Boolean included from InstanceMethods
- #encode ⇒ String included from InstanceMethods
-
#fetch_attribute(obj, key, attribute) ⇒ Object
included
from InstanceMethods
rubocop:disable Metrics.
- #fetch_key ⇒ String included from InstanceMethods
- #handle_error(error, obj, key, attribute, hash) ⇒ Object included from InstanceMethods
- #hash_with_metadata(hash, meta) ⇒ generic_hash included from InstanceMethods
-
#initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ Object
included
from InstanceMethods
Initialization.
- #on_nil ⇒ Object included from InstanceMethods
-
#select(_key, _value, _attribute) ⇒ Boolean
included
from InstanceMethods
Default implementation for selecting attributes Override this method to filter attributes based on key and value.
-
#serializable_hash ⇒ Hash
(also: #to_h)
included
from InstanceMethods
A Hash for serialization.
-
#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String
included
from InstanceMethods
Serialization methods.
- #serialize_with(hash) ⇒ String included from InstanceMethods
-
#to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ String
included
from InstanceMethods
For Rails compatibility The first options is a dummy parameter.
- #transform_key(key) ⇒ Alba::key_type included from InstanceMethods
- #transform_keys ⇒ generic_hash included from InstanceMethods
- #transformable_root? ⇒ Boolean included from InstanceMethods
Instance Attribute Details
#_attributes ⇒ generic_hash Originally defined in module ClassMethods
Internal attribute accessors
#_collection_key ⇒ Alba::key_type Originally defined in module ClassMethods
Returns the value of attribute _collection_key.
#_helper ⇒ Module? Originally defined in module ClassMethods
Returns the value of attribute _helper.
#_key ⇒ Alba::key_type Originally defined in module ClassMethods
Returns the value of attribute _key.
#_key_for_collection ⇒ Alba::key_type Originally defined in module ClassMethods
Returns the value of attribute _key_for_collection.
#_key_transformation_cascade ⇒ Boolean Originally defined in module ClassMethods
Returns the value of attribute _key_transformation_cascade.
#_layout ⇒ Object Originally defined in module ClassMethods
Returns the value of attribute _layout.
#_meta ⇒ Object Originally defined in module ClassMethods
Returns the value of attribute _meta.
#_on_error ⇒ Symbol? Originally defined in module ClassMethods
Returns the value of attribute _on_error.
#_on_nil ⇒ Symbol? Originally defined in module ClassMethods
Returns the value of attribute _on_nil.
#_resource_methods ⇒ Array[Symbol] Originally defined in module ClassMethods
Returns the value of attribute _resource_methods.
#_select_arity ⇒ Integer? Originally defined in module ClassMethods
Returns the value of attribute _select_arity.
#_traits ⇒ Hash[Symbol, untyped] Originally defined in module ClassMethods
Returns the value of attribute _traits.
#_transform_type ⇒ Alba::transform_type Originally defined in module ClassMethods
Returns the value of attribute _transform_type.
#_transforming_root_key ⇒ Boolean Originally defined in module ClassMethods
Returns the value of attribute _transforming_root_key.
#object ⇒ Object (readonly) Originally defined in module InstanceMethods
Returns the value of attribute object.
#params ⇒ generic_hash (readonly) Originally defined in module InstanceMethods
Returns the value of attribute params.
Class Method Details
.association(name, condition = nil, resource: nil, serializer: nil, source: nil, key: nil, with_traits: nil, params: EMPTY_HASH, **options, &block) ⇒ Object Also known as: one, many, has_one, has_many Originally defined in module ClassMethods
Association DSL methods
.attribute(name = nil, if: nil, **name_with_type, &block) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Set an attribute with the given block
.attributes(*attrs, if: nil, **attrs_with_types) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
DSL Methods for defining attributes
.belongs_to ⇒ Object Originally defined in module ClassMethods
.collection_key(key) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Sets key for collection serialization
.helper(mod = @_helper || Module.new, &block) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Helper methods
.ignoring { ... } ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Ignoring attributes
.included(base) ⇒ void
This method returns an undefined value.
Module inclusion hook
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/alba/resource.rb', line 28 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 |
.inherited(subclass) ⇒ Object Originally defined in module ClassMethods
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.
.layout(file: nil, inline: nil) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Set layout
.meta(key = :meta, &block) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Meta and params
.method_added(method_name) ⇒ Object Originally defined in module ClassMethods
This method_added is used for defining "resource methods"
.nested_attribute(name, **options) { ... } ⇒ void Also known as: nested Originally defined in module ClassMethods
This method returns an undefined value.
Nested attributes
.on_error(handler = nil, &block) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Set error handler If this is set it's used as a error handler overriding global one
.on_nil(&block) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Set nil handler
.prefer_object_method! ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
DSL for alias, purely for readability
.prefer_resource_method! ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
DSL for alias, purely for readability
.root_key(key, key_for_collection = nil) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Set root key
.root_key! ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Set root key to true
.root_key_for_collection(key) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Set root key for collection
.select ⇒ Boolean Originally defined in module ClassMethods
Selecting attributes
.trait(name) { ... } ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Traits
.transform_keys(type, root: true, cascade: true) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Transform and configuration
.transform_keys!(type) ⇒ Object Originally defined in module ClassMethods
Transform keys as specified type AFTER the class is defined Note that this is an experimental API and may be removed/changed
.typed_attribute ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Type checking
Instance Method Details
#_setup ⇒ void Originally defined in module InstanceMethods
This method returns an undefined value.
#as_json(_options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ Hash Originally defined in module InstanceMethods
Returns a Hash corresponding #serialize
#assoc_name_from_params ⇒ Symbol, String Originally defined in module InstanceMethods
#association_editor ⇒ Module? Originally defined in module InstanceMethods
#attributes_from_association ⇒ [Alba::key_type, untyped] Originally defined in module InstanceMethods
#conditional_attribute? ⇒ Boolean Originally defined in module InstanceMethods
#encode ⇒ String Originally defined in module InstanceMethods
#fetch_attribute(obj, key, attribute) ⇒ Object Originally defined in module InstanceMethods
rubocop:disable Metrics
#fetch_key ⇒ String Originally defined in module InstanceMethods
#handle_error(error, obj, key, attribute, hash) ⇒ Object Originally defined in module InstanceMethods
#hash_with_metadata(hash, meta) ⇒ generic_hash Originally defined in module InstanceMethods
#initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ Object Originally defined in module InstanceMethods
Initialization
#on_nil ⇒ Object Originally defined in module InstanceMethods
#select(_key, _value, _attribute) ⇒ Boolean Originally defined in module InstanceMethods
Default implementation for selecting attributes Override this method to filter attributes based on key and value
#serializable_hash ⇒ Hash Also known as: to_h Originally defined in module InstanceMethods
A Hash for serialization
#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String Originally defined in module InstanceMethods
Serialization methods
#serialize_with(hash) ⇒ String Originally defined in module InstanceMethods
#to_json(options = EMPTY_HASH, root_key: nil, meta: EMPTY_HASH) ⇒ String Originally defined in module InstanceMethods
For Rails compatibility The first options is a dummy parameter