class Graphiti::Util::SerializerAttribute

Public Class Methods

new(name, attr, resource, serializer, extra) click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 4
def initialize(name, attr, resource, serializer, extra)
  @name = name
  @attr = attr
  @resource = resource
  @serializer = serializer
  @extra = extra
end

Public Instance Methods

apply() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 12
def apply
  return unless readable?

  remove_guard if previously_guarded?

  if @name == :id
    @serializer.id(&proc)
  elsif @attr[:proc] ||
      !previously_applied? ||
      previously_applied_via_resource?
    @serializer.send(_method, @name, serializer_options, &proc)
  else # Previously applied via explicit serializer, so wrap it
    inner = @serializer.attribute_blocks.delete(@name)
    wrapped = wrap_proc(inner)
    @serializer.send(_method, @name, serializer_options, &wrapped)
  end

  existing = @serializer.send(applied_method)
  @serializer.send(:"#{applied_method}=", [@name] | existing)

  @serializer.meta do
    if !!@resource.try(:cursor_paginatable?) && !Graphiti.context[:graphql]
      {cursor: cursor}
    end
  end
end

Private Instance Methods

_method() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 65
def _method
  extra? ? :extra_attribute : :attribute
end
applied_method() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 57
def applied_method
  if extra?
    :extra_attributes_applied_via_resource
  else
    :attributes_applied_via_resource
  end
end
default_proc() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 122
def default_proc
  name_ref = @name
  typecast_ref = typecast(Graphiti::Types[@attr[:type]][:read])
  ->(_) {
    val = @object.send(name_ref)
    if Graphiti.config.typecast_reads
      typecast_ref.call(val)
    else
      val
    end
  }
end
extra?() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 69
def extra?
  !!@extra
end
guard() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 77
def guard
  method_name = @attr[:readable]
  instance = @resource.new
  attribute = @name.to_s
  resource_class = @resource

  -> {
    method = instance.method(method_name)
    result = if method.arity.zero?
      instance.instance_eval(&method_name)
    elsif method.arity == 1
      instance.instance_exec(@object, &method)
    else
      instance.instance_exec(@object, attribute, &method)
    end
    if Graphiti.context[:graphql] && !result
      raise ::Graphiti::Errors::UnreadableAttribute.new(resource_class, attribute)
    end
    result
  }
end
guard?() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 99
def guard?
  @attr[:readable] != true
end
previously_applied?() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 41
def previously_applied?
  @serializer.attribute_blocks[@name].present?
end
previously_applied_via_resource?() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 45
def previously_applied_via_resource?
  @serializer.send(applied_method).include?(@name)
end
previously_guarded?() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 49
def previously_guarded?
  @serializer.field_condition_blocks[@name]
end
proc() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 147
def proc
  @attr[:proc] ? wrap_proc(@attr[:proc]) : default_proc
end
readable?() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 73
def readable?
  !!@attr[:readable]
end
remove_guard() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 53
def remove_guard
  @serializer.field_condition_blocks.delete(@name)
end
serializer_options() click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 103
def serializer_options
  {}.tap do |opts|
    opts[:if] = guard if guard?
  end
end
typecast(type) click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 109
def typecast(type)
  resource_ref = @resource
  name_ref = @name
  type_ref = type
  ->(value) {
    begin
      type_ref[value] unless value.nil?
    rescue => e
      raise Errors::TypecastFailed.new(resource_ref, name_ref, value, e, type_ref)
    end
  }
end
wrap_proc(inner) click to toggle source
# File lib/graphiti/util/serializer_attributes.rb, line 135
def wrap_proc(inner)
  typecast_ref = typecast(Graphiti::Types[@attr[:type]][:read])
  ->(serializer_instance = nil) {
    val = serializer_instance.instance_eval(&inner)
    if Graphiti.config.typecast_reads
      typecast_ref.call(val)
    else
      val
    end
  }
end