Sha256: eff16550edb72548b788e009869ebcabe9c07146125b1b6ed57c4ea3e116b9d9

Contents?: true

Size: 738 Bytes

Versions: 5

Compression:

Stored size: 738 Bytes

Contents

require_relative 'association'

module Alba
  # Representing one association
  class One < Association
    # Recursively converts an object into a Hash
    #
    # @param target [Object] the object having an association method
    # @param within [Hash] determines what associations to be serialized. If not set, it serializes all associations.
    # @param params [Hash] user-given Hash for arbitrary data
    # @return [Hash]
    def to_hash(target, within: nil, params: {})
      @object = target.public_send(@name)
      @object = @condition.call(object, params) if @condition
      return if @object.nil?

      @resource = constantize(@resource)
      @resource.new(object, params: params, within: within).to_hash
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alba-1.5.0 lib/alba/one.rb
alba-1.4.0 lib/alba/one.rb
alba-1.3.0 lib/alba/one.rb
alba-1.2.0 lib/alba/one.rb
alba-1.1.0 lib/alba/one.rb