module Caprese::Adapter::JsonApi::JsonPointer
Constants
- POINTERS
Public Instance Methods
new(pointer_type, record, value)
click to toggle source
Iterates over the field of an error and converts it to a pointer in JSON API format
@example
new(:attribute, record, 'name') => '/data/attributes/name'
@example
new(:relationship, record, 'post') => '/data/attributes/name'
@example
new(:relationship_attribute, record, 'post.user.name') => '/data/relationships/post/data/relationships/user/data/attributes/name'
@param [Symbol] pointer_type the type of pointer: :attribute, :relationship, :primary_data @param [Record] the record that owns the errors @param [Object,Array<Object>]
# File lib/caprese/adapter/json_api/json_pointer.rb, line 33 def new(pointer_type, record, value) if pointer_type == :relationship_attribute values = value.to_s.split('.') last_index = values.count - 1 klass_iteratee = record.class values.each_with_index.inject('') do |pointer, (v, i)| pointer + if ref = (klass_iteratee.reflect_on_association(v) || klass_iteratee.reflect_on_association(klass_iteratee.caprese_unalias_field(v))) klass_iteratee = ref.klass if i == last_index format(POINTERS[:relationship_base], v) else format(POINTERS[:relationship_attribute], v) end else format(POINTERS[:attribute], v) end end else format(POINTERS[pointer_type], *Array.wrap(value)) end end