class Blueprinter::Field

@api private

Attributes

blueprint[R]
extractor[R]
method[R]
name[R]
options[R]

Public Class Methods

new(method, name, extractor, blueprint, options = {}) click to toggle source
# File lib/blueprinter/field.rb, line 4
def initialize(method, name, extractor, blueprint, options = {})
  @method = method
  @name = name
  @extractor = extractor
  @blueprint = blueprint
  @options = options
end

Public Instance Methods

extract(object, local_options) click to toggle source
# File lib/blueprinter/field.rb, line 12
def extract(object, local_options)
  extractor.extract(method, object, local_options, options)
end
skip?(field_name, object, local_options) click to toggle source
# File lib/blueprinter/field.rb, line 16
def skip?(field_name, object, local_options)
  return true if if_callable && !if_callable.call(field_name, object, local_options)
  unless_callable && unless_callable.call(field_name, object, local_options)
end

Private Instance Methods

callable_from(condition) click to toggle source
# File lib/blueprinter/field.rb, line 33
def callable_from(condition)
  callable = old_callable_from(condition)

  if callable && callable.arity == 2
    Blueprinter::Deprecation.report("`:#{condition}` conditions now expects 3 arguments instead of 2.")
    ->(_field_name, obj, options) { callable.call(obj, options) }
  else
    callable
  end
end
if_callable() click to toggle source
# File lib/blueprinter/field.rb, line 23
def if_callable
  return @if_callable if defined?(@if_callable)
  @if_callable = callable_from(:if)
end
old_callable_from(condition) click to toggle source
# File lib/blueprinter/field.rb, line 44
def old_callable_from(condition)
  config = Blueprinter.configuration

  # Use field-level callable, or when not defined, try global callable
  tmp = if options.key?(condition)
          options.fetch(condition)
        elsif config.valid_callable?(condition)
          config.public_send(condition)
        end

  return false unless tmp

  case tmp
  when Proc then tmp
  when Symbol then blueprint.method(tmp)
  else
    raise ArgumentError, "#{tmp.class} is passed to :#{condition}"
  end
end
unless_callable() click to toggle source
# File lib/blueprinter/field.rb, line 28
def unless_callable
  return @unless_callable if defined?(@unless_callable)
  @unless_callable = callable_from(:unless)
end