class JsonApiResource::Associations::Base

Constants

ASSOCIATION_OPTS
RESERVED_KEYWORDS

Attributes

action[RW]
key[RW]
name[RW]
root[RW]

Public Class Methods

new(associated_class, name, opts = {}) click to toggle source
# File lib/json_api_resource/associations/base.rb, line 7
def initialize(associated_class, name, opts = {})
  self.name   = name.to_sym
  self.root   = associated_class
  @opts       = opts.merge( skip_pagination: true )
  
  self.action = @opts.delete :action       do default_action end
  self.key    = @opts.delete :foreign_key  do server_key end

  self.key    = self.key.try :to_sym
  validate_options
end

Public Instance Methods

callable?( root_insatnce ) click to toggle source
# File lib/json_api_resource/associations/base.rb, line 27
def callable?( root_insatnce )
  raise NotImplementedError
end
default_nil() click to toggle source
# File lib/json_api_resource/associations/base.rb, line 31
def default_nil
  raise NotImplementedError
end
klass() click to toggle source

klass has to be lazy initted for circular dependencies

# File lib/json_api_resource/associations/base.rb, line 36
def klass
  @klass ||= begin
    klass = @opts.delete(:class_name).try :constantize
    klass || @opts.delete( :class ) do derived_class end
  end
end
opts() click to toggle source
# File lib/json_api_resource/associations/base.rb, line 47
def opts
  @opts.except *ASSOCIATION_OPTS
end
post_process( value ) click to toggle source
# File lib/json_api_resource/associations/base.rb, line 43
def post_process( value )
  value
end
query( root_insatnce ) click to toggle source
# File lib/json_api_resource/associations/base.rb, line 23
def query( root_insatnce )
  raise NotImplementedError
end
type() click to toggle source
# File lib/json_api_resource/associations/base.rb, line 19
def type
  raise NotImplementedError
end

Protected Instance Methods

default_action() click to toggle source
# File lib/json_api_resource/associations/base.rb, line 57
def default_action
  raise NotImplementedError
end
derived_class() click to toggle source
# File lib/json_api_resource/associations/base.rb, line 61
def derived_class
  module_string = self.root.to_s.split("::")[0 ... -1].join("::")
  class_string  = name.to_s.singularize.camelize
  
  # we don't necessarily want to add :: to classes, in case they have a relative path or something
  class_string = [module_string, class_string].select{|s| s.present? }.join "::"
  class_string.constantize
end
server_key() click to toggle source
# File lib/json_api_resource/associations/base.rb, line 53
def server_key
  raise NotImplementedError
end
validate_options() click to toggle source
# File lib/json_api_resource/associations/base.rb, line 74
def validate_options
  raise_unless action.present?, "Invalid action: #{self.root}.#{name}"
  raise_unless    key.present?, "Invalid foreign_key for #{self.root}.#{name}"

  raise_if RESERVED_KEYWORDS.include?(name), "'#{name}' is a reserved keyword for #{self.root}"
end