module Grape::Entity::Exposure
Public Class Methods
new(attribute, options)
click to toggle source
# File lib/grape_entity/exposure.rb, line 16 def new(attribute, options) conditions = compile_conditions(attribute, options) base_args = [attribute, options, conditions] passed_proc = options[:proc] using_class = options[:using] format_with = options[:format_with] if using_class build_class_exposure(base_args, using_class, passed_proc) elsif passed_proc build_block_exposure(base_args, passed_proc) elsif format_with build_formatter_exposure(base_args, format_with) elsif options[:nesting] build_nesting_exposure(base_args) else build_delegator_exposure(base_args) end end
Private Class Methods
build_block_exposure(base_args, passed_proc)
click to toggle source
# File lib/grape_entity/exposure.rb, line 95 def build_block_exposure(base_args, passed_proc) BlockExposure.new(*base_args, &passed_proc) end
build_class_exposure(base_args, using_class, passed_proc)
click to toggle source
# File lib/grape_entity/exposure.rb, line 72 def build_class_exposure(base_args, using_class, passed_proc) exposure = if passed_proc build_block_exposure(base_args, passed_proc) else build_delegator_exposure(base_args) end RepresentExposure.new(*base_args, using_class, exposure) end
build_delegator_exposure(base_args)
click to toggle source
# File lib/grape_entity/exposure.rb, line 99 def build_delegator_exposure(base_args) DelegatorExposure.new(*base_args) end
build_formatter_exposure(base_args, format_with)
click to toggle source
# File lib/grape_entity/exposure.rb, line 83 def build_formatter_exposure(base_args, format_with) if format_with.is_a? Symbol FormatterExposure.new(*base_args, format_with) elsif format_with.respond_to?(:call) FormatterBlockExposure.new(*base_args, &format_with) end end
build_nesting_exposure(base_args)
click to toggle source
# File lib/grape_entity/exposure.rb, line 91 def build_nesting_exposure(base_args) NestingExposure.new(*base_args) end
compile_conditions(attribute, options)
click to toggle source
# File lib/grape_entity/exposure.rb, line 39 def compile_conditions(attribute, options) if_conditions = [ options[:if_extras], options[:if] ].compact.flatten.map { |cond| Condition.new_if(cond) } unless_conditions = [ options[:unless_extras], options[:unless] ].compact.flatten.map { |cond| Condition.new_unless(cond) } unless_conditions << expose_nil_condition(attribute, options) if options[:expose_nil] == false if_conditions + unless_conditions end
expose_nil_condition(attribute, options)
click to toggle source
# File lib/grape_entity/exposure.rb, line 55 def expose_nil_condition(attribute, options) Condition.new_unless( proc do |object, _options| if options[:proc].nil? delegator = Delegator.new(object) if is_a?(Grape::Entity) && delegator.accepts_options? delegator.delegate(attribute, **self.class.delegation_opts).nil? else delegator.delegate(attribute).nil? end else exec_with_object(options, &options[:proc]).nil? end end ) end