lib/gift_wrap/presenter.rb in gift_wrap-0.2.0 vs lib/gift_wrap/presenter.rb in gift_wrap-1.0.0
- old
+ new
@@ -110,14 +110,18 @@
def wrap_association(association, with: , as: association, **options)
wrapped_association_defaults[as] = with
define_method(as) do
presenter_class = wrapped_association_presenter(as)
associated = @wrapped_object.send(association)
- if associated.respond_to?(:each)
- associated.map { |assoc| presenter_class.new(assoc, **options) }
- else
- presenter_class.new(associated, **options)
- end
+ memoized_within = "@#{as}"
+ instance_variable_get(memoized_within) ||
+ instance_variable_set(memoized_within,
+ if associated.respond_to?(:each)
+ associated.map { |assoc| presenter_class.new(assoc, **options) }
+ else
+ presenter_class.new(associated, **options)
+ end
+ )
end
end
end