Sha256: e23cd57fba9bbad2543dabcd1da4b1e5d81a3c72b3ad8ec06a15b0e02b566771

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

module Alba
  # Base class for `One` and `Many`
  # Child class should implement `to_hash` method
  class Association
    attr_reader :object

    # @param name [Symbol] name of the method to fetch association
    # @param condition [Proc] a proc filtering data
    # @param resource [Class<Alba::Resource>] a resource class for the association
    # @param block [Block] used to define resource when resource arg is absent
    def initialize(name:, condition: nil, resource: nil, nesting: nil, &block)
      @name = name
      @condition = condition
      @block = block
      @resource = resource
      return if @resource

      if @block
        @resource = resource_class
      elsif Alba.inferring
        const_parent = nesting.nil? ? Object : Object.const_get(nesting)
        @resource = const_parent.const_get("#{ActiveSupport::Inflector.classify(@name)}Resource")
      else
        raise ArgumentError, 'When Alba.inferring is false, either resource or block is required'
      end
    end

    private

    def constantize(resource)
      case resource # rubocop:disable Style/MissingElse
      when Class
        resource
      when Symbol, String
        Object.const_get(resource)
      end
    end

    def resource_class
      klass = Class.new
      klass.include(Alba::Resource)
      klass.class_eval(&@block)
      klass
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
alba-1.2.0 lib/alba/association.rb
alba-1.1.0 lib/alba/association.rb
alba-1.0.1 lib/alba/association.rb