class Alba::Association

Base class for `One` and `Many` Child class should implement `to_hash` method

Attributes

name[R]
object[R]

Public Class Methods

new(name:, condition: nil, resource: nil, nesting: nil, &block) click to toggle source

@param name [Symbol, String] name of the method to fetch association @param condition [Proc, nil] a proc filtering data @param resource [Class<Alba::Resource>, nil] a resource class for the association @param nesting [String] a namespace where source class is inferred with @param block [Block] used to define resource when resource arg is absent

# File lib/alba/association.rb, line 12
def initialize(name:, condition: nil, resource: nil, nesting: nil, &block)
  @name = name
  @condition = condition
  @block = block
  @resource = resource
  return if @resource

  assign_resource(nesting)
end

Private Instance Methods

assign_resource(nesting) click to toggle source
# File lib/alba/association.rb, line 33
def assign_resource(nesting)
  @resource = if @block
                Alba.resource_class(&@block)
              elsif Alba.inferring
                Alba.infer_resource_class(@name, nesting: nesting)
              else
                raise ArgumentError, 'When Alba.inferring is false, either resource or block is required'
              end
end
constantize(resource) click to toggle source
# File lib/alba/association.rb, line 24
def constantize(resource)
  case resource # rubocop:disable Style/MissingElse
  when Class
    resource
  when Symbol, String
    Object.const_get(resource)
  end
end