class Grape::Entity::Exposure::Base

Attributes

attribute[R]
conditions[R]
documentation[R]
for_merge[R]
is_safe[R]
options[R]
override[R]

Public Class Methods

new(attribute, options, conditions) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 12
def initialize(attribute, options, conditions)
  @attribute = attribute.try(:to_sym)
  @options = options
  key = options[:as] || attribute
  @key = key.respond_to?(:to_sym) ? key.to_sym : key
  @is_safe = options[:safe]
  @for_merge = options[:merge]
  @attr_path_proc = options[:attr_path]
  @documentation = options[:documentation]
  @override = options[:override]
  @conditions = conditions
end
new(attribute, options, conditions, *args, &block) click to toggle source
Calls superclass method
# File lib/grape_entity/exposure/base.rb, line 8
def self.new(attribute, options, conditions, *args, &block)
  super(attribute, options, conditions).tap { |e| e.setup(*args, &block) }
end

Public Instance Methods

==(other) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 33
def ==(other)
  self.class == other.class &&
    @attribute == other.attribute &&
    @options == other.options &&
    @conditions == other.conditions
end
attr_path(entity, options) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 100
def attr_path(entity, options)
  if @attr_path_proc
    entity.exec_with_object(options, &@attr_path_proc)
  else
    @key
  end
end
conditional?() click to toggle source
# File lib/grape_entity/exposure/base.rb, line 88
def conditional?
  !@conditions.empty?
end
conditions_met?(entity, options) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 92
def conditions_met?(entity, options)
  @conditions.all? { |condition| condition.met? entity, options }
end
deep_complex_nesting?(entity) click to toggle source

if we have any nesting exposures with the same name.

# File lib/grape_entity/exposure/base.rb, line 47
def deep_complex_nesting?(entity) # rubocop:disable Lint/UnusedMethodArgument
  false
end
dup(&block) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 25
def dup(&block)
  self.class.new(*dup_args, &block)
end
dup_args() click to toggle source
# File lib/grape_entity/exposure/base.rb, line 29
def dup_args
  [@attribute, @options, @conditions.map(&:dup)]
end
key(entity = nil) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 108
def key(entity = nil)
  @key.respond_to?(:call) ? entity.exec_with_object(@options, &@key) : @key
end
nesting?() click to toggle source
# File lib/grape_entity/exposure/base.rb, line 42
def nesting?
  false
end
override?() click to toggle source
# File lib/grape_entity/exposure/base.rb, line 119
def override?
  @override
end
serializable_value(entity, options) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 64
def serializable_value(entity, options)
  partial_output = valid_value(entity, options)

  if partial_output.respond_to?(:serializable_hash)
    partial_output.serializable_hash
  elsif partial_output.is_a?(Array) && partial_output.all? { |o| o.respond_to?(:serializable_hash) }
    partial_output.map(&:serializable_hash)
  elsif partial_output.is_a?(Hash)
    partial_output.each do |key, value|
      partial_output[key] = value.serializable_hash if value.respond_to?(:serializable_hash)
    end
  else
    partial_output
  end
end
setup() click to toggle source
# File lib/grape_entity/exposure/base.rb, line 40
def setup; end
should_expose?(entity, options) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 96
def should_expose?(entity, options)
  should_return_key?(options) && conditions_met?(entity, options)
end
should_return_key?(options) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 84
def should_return_key?(options)
  options.should_return_key?(@key)
end
valid?(entity) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 51
def valid?(entity)
  is_delegatable = entity.delegator.delegatable?(@attribute) || entity.respond_to?(@attribute, true)
  if @is_safe
    is_delegatable
  else
    is_delegatable || raise(NoMethodError, "#{entity.class.name} missing attribute `#{@attribute}' on #{entity.object}")
  end
end
valid_value(entity, options) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 80
def valid_value(entity, options)
  value(entity, options) if valid?(entity)
end
value(_entity, _options) click to toggle source
# File lib/grape_entity/exposure/base.rb, line 60
def value(_entity, _options)
  raise NotImplementedError
end
with_attr_path(entity, options) { || ... } click to toggle source
# File lib/grape_entity/exposure/base.rb, line 112
def with_attr_path(entity, options)
  path_part = attr_path(entity, options)
  options.with_attr_path(path_part) do
    yield
  end
end