class JsonapiSpecHelpers::Payload

Attributes

registry[RW]
keys[RW]
name[RW]
no_keys[RW]
type[RW]

Public Class Methods

by_type(type) click to toggle source
# File lib/jsonapi_spec_helpers/payload.rb, line 17
def self.by_type(type)
  found = nil
  registry.each_pair do |name, payload|
    found = payload if payload.type == type
  end
  raise "Could not find payload for type #{type}" unless found
  found
end
new() click to toggle source
# File lib/jsonapi_spec_helpers/payload.rb, line 33
def initialize
  @keys = {}
  @no_keys = []
end
register(name, &blk) click to toggle source
# File lib/jsonapi_spec_helpers/payload.rb, line 10
def self.register(name, &blk)
  instance = new
  instance.instance_eval(&blk)
  instance.name = name
  registry[name] = instance
end

Public Instance Methods

fork() click to toggle source
# File lib/jsonapi_spec_helpers/payload.rb, line 26
def fork
  instance = self.class.new
  instance.keys = keys.dup
  instance.no_keys = no_keys.dup
  instance
end
key(name, *args, &blk) click to toggle source
# File lib/jsonapi_spec_helpers/payload.rb, line 51
def key(name, *args, &blk)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options[:type] = args.first
  options[:allow_nil] ||= false
  @no_keys.reject! { |k| k == name }
  prc = blk
  prc = ->(record) { record.send(name) } if prc.nil?
  @keys[name] = options.merge(proc: prc)
end
no_key(name) click to toggle source
# File lib/jsonapi_spec_helpers/payload.rb, line 38
def no_key(name)
  @keys.delete(name)
  @no_keys << name
end
timestamps!() click to toggle source
# File lib/jsonapi_spec_helpers/payload.rb, line 61
def timestamps!
  @keys[:created_at] = key(:created_at, String)
  @keys[:updated_at] = key(:updated_at, String)
end