module Alba::Resource::ClassMethods
Class methods
Public Instance Methods
Set an attribute with the given block
@param name [String, Symbol] key name @param options [Hash<Symbol, Proc>] @option options [Proc] if a condition to decide if this attribute should be serialized @param block [Block] the block called during serialization @raise [ArgumentError] if block is absent @return [void]
# File lib/alba/resource.rb, line 245 def attribute(name, **options, &block) raise ArgumentError, 'No block given in attribute method' unless block @_attributes[name.to_sym] = options[:if] ? [block, options[:if]] : block end
Set multiple attributes at once
@param attrs [Array<String, Symbol>] @param if [Proc] condition to decide if it should serialize these attributes @param attrs_with_types [Hash<[Symbol, String], [Array<Symbol, Proc>, Symbol]>]
attributes with name in its key and type and optional type converter in its value
@return [void]
# File lib/alba/resource.rb, line 212 def attributes(*attrs, if: nil, **attrs_with_types) # rubocop:disable Naming/MethodParameterName if_value = binding.local_variable_get(:if) assign_attributes(attrs, if_value) assign_attributes_with_types(attrs_with_types, if_value) end
Delete attributes Use this DSL in child class to ignore certain attributes
@param attributes [Array<String, Symbol>]
# File lib/alba/resource.rb, line 330 def ignoring(*attributes) attributes.each do |attr_name| @_attributes.delete(attr_name.to_sym) end end
@private
# File lib/alba/resource.rb, line 197 def inherited(subclass) super DSLS.each_key { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}").clone) } end
Set key
@param key [String, Symbol] @deprecated Use {#root_key} instead
# File lib/alba/resource.rb, line 291 def key(key) warn '[DEPRECATION] `key` is deprecated, use `root_key` instead.' @_key = key.respond_to?(:to_sym) ? key.to_sym : key end
Set key to true
@deprecated Use {#root_key!} instead
# File lib/alba/resource.rb, line 309 def key! warn '[DEPRECATION] `key!` is deprecated, use `root_key!` instead.' @_key = true @_key_for_collection = true end
Set Many
association
@param name [String, Symbol] name of the association, used as key when `key` param doesn't exist @param condition [Proc, nil] a Proc to filter the collection @param resource [Class<Alba::Resource>, String, nil] representing resource for this association @param key [String, Symbol, nil] used as key when given @param options [Hash<Symbol, Proc>] @option options [Proc] if a condition to decide if this association should be serialized @param block [Block] @return [void] @see Alba::Many#initialize
# File lib/alba/resource.rb, line 280 def many(name, condition = nil, resource: nil, key: nil, **options, &block) nesting = self.name&.rpartition('::')&.first many = Many.new(name: name, condition: condition, resource: resource, nesting: nesting, &block) @_attributes[key&.to_sym || name.to_sym] = options[:if] ? [many, options[:if]] : many end
Set metadata
# File lib/alba/resource.rb, line 322 def meta(&block) @_meta = block end
Set error handler If this is set it's used as a error handler overriding global one
@param handler [Symbol] `:raise`, `:ignore` or `:nullify` @param block [Block]
# File lib/alba/resource.rb, line 350 def on_error(handler = nil, &block) raise ArgumentError, 'You cannot specify error handler with both Symbol and block' if handler && block raise ArgumentError, 'You must specify error handler with either Symbol or block' unless handler || block @_on_error = handler || block end
Set One
association
@param name [String, Symbol] name of the association, used as key when `key` param doesn't exist @param condition [Proc, nil] a Proc to modify the association @param resource [Class<Alba::Resource>, String, nil] representing resource for this association @param key [String, Symbol, nil] used as key when given @param options [Hash<Symbol, Proc>] @option options [Proc] if a condition to decide if this association should be serialized @param block [Block] @return [void] @see Alba::One#initialize
# File lib/alba/resource.rb, line 262 def one(name, condition = nil, resource: nil, key: nil, **options, &block) nesting = self.name&.rpartition('::')&.first one = One.new(name: name, condition: condition, resource: resource, nesting: nesting, &block) @_attributes[key&.to_sym || name.to_sym] = options[:if] ? [one, options[:if]] : one end
Set root key
@param key [String, Symbol] @param key_for_collection [String, Symbol] @raise [NoMethodError] when key doesn't respond to `to_sym` method
# File lib/alba/resource.rb, line 301 def root_key(key, key_for_collection = nil) @_key = key.to_sym @_key_for_collection = key_for_collection&.to_sym end
Set root key to true
# File lib/alba/resource.rb, line 316 def root_key! @_key = true @_key_for_collection = true end
Transform keys as specified type
@param type [String, Symbol] @param root [Boolean] decides if root key also should be transformed
# File lib/alba/resource.rb, line 340 def transform_keys(type, root: nil) @_transform_key_function = KeyTransformFactory.create(type.to_sym) @_transforming_root_key = root end
Private Instance Methods
# File lib/alba/resource.rb, line 218 def assign_attributes(attrs, if_value) attrs.each do |attr_name| attr = if_value ? [attr_name.to_sym, if_value] : attr_name.to_sym @_attributes[attr_name.to_sym] = attr end end
# File lib/alba/resource.rb, line 226 def assign_attributes_with_types(attrs_with_types, if_value) attrs_with_types.each do |attr_name, type_and_converter| attr_name = attr_name.to_sym type, type_converter = type_and_converter typed_attr = TypedAttribute.new(name: attr_name, type: type, converter: type_converter) attr = if_value ? [typed_attr, if_value] : typed_attr @_attributes[attr_name] = attr end end