Module: Alba::Resource
- Extended by:
- ClassMethods
- Includes:
- InstanceMethods
- Defined in:
- lib/alba/resource.rb,
sig/alba/resource.rbs
Overview
This module represents what should be serialized
Defined Under Namespace
Modules: ClassMethods, InstanceMethods, _ClassContext
Constant Summary collapse
- INTERNAL_VARIABLES =
{_attributes: {}, _key: nil, _key_for_collection: nil, _meta: nil, _transform_type: :none, _transforming_root_key: false, _key_transformation_cascade: true, _on_error: nil, _on_nil: nil, _layout: nil, _collection_key: nil, _helper: nil, _resource_methods: [], _select_arity: nil, _traits: {}}.freeze
- WITHIN_DEFAULT =
Object.new.freeze
- EMPTY_HASH =
{}.freeze
Instance Attribute Summary collapse
-
#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
- ._resource_methods ⇒ Array[Symbol] extended from ClassMethods
- .alias_method ⇒ Symbol extended from ClassMethods
- .assign_attributes(attrs, if_value) ⇒ Object extended from ClassMethods
- .assign_attributes_with_types(attrs_with_types, if_value, &block) ⇒ void extended from ClassMethods
-
.association(name, condition = nil, resource: nil, serializer: nil, source: nil, key: nil, with_traits: nil, params: EMPTY_HASH, **options, &block) ⇒ void
(also: #one, #many, #has_one, #has_many)
extended
from ClassMethods
Set association.
-
.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
Set multiple attributes at once.
- .class_eval ⇒ void extended from ClassMethods
-
.collection_key(key) ⇒ void
extended
from ClassMethods
Sets key for collection serialization.
- .dup ⇒ Object extended from ClassMethods
- .extend ⇒ Object extended from ClassMethods
-
.helper(mod = @_helper || Module.new, &block) ⇒ void
extended
from ClassMethods
Define helper methods.
- .include ⇒ Object extended from ClassMethods
-
.included(base) ⇒ Object
private
setupmethod is meta-programmatically defined here for performance. - .inherited(subclass) ⇒ Object extended from ClassMethods private
- .instance_method ⇒ UnboundMethod extended from ClassMethods
-
.layout(file: nil, inline: nil) ⇒ void
extended
from ClassMethods
Set layout.
-
.meta(key = :meta, &block) ⇒ void
extended
from ClassMethods
Set metadata.
-
.method_added(method_name) ⇒ Object
extended
from ClassMethods
This
method_addedis used for defining "resource methods". - .module_eval ⇒ void extended from ClassMethods
- .name ⇒ Object extended from ClassMethods
-
.nested_attribute(name, **options, &block) ⇒ void
(also: #nested)
extended
from ClassMethods
Set a nested attribute with the given block.
- .nesting ⇒ String? extended from ClassMethods
- .new ⇒ Object extended from ClassMethods
-
.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.
- .private ⇒ Object extended from ClassMethods
-
.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.
-
.trait(name, &block) ⇒ void
extended
from ClassMethods
Set a trait.
-
.transform_keys(type, root: true, cascade: true) ⇒ void
extended
from ClassMethods
Transform keys as specified type.
-
.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.
- .validated_error_handler(handler) ⇒ Symbol extended from ClassMethods
Instance Method Summary collapse
- #__send__ ⇒ Object included from InstanceMethods
- #_fetch_attribute_from_object_first(obj, attribute) ⇒ Object included from InstanceMethods
- #_fetch_attribute_from_resource_first(obj, attribute) ⇒ Object included from InstanceMethods
- #_key ⇒ root_key_type included from InstanceMethods
- #_key_for_collection ⇒ root_key_type included from InstanceMethods
- #_metadata(block, meta) ⇒ generic_hash included from InstanceMethods
- #_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.
-
#attributes ⇒ Hash[Symbol | String, attribute]
included
from InstanceMethods
deprecated
Deprecated.
in favor of
select - #attributes_to_hash(obj, hash) ⇒ generic_hash included from InstanceMethods
- #binding ⇒ Binding included from InstanceMethods
- #check_within(association_name) ⇒ Object included from InstanceMethods
-
#class ⇒ Class
included
from InstanceMethods
These methods are available on the eventual resource instance.
- #collection_converter ⇒ Proc included from InstanceMethods
- #converter ⇒ Proc included from InstanceMethods
- #deprecated_serializable_hash ⇒ Object included from InstanceMethods
- #deprecated_serializable_hash_for_collection ⇒ Object included from InstanceMethods
- #do_select(key, value, attribute) ⇒ Boolean included from InstanceMethods
- #encode ⇒ String included from InstanceMethods
-
#fetch_attribute(obj, key, attribute) ⇒ Object
included
from InstanceMethods
rubocop:disable Metrics.
- #fetch_attribute_from_object_and_resource(obj, attribute) ⇒ Object included from InstanceMethods
- #fetch_key ⇒ String included from InstanceMethods
- #handle_error(error, obj, key, attribute, hash) ⇒ Object included from InstanceMethods
- #hash_from_traits(obj) ⇒ Object included from InstanceMethods
- #hash_with_metadata(hash, meta) ⇒ Object included from InstanceMethods
-
#initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ InstanceMethods
included
from InstanceMethods
A new instance of InstanceMethods.
- #instance_eval ⇒ void included from InstanceMethods
- #instance_exec ⇒ void included from InstanceMethods
- #nil_handler ⇒ Object included from InstanceMethods
- #resource_name(pluralized: false) ⇒ String 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.
- #serializable_hash_for_collection ⇒ Object included from InstanceMethods
-
#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String
included
from InstanceMethods
Serialize object into JSON string.
- #serialize_with(hash) ⇒ String included from InstanceMethods
- #set_key_and_attribute_body_from(obj, key, attribute, hash) ⇒ Object 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) ⇒ Object included from InstanceMethods
- #transforming_root_key? ⇒ Boolean included from InstanceMethods
- #yield_if_within(association_name) {|arg0| ... } ⇒ Object included from InstanceMethods
Instance Attribute Details
#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
._resource_methods ⇒ Array[Symbol] Originally defined in module ClassMethods
.alias_method ⇒ Symbol Originally defined in module ClassMethods
.assign_attributes(attrs, if_value) ⇒ Object Originally defined in module ClassMethods
.assign_attributes_with_types(attrs_with_types, if_value, &block) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
.association(name, condition = nil, resource: nil, serializer: nil, source: nil, key: nil, with_traits: nil, params: EMPTY_HASH, **options, &block) ⇒ void Also known as: one, many, has_one, has_many Originally defined in module ClassMethods
This method returns an undefined value.
Set association
.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.
Set multiple attributes at once
.class_eval ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
.collection_key(key) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Sets key for collection serialization
.dup ⇒ Object Originally defined in module ClassMethods
.extend ⇒ Object Originally defined in module ClassMethods
.helper(mod = @_helper || Module.new, &block) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Define helper methods
.include ⇒ Object Originally defined in module ClassMethods
.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.
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.
.instance_method ⇒ UnboundMethod Originally defined in module ClassMethods
.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.
Set metadata
.method_added(method_name) ⇒ Object Originally defined in module ClassMethods
This method_added is used for defining "resource methods"
.module_eval ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
.name ⇒ Object Originally defined in module ClassMethods
.nested_attribute(name, **options, &block) ⇒ void Also known as: nested Originally defined in module ClassMethods
This method returns an undefined value.
Set a nested attribute with the given block
.nesting ⇒ String? Originally defined in module ClassMethods
.new ⇒ Object Originally defined in module ClassMethods
.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
.private ⇒ Object Originally defined in module ClassMethods
.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
.trait(name, &block) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Set a trait
.transform_keys(type, root: true, cascade: true) ⇒ void Originally defined in module ClassMethods
This method returns an undefined value.
Transform keys as specified type
.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
.validated_error_handler(handler) ⇒ Symbol Originally defined in module ClassMethods
Instance Method Details
#__send__ ⇒ Object Originally defined in module InstanceMethods
#_fetch_attribute_from_object_first(obj, attribute) ⇒ Object Originally defined in module InstanceMethods
#_fetch_attribute_from_resource_first(obj, attribute) ⇒ Object Originally defined in module InstanceMethods
#_key ⇒ root_key_type Originally defined in module InstanceMethods
#_key_for_collection ⇒ root_key_type Originally defined in module InstanceMethods
#_metadata(block, meta) ⇒ generic_hash Originally defined in module InstanceMethods
#_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
#attributes ⇒ Hash[Symbol | String, attribute] Originally defined in module InstanceMethods
in favor of select
This is default behavior for getting attributes for serialization Override this method to filter certain attributes
#attributes_to_hash(obj, hash) ⇒ generic_hash Originally defined in module InstanceMethods
#binding ⇒ Binding Originally defined in module InstanceMethods
#check_within(association_name) ⇒ Object Originally defined in module InstanceMethods
#class ⇒ Class Originally defined in module InstanceMethods
These methods are available on the eventual resource instance. Declaring them here models the module's inclusion context for Steep.
#collection_converter ⇒ Proc Originally defined in module InstanceMethods
#converter ⇒ Proc Originally defined in module InstanceMethods
#deprecated_serializable_hash ⇒ Object Originally defined in module InstanceMethods
#deprecated_serializable_hash_for_collection ⇒ Object Originally defined in module InstanceMethods
#do_select(key, value, 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_attribute_from_object_and_resource(obj, attribute) ⇒ Object Originally defined in module InstanceMethods
#fetch_key ⇒ String Originally defined in module InstanceMethods
#handle_error(error, obj, key, attribute, hash) ⇒ Object Originally defined in module InstanceMethods
#hash_from_traits(obj) ⇒ Object Originally defined in module InstanceMethods
#hash_with_metadata(hash, meta) ⇒ Object Originally defined in module InstanceMethods
#initialize(object, params: EMPTY_HASH, within: WITHIN_DEFAULT, with_traits: nil, select: nil) ⇒ InstanceMethods Originally defined in module InstanceMethods
Returns a new instance of InstanceMethods.
#instance_eval ⇒ void Originally defined in module InstanceMethods
This method returns an undefined value.
#instance_exec ⇒ void Originally defined in module InstanceMethods
This method returns an undefined value.
#nil_handler ⇒ Object Originally defined in module InstanceMethods
#resource_name(pluralized: false) ⇒ String 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
#serializable_hash_for_collection ⇒ Object Originally defined in module InstanceMethods
#serialize(root_key: nil, meta: EMPTY_HASH) ⇒ String Originally defined in module InstanceMethods
Serialize object into JSON string
#serialize_with(hash) ⇒ String Originally defined in module InstanceMethods
#set_key_and_attribute_body_from(obj, key, attribute, hash) ⇒ Object 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