Class: Pragma::Decorator::Association::Bond Private
- Inherits:
-
Object
- Object
- Pragma::Decorator::Association::Bond
- 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
- #decorator ⇒ Object readonly private
-
#reflection ⇒ Reflection
readonly
The association reflection.
Instance Method Summary collapse
-
#associated_object ⇒ Object
private
Returns the associated object.
-
#expanded_value(user_options) ⇒ Hash
private
Returns the expanded value for the associated object.
-
#initialize(reflection:, decorator:) ⇒ Bond
constructor
private
Initializes the bond.
-
#model ⇒ Object
private
Returns the model associated to this bond.
-
#render(user_options) ⇒ Hash|Pragma::Decorator::Base
private
Renders the unexpanded or expanded associations, depending on the
expand
user option passed to the decorator. -
#unexpanded_value ⇒ String
private
Returns the unexpanded value for the associated object (i.e. its
id
property).
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.
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
#decorator ⇒ Object (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 |
#reflection ⇒ Reflection (readonly)
Returns the association reflection.
15 16 17 |
# File 'lib/pragma/decorator/association/bond.rb', line 15 def reflection @reflection end |
Instance Method Details
#associated_object ⇒ Object
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.
29 30 31 32 33 34 35 36 |
# File 'lib/pragma/decorator/association/bond.rb', line 29 def associated_object case reflection.[: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.
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 () full_object = adapter.full_object return unless full_object = { user_options: .merge( expand: ([:expand]) ) } decorator_klass = compute_decorator if decorator_klass decorator_klass.new(full_object).to_hash() else full_object.as_json() end end |
#model ⇒ Object
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.
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.
83 84 85 86 87 88 89 |
# File 'lib/pragma/decorator/association/bond.rb', line 83 def render() if [:expand]&.any? { |value| value.to_s == reflection.property.to_s } () else end end |
#unexpanded_value ⇒ String
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).
41 42 43 |
# File 'lib/pragma/decorator/association/bond.rb', line 41 def adapter.primary_key end |