Sha256: 063fb8b11a723dbfda8e5602c68ccaeebd73ab2a8ac36ef5e49116d211652bed

Contents?: true

Size: 626 Bytes

Versions: 2

Compression:

Stored size: 626 Bytes

Contents

require_relative 'association'

module Alba
  # Representing many association
  class Many < Association
    # Recursively converts objects into an Array of Hashes
    #
    # @param target [Object] the object having an association method
    # @param params [Hash] user-given Hash for arbitrary data
    # @return [Array<Hash>]
    def to_hash(target, params: {})
      @object = target.public_send(@name)
      @object = @condition.call(@object, params) if @condition
      return if @object.nil?

      @resource = constantize(@resource)
      @object.map { |o| @resource.new(o, params: params).to_hash }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alba-1.0.1 lib/alba/many.rb
alba-1.0.0 lib/alba/many.rb