Class: Pragma::Decorator::Association::Bond Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pragma/decorator/association/bond.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reflection:, decorator:) ⇒ Bond

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the bond.

Parameters:



21
22
23
24
# File 'lib/pragma/decorator/association/bond.rb', line 21

def initialize(reflection:, decorator:)
  @reflection = reflection
  @decorator = decorator
end

Instance Attribute Details

#decoratorObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
# File 'lib/pragma/decorator/association/bond.rb', line 15

attr_reader :reflection, :decorator

#reflectionReflection (readonly)

Returns the association reflection.

Returns:



15
16
17
# File 'lib/pragma/decorator/association/bond.rb', line 15

def reflection
  @reflection
end

Instance Method Details

#associated_objectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the associated object.

Returns:

  • (Object)


29
30
31
32
33
34
35
36
# File 'lib/pragma/decorator/association/bond.rb', line 29

def associated_object
  case reflection.options[:exec_context]
  when :decorated
    model.public_send(reflection.property)
  when :decorator
    decorator.public_send(reflection.property)
  end
end

#expanded_value(user_options) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Parameters:

  • user_options (Array)

Returns:

  • (Hash)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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

#modelObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the model associated to this bond.

Returns:

  • (Object)


94
95
96
# File 'lib/pragma/decorator/association/bond.rb', line 94

def model
  decorator.decorated
end

#render(user_options) ⇒ Hash|Pragma::Decorator::Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • user_options (Array)

Returns:



83
84
85
86
87
88
89
# File 'lib/pragma/decorator/association/bond.rb', line 83

def render(user_options)
  if user_options[:expand]&.any? { |value| value.to_s == reflection.property.to_s }
    expanded_value(user_options)
  else
    unexpanded_value
  end
end

#unexpanded_valueString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (String)


41
42
43
# File 'lib/pragma/decorator/association/bond.rb', line 41

def unexpanded_value
  adapter.primary_key
end