lib/alba/association.rb in alba-1.3.0 vs lib/alba/association.rb in alba-1.4.0
- old
+ new
@@ -2,13 +2,14 @@
# Base class for `One` and `Many`
# Child class should implement `to_hash` method
class Association
attr_reader :object, :name
- # @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 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
def initialize(name:, condition: nil, resource: nil, nesting: nil, &block)
@name = name
@condition = condition
@block = block
@@ -29,26 +30,14 @@
end
end
def assign_resource(nesting)
@resource = if @block
- resource_class
+ Alba.resource_class(&@block)
elsif Alba.inferring
- resource_class_with_nesting(nesting)
+ Alba.infer_resource_class(@name, nesting: nesting)
else
raise ArgumentError, 'When Alba.inferring is false, either resource or block is required'
end
- end
-
- def resource_class
- klass = Class.new
- klass.include(Alba::Resource)
- klass.class_eval(&@block)
- klass
- end
-
- def resource_class_with_nesting(nesting)
- const_parent = nesting.nil? ? Object : Object.const_get(nesting)
- const_parent.const_get("#{ActiveSupport::Inflector.classify(@name)}Resource")
end
end
end