class Pragma::Decorator::Association::Bond

Links an association definition to a specific decorator instance, allowing to render it.

@api private

Attributes

decorator[R]

@!attribute [r] reflection

@return [Reflection] the association reflection

@!attribute [r] decorator

@return [Pragma::Decorator::Base] the decorator instance
reflection[R]

@!attribute [r] reflection

@return [Reflection] the association reflection

@!attribute [r] decorator

@return [Pragma::Decorator::Base] the decorator instance

Public Class Methods

new(reflection:, decorator:) click to toggle source

Initializes the bond.

@param reflection [Reflection] the association reflection @param decorator [Pragma::Decorator::Base] the decorator instance

# File lib/pragma/decorator/association/bond.rb, line 21
def initialize(reflection:, decorator:)
  @reflection = reflection
  @decorator = decorator
end

Public Instance Methods

associated_object() click to toggle source

Returns the associated object.

@return [Object]

# File lib/pragma/decorator/association/bond.rb, line 29
def associated_object
  case reflection.options[:exec_context]
  when :decorated
    model.public_send(reflection.attribute)
  when :decorator
    decorator.public_send(reflection.attribute)
  end
end
expanded_value(user_options) click to toggle source

Returns the expanded value for the associated object.

If a decorator was specified for the association, first decorates the associated object, then calls #to_hash to render it as a hash.

If no decorator was specified, calls #as_json on the associated object.

In any case, passes all nested associations as the expand user option of the method called.

@param user_options [Array]

@return [Hash]

# File lib/pragma/decorator/association/bond.rb, line 58
def expanded_value(user_options)
  full_object = adapter.full_object
  return unless full_object

  options = {
    user_options: user_options.merge(
      expand: flatten_expand(user_options[:expand])
    )
  }

  decorator_klass = compute_decorator

  if decorator_klass
    decorator_klass.new(full_object).to_hash(options)
  else
    full_object.as_json(options)
  end
end
model() click to toggle source

Returns the model associated to this bond.

@return [Object]

# File lib/pragma/decorator/association/bond.rb, line 94
def model
  decorator.decorated
end
render(user_options) click to toggle source

Renders the unexpanded or expanded associations, depending on the expand user option passed to the decorator.

@param user_options [Array]

@return [Hash|Pragma::Decorator::Base]

# File lib/pragma/decorator/association/bond.rb, line 83
def render(user_options)
  if user_options[:expand]&.any? { |value| value.to_s == reflection.name.to_s }
    expanded_value(user_options)
  else
    unexpanded_value
  end
end
unexpanded_value() click to toggle source

Returns the unexpanded value for the associated object (i.e. its id property).

@return [String]

# File lib/pragma/decorator/association/bond.rb, line 41
def unexpanded_value
  adapter.primary_key
end

Private Instance Methods

adapter() click to toggle source
# File lib/pragma/decorator/association/bond.rb, line 100
def adapter
  @adapter ||= Adapter.load_adaptor(self)
end
compute_decorator() click to toggle source
# File lib/pragma/decorator/association/bond.rb, line 114
def compute_decorator
  if reflection.options[:decorator].respond_to?(:call)
    reflection.options[:decorator].call(associated_object)
  else
    reflection.options[:decorator]
  end
end
flatten_expand(expand) click to toggle source
# File lib/pragma/decorator/association/bond.rb, line 104
def flatten_expand(expand)
  expand ||= []

  expected_beginning = "#{reflection.name}."

  expand.select { |value| value.start_with?(expected_beginning) }.map do |value|
    value.sub(expected_beginning, '')
  end
end