1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
# frozen_string_literal: true
module Grape
class Entity
module Exposure
class FormatterExposure < Base
attr_reader :format_with
def setup(format_with)
@format_with = format_with
end
def dup_args
[*super, format_with]
end
def ==(other)
super && @format_with == other.format_with
end
def value(entity, _options)
formatters = entity.class.formatters
if formatters[@format_with]
entity.exec_with_attribute(attribute, &formatters[@format_with])
else
entity.send(@format_with, entity.delegate_attribute(attribute))
end
end
end
end
end
end
|