New DSL. (#13)

This commit is contained in:
Lucas Hosseini 2017-07-14 15:07:02 +02:00 committed by GitHub
parent 65e6a1adf4
commit 4d1ed42095
15 changed files with 293 additions and 308 deletions

View File

@ -25,6 +25,7 @@ module JSONAPI
@document = payload @document = payload
@data = payload['data'] @data = payload['data']
deserialize! deserialize!
freeze freeze
end end
@ -45,23 +46,19 @@ module JSONAPI
end end
def deserialize_has_one def deserialize_has_one
block = self.class.has_one_block
return {} unless block
id = @data && @data['id'] id = @data && @data['id']
type = @data && @data['type'] type = @data && @data['type']
if self.class.has_one_block block.call(@document, id, type)
self.class.has_one_block.call(@document, id, type)
else
{ id: id, type: type }
end
end end
def deserialize_has_many def deserialize_has_many
block = self.class.has_many_block
return {} unless block
ids = @data.map { |ri| ri['id'] } ids = @data.map { |ri| ri['id'] }
types = @data.map { |ri| ri['type'] } types = @data.map { |ri| ri['type'] }
if self.class.has_many_block block.call(@document, ids, types)
self.class.has_many_block.call(@document, ids, types)
else
{ ids: ids, types: types }
end
end end
end end
end end

View File

@ -2,12 +2,19 @@ module JSONAPI
module Deserializable module Deserializable
class Relationship class Relationship
module DSL module DSL
DEFAULT_HAS_ONE_REL_BLOCK = proc do |_val, id, type|
{ type: type, id: id }
end
DEFAULT_HAS_MANY_REL_BLOCK = proc do |_val, ids, types|
{ types: types, ids: ids }
end
def has_one(&block) def has_one(&block)
self.has_one_block = block self.has_one_block = block || DEFAULT_HAS_ONE_REL_BLOCK
end end
def has_many(&block) def has_many(&block)
self.has_many_block = block self.has_many_block = block || DEFAULT_HAS_MANY_REL_BLOCK
end end
end end
end end

View File

@ -1,4 +1,3 @@
require 'jsonapi/deserializable/resource/configuration'
require 'jsonapi/deserializable/resource/dsl' require 'jsonapi/deserializable/resource/dsl'
require 'jsonapi/parser/resource' require 'jsonapi/parser/resource'
@ -10,32 +9,24 @@ module JSONAPI
class << self class << self
attr_accessor :type_block, :id_block, :attr_blocks, attr_accessor :type_block, :id_block, :attr_blocks,
:has_one_rel_blocks, :has_many_rel_blocks, :has_one_rel_blocks, :has_many_rel_blocks,
:configuration :default_attr_block, :default_has_one_rel_block,
:default_has_many_rel_block
end end
@class_cache = {}
self.configuration = Configuration.new
self.attr_blocks = {} self.attr_blocks = {}
self.has_one_rel_blocks = {} self.has_one_rel_blocks = {}
self.has_many_rel_blocks = {} self.has_many_rel_blocks = {}
def self.inherited(klass) def self.inherited(klass)
super super
klass.configuration = configuration.dup
klass.type_block = type_block klass.type_block = type_block
klass.id_block = id_block klass.id_block = id_block
klass.attr_blocks = attr_blocks.dup klass.attr_blocks = attr_blocks.dup
klass.has_one_rel_blocks = has_one_rel_blocks.dup klass.has_one_rel_blocks = has_one_rel_blocks.dup
klass.has_many_rel_blocks = has_many_rel_blocks.dup klass.has_many_rel_blocks = has_many_rel_blocks.dup
end klass.default_attr_block = default_attr_block
klass.default_has_one_rel_block = default_has_one_rel_block
def self.configure klass.default_has_many_rel_block = default_has_many_rel_block
yield(configuration)
end
def self.[](name)
@class_cache[name] ||= Class.new(self)
end end
def self.call(payload) def self.call(payload)
@ -63,10 +54,6 @@ module JSONAPI
private private
def configuration
self.class.configuration
end
def register_mappings(keys, path) def register_mappings(keys, path)
keys.each do |k| keys.each do |k|
@reverse_mapping[k] = path @reverse_mapping[k] = path
@ -81,16 +68,19 @@ module JSONAPI
end end
def deserialize_type def deserialize_type
block = self.class.type_block || configuration.default_type block = self.class.type_block
return {} unless block
hash = block.call(@type) hash = block.call(@type)
register_mappings(hash.keys, '/data/type') register_mappings(hash.keys, '/data/type')
hash hash
end end
def deserialize_id def deserialize_id
return {} unless @id block = self.class.id_block
block = self.class.id_block || configuration.default_id return {} unless @id && block
hash = block.call(@id)
hash = block.call(@id)
register_mappings(hash.keys, '/data/id') register_mappings(hash.keys, '/data/id')
hash hash
end end
@ -102,11 +92,10 @@ module JSONAPI
end end
def deserialize_attr(key, val) def deserialize_attr(key, val)
hash = if self.class.attr_blocks.key?(key) block = self.class.attr_blocks[key] || self.class.default_attr_block
self.class.attr_blocks[key].call(val) return {} unless block
else
configuration.default_attribute.call(key, val) hash = block.call(val, key)
end
register_mappings(hash.keys, "/data/attributes/#{key}") register_mappings(hash.keys, "/data/attributes/#{key}")
hash hash
end end
@ -118,36 +107,38 @@ module JSONAPI
end end
def deserialize_rel(key, val) def deserialize_rel(key, val)
hash = if val['data'].is_a?(Array) if val['data'].is_a?(Array)
deserialize_has_many_rel(key, val) deserialize_has_many_rel(key, val)
else else
deserialize_has_one_rel(key, val) deserialize_has_one_rel(key, val)
end end
register_mappings(hash.keys, "/data/relationships/#{key}")
hash
end end
# rubocop: disable Metrics/AbcSize # rubocop: disable Metrics/AbcSize
def deserialize_has_one_rel(key, val) def deserialize_has_one_rel(key, val)
block = self.class.has_one_rel_blocks[key] ||
self.class.default_has_one_rel_block
return {} unless block
id = val['data'] && val['data']['id'] id = val['data'] && val['data']['id']
type = val['data'] && val['data']['type'] type = val['data'] && val['data']['type']
if self.class.has_one_rel_blocks.key?(key) hash = block.call(val, id, type, key)
self.class.has_one_rel_blocks[key].call(val, id, type) register_mappings(hash.keys, "/data/relationships/#{key}")
else hash
configuration.default_has_one.call(key, val, id, type)
end
end end
# rubocop: enable Metrics/AbcSize # rubocop: enable Metrics/AbcSize
# rubocop: disable Metrics/AbcSize # rubocop: disable Metrics/AbcSize
def deserialize_has_many_rel(key, val) def deserialize_has_many_rel(key, val)
block = self.class.has_many_rel_blocks[key] ||
self.class.default_has_many_rel_block
return {} unless block
ids = val['data'].map { |ri| ri['id'] } ids = val['data'].map { |ri| ri['id'] }
types = val['data'].map { |ri| ri['type'] } types = val['data'].map { |ri| ri['type'] }
if self.class.has_many_rel_blocks.key?(key) hash = block.call(val, ids, types, key)
self.class.has_many_rel_blocks[key].call(val, ids, types) register_mappings(hash.keys, "/data/relationships/#{key}")
else hash
configuration.default_has_many.call(key, val, ids, types)
end
end end
# rubocop: enable Metrics/AbcSize # rubocop: enable Metrics/AbcSize
end end

View File

@ -1,28 +0,0 @@
module JSONAPI
module Deserializable
class Resource
class Configuration
DEFAULT_TYPE_BLOCK = proc { |t| { type: t } }
DEFAULT_ID_BLOCK = proc { |i| { id: i } }
DEFAULT_ATTR_BLOCK = proc { |k, v| { k.to_sym => v } }
DEFAULT_HAS_ONE_BLOCK = proc do |k, _, i, t|
{ "#{k}_id".to_sym => i, "#{k}_type".to_sym => t }
end
DEFAULT_HAS_MANY_BLOCK = proc do |k, _, i, t|
{ "#{k}_ids".to_sym => i, "#{k}_types".to_sym => t }
end
attr_accessor :default_type, :default_id, :default_attribute,
:default_has_one, :default_has_many
def initialize
self.default_type = DEFAULT_TYPE_BLOCK
self.default_id = DEFAULT_ID_BLOCK
self.default_attribute = DEFAULT_ATTR_BLOCK
self.default_has_one = DEFAULT_HAS_ONE_BLOCK
self.default_has_many = DEFAULT_HAS_MANY_BLOCK
end
end
end
end
end

View File

@ -2,24 +2,50 @@ module JSONAPI
module Deserializable module Deserializable
class Resource class Resource
module DSL module DSL
DEFAULT_TYPE_BLOCK = proc { |t| { type: t } }
DEFAULT_ID_BLOCK = proc { |i| { id: i } }
DEFAULT_ATTR_BLOCK = proc { |v, k| { k.to_sym => v } }
DEFAULT_HAS_ONE_BLOCK = proc do |_, i, t, k|
{ "#{k}_id".to_sym => i, "#{k}_type".to_sym => t }
end
DEFAULT_HAS_MANY_BLOCK = proc do |_, i, t, k|
{ "#{k}_ids".to_sym => i, "#{k}_types".to_sym => t }
end
def type(&block) def type(&block)
self.type_block = block self.type_block = block || DEFAULT_TYPE_BLOCK
end end
def id(&block) def id(&block)
self.id_block = block self.id_block = block || DEFAULT_ID_BLOCK
end end
def attribute(key, &block) def attribute(key, &block)
attr_blocks[key.to_s] = block attr_blocks[key.to_s] = block || DEFAULT_ATTR_BLOCK
end end
def has_one(key, &block) def attributes(*keys, &block)
has_one_rel_blocks[key.to_s] = block if keys.empty?
self.default_attr_block = block || DEFAULT_ATTR_BLOCK
else
keys.each { |k| attribute(k, &block) }
end
end end
def has_many(key, &block) def has_one(key = nil, &block)
has_many_rel_blocks[key.to_s] = block if key
has_one_rel_blocks[key.to_s] = block || DEFAULT_HAS_ONE_BLOCK
else
self.default_has_one_rel_block = block || DEFAULT_HAS_ONE_BLOCK
end
end
def has_many(key = nil, &block)
if key
has_many_rel_blocks[key.to_s] = block || DEFAULT_HAS_MANY_BLOCK
else
self.default_has_many_rel_block = block || DEFAULT_HAS_MANY_BLOCK
end
end end
end end
end end

View File

@ -28,7 +28,9 @@ describe JSONAPI::Deserializable::Relationship, '.has_many' do
end end
it 'defaults to creating ids and types fields' do it 'defaults to creating ids and types fields' do
klass = Class.new(JSONAPI::Deserializable::Relationship) klass = Class.new(JSONAPI::Deserializable::Relationship) do
has_many
end
actual = klass.call(payload) actual = klass.call(payload)
expected = { ids: %w(bar baz), types: %w(foo foo) } expected = { ids: %w(bar baz), types: %w(foo foo) }
@ -56,10 +58,10 @@ describe JSONAPI::Deserializable::Relationship, '.has_many' do
end end
context 'relationship is not to-many' do context 'relationship is not to-many' do
it 'falls back to default to-one deserialization scheme' do it 'does not deserialize relationship' do
payload = { 'data' => nil } payload = { 'data' => nil }
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { id: nil, type: nil } expected = {}
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end

View File

@ -22,7 +22,9 @@ describe JSONAPI::Deserializable::Relationship, '.has_one' do
end end
it 'defaults to creating id and type fields' do it 'defaults to creating id and type fields' do
klass = Class.new(JSONAPI::Deserializable::Relationship) klass = Class.new(JSONAPI::Deserializable::Relationship) do
has_one
end
actual = klass.call(payload) actual = klass.call(payload)
expected = { id: 'bar', type: 'foo' } expected = { id: 'bar', type: 'foo' }
@ -50,10 +52,10 @@ describe JSONAPI::Deserializable::Relationship, '.has_one' do
end end
context 'relationship is not to-one' do context 'relationship is not to-one' do
it 'falls back to default has_many deserialization scheme ' do it 'does not deserialize relationship' do
payload = { 'data' => [] } payload = { 'data' => [] }
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { ids: [], types: [] } expected = {}
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end

View File

@ -1,55 +1,67 @@
require 'spec_helper' require 'spec_helper'
describe JSONAPI::Deserializable::Resource, '.attribute' do describe JSONAPI::Deserializable::Resource, '.attribute' do
it 'creates corresponding field if attribute is present' do context 'when attribute is present' do
payload = { context 'when a block is specified' do
'data' => { it 'creates corresponding field' do
'type' => 'foo', payload = {
'attributes' => { 'foo' => 'bar' } 'data' => {
} 'type' => 'foo',
} 'attributes' => { 'foo' => 'bar' }
klass = Class.new(JSONAPI::Deserializable::Resource) do }
attribute(:foo) { |foo| Hash[foo: foo] } }
end klass = Class.new(JSONAPI::Deserializable::Resource) do
actual = klass.call(payload) attribute(:foo) { |foo| Hash[foo: foo] }
expected = { foo: 'bar', type: 'foo' } end
actual = klass.call(payload)
expected = { foo: 'bar' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end
end
context 'when no block is specified' do
it 'defaults to creating a field with same name' do
payload = {
'data' => {
'type' => 'foo',
'attributes' => { 'foo' => 'bar' }
}
}
klass = Class.new(JSONAPI::Deserializable::Resource) do
attribute(:foo)
end
actual = klass.call(payload)
expected = { foo: 'bar' }
expect(actual).to eq(expected)
end
end
end end
it 'does not create corresponding field if attribute is absent' do context 'when attribute is absent' do
payload = { 'data' => { 'type' => 'foo', 'attributes' => {} } } it 'does not create corresponding field if attribute is absent' do
klass = Class.new(JSONAPI::Deserializable::Resource) do payload = { 'data' => { 'type' => 'foo', 'attributes' => {} } }
attribute(:foo) { |foo| Hash[foo: foo] } klass = Class.new(JSONAPI::Deserializable::Resource) do
end attribute(:foo) { |foo| Hash[foo: foo] }
actual = klass.call(payload) end
expected = { type: 'foo' } actual = klass.call(payload)
expected = {}
expect(actual).to eq(expected) expect(actual).to eq(expected)
end
end end
it 'does not create corresponding field if no attribute specified' do context 'when attributes member is absent' do
payload = { 'data' => { 'type' => 'foo' } } it 'does not create corresponding field if no attribute specified' do
klass = Class.new(JSONAPI::Deserializable::Resource) do payload = { 'data' => { 'type' => 'foo' } }
attribute(:foo) { |foo| Hash[foo: foo] } klass = Class.new(JSONAPI::Deserializable::Resource) do
attribute(:foo) { |foo| Hash[foo: foo] }
end
actual = klass.call(payload)
expected = {}
expect(actual).to eq(expected)
end end
actual = klass.call(payload)
expected = { type: 'foo' }
expect(actual).to eq(expected)
end
it 'defaults to creating a field with same name' do
payload = {
'data' => {
'type' => 'foo',
'attributes' => { 'foo' => 'bar' }
}
}
klass = JSONAPI::Deserializable::Resource
actual = klass.call(payload)
expected = { foo: 'bar', type: 'foo' }
expect(actual).to eq(expected)
end end
end end

View File

@ -0,0 +1,83 @@
require 'spec_helper'
describe JSONAPI::Deserializable::Resource, '.attributes' do
context 'when no block is specified' do
context 'when no keys are specified' do
it 'defaults to creating fields with same name' do
payload = {
'data' => {
'type' => 'foo',
'attributes' => { 'foo' => 'bar', 'baz' => 'foo' }
}
}
klass = Class.new(JSONAPI::Deserializable::Resource) do
attributes
end
actual = klass.call(payload)
expected = { foo: 'bar', baz: 'foo' }
expect(actual).to eq(expected)
end
end
context 'when keys are specified' do
it 'creates fields with same name for whitelisted attributes' do
payload = {
'data' => {
'type' => 'foo',
'attributes' => { 'foo' => 'bar', 'baz' => 'foo', 'bar' => 'foo' }
}
}
klass = Class.new(JSONAPI::Deserializable::Resource) do
attributes :foo, :baz
end
actual = klass.call(payload)
expected = { foo: 'bar', baz: 'foo' }
expect(actual).to eq(expected)
end
end
end
context 'when a block is specified' do
context 'when no keys are specified' do
it 'defaults to creating fields with same name' do
payload = {
'data' => {
'type' => 'foo',
'attributes' => { 'foo' => 'bar', 'baz' => 'foo' }
}
}
klass = Class.new(JSONAPI::Deserializable::Resource) do
attributes do |val, key|
Hash["#{key}_attr".to_sym => val]
end
end
actual = klass.call(payload)
expected = { foo_attr: 'bar', baz_attr: 'foo' }
expect(actual).to eq(expected)
end
end
context 'when keys are specified' do
it 'creates customized fields for whitelisted attributes' do
payload = {
'data' => {
'type' => 'foo',
'attributes' => { 'foo' => 'bar', 'baz' => 'foo', 'bar' => 'foo' }
}
}
klass = Class.new(JSONAPI::Deserializable::Resource) do
attributes(:foo, :baz) do |val, key|
Hash["#{key}_attr".to_sym => val]
end
end
actual = klass.call(payload)
expected = { foo_attr: 'bar', baz_attr: 'foo' }
expect(actual).to eq(expected)
end
end
end
end

View File

@ -29,16 +29,17 @@ describe JSONAPI::Deserializable::Resource, '.has_many' do
it 'creates corresponding fields' do it 'creates corresponding fields' do
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { foo_ids: %w(bar baz), foo_types: %w(foo foo), expected = { foo_ids: %w(bar baz), foo_types: %w(foo foo),
foo_rel: payload['data']['relationships']['foo'], foo_rel: payload['data']['relationships']['foo'] }
type: 'foo' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
it 'defaults to creating a #{name}_ids and #{name}_types fields' do it 'defaults to creating a #{name}_ids and #{name}_types fields' do
klass = JSONAPI::Deserializable::Resource klass = Class.new(JSONAPI::Deserializable::Resource) do
has_many
end
actual = klass.call(payload) actual = klass.call(payload)
expected = { foo_ids: %w(bar baz), foo_types: %w(foo foo), type: 'foo' } expected = { foo_ids: %w(bar baz), foo_types: %w(foo foo) }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -58,8 +59,7 @@ describe JSONAPI::Deserializable::Resource, '.has_many' do
} }
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { foo_ids: [], foo_types: [], expected = { foo_ids: [], foo_types: [],
foo_rel: payload['data']['relationships']['foo'], foo_rel: payload['data']['relationships']['foo'] }
type: 'foo' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -74,7 +74,7 @@ describe JSONAPI::Deserializable::Resource, '.has_many' do
} }
} }
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { type: 'foo' } expected = {}
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -88,7 +88,7 @@ describe JSONAPI::Deserializable::Resource, '.has_many' do
} }
} }
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { type: 'foo' } expected = {}
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end

View File

@ -26,16 +26,17 @@ describe JSONAPI::Deserializable::Resource, '.has_one' do
it 'creates corresponding fields' do it 'creates corresponding fields' do
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { foo_id: 'bar', foo_type: 'foo', expected = { foo_id: 'bar', foo_type: 'foo',
foo_rel: payload['data']['relationships']['foo'], foo_rel: payload['data']['relationships']['foo'] }
type: 'foo' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
it 'defaults to creating #{name}_id and #{name}_type' do it 'defaults to creating #{name}_id and #{name}_type' do
klass = JSONAPI::Deserializable::Resource klass = Class.new(JSONAPI::Deserializable::Resource) do
has_one
end
actual = klass.call(payload) actual = klass.call(payload)
expected = { foo_id: 'bar', foo_type: 'foo', type: 'foo' } expected = { foo_id: 'bar', foo_type: 'foo' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -56,8 +57,7 @@ describe JSONAPI::Deserializable::Resource, '.has_one' do
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { foo_id: nil, foo_type: nil, expected = { foo_id: nil, foo_type: nil,
foo_rel: payload['data']['relationships']['foo'], foo_rel: payload['data']['relationships']['foo'] }
type: 'foo' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -72,7 +72,7 @@ describe JSONAPI::Deserializable::Resource, '.has_one' do
} }
} }
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { type: 'foo' } expected = {}
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -86,7 +86,7 @@ describe JSONAPI::Deserializable::Resource, '.has_one' do
} }
} }
actual = deserializable_foo.call(payload) actual = deserializable_foo.call(payload)
expected = { type: 'foo' } expected = {}
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end

View File

@ -7,7 +7,7 @@ describe JSONAPI::Deserializable::Resource, '.id' do
id { |i| Hash[id: i] } id { |i| Hash[id: i] }
end end
actual = klass.call(payload) actual = klass.call(payload)
expected = { id: 'bar', type: 'foo' } expected = { id: 'bar' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -18,16 +18,18 @@ describe JSONAPI::Deserializable::Resource, '.id' do
id { |i| Hash[id: i] } id { |i| Hash[id: i] }
end end
actual = klass.call(payload) actual = klass.call(payload)
expected = { type: 'foo' } expected = {}
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
it 'defaults to creating an id field' do it 'defaults to creating an id field' do
payload = { 'data' => { 'type' => 'foo', 'id' => 'bar' } } payload = { 'data' => { 'type' => 'foo', 'id' => 'bar' } }
klass = JSONAPI::Deserializable::Resource klass = Class.new(JSONAPI::Deserializable::Resource) do
id
end
actual = klass.call(payload) actual = klass.call(payload)
expected = { id: 'bar', type: 'foo' } expected = { id: 'bar' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end

View File

@ -14,7 +14,9 @@ describe JSONAPI::Deserializable::Resource, '.type' do
it 'defaults to creating a type field' do it 'defaults to creating a type field' do
payload = { 'data' => { 'type' => 'foo' } } payload = { 'data' => { 'type' => 'foo' } }
klass = JSONAPI::Deserializable::Resource klass = Class.new(JSONAPI::Deserializable::Resource) do
type
end
actual = klass.call(payload) actual = klass.call(payload)
expected = { type: 'foo' } expected = { type: 'foo' }

View File

@ -1,108 +0,0 @@
require 'spec_helper'
describe JSONAPI::Deserializable::Resource, '.configure' do
it 'overrides global default attribute deserialization scheme' do
payload = {
'data' => {
'type' => 'foo',
'attributes' => {
'foo' => 'bar',
'baz' => 'foo'
}
}
}
begin
JSONAPI::Deserializable::Resource.configure do |cfg|
cfg.default_attribute = proc do |key, value|
{ "custom_#{key}".to_sym => value }
end
end
klass = JSONAPI::Deserializable::Resource
actual = klass.call(payload)
expected = { custom_foo: 'bar', custom_baz: 'foo', type: 'foo' }
expect(actual).to eq(expected)
ensure
JSONAPI::Deserializable::Resource.configuration =
JSONAPI::Deserializable::Resource::Configuration.new
end
end
it 'overrides default attribute deserialization scheme' do
payload = {
'data' => {
'type' => 'foo',
'attributes' => {
'foo' => 'bar',
'baz' => 'foo'
}
}
}
JSONAPI::Deserializable::Resource[:c1].configure do |cfg|
cfg.default_attribute = proc do |key, value|
{ "custom_#{key}".to_sym => value }
end
end
klass = JSONAPI::Deserializable::Resource[:c1]
actual = klass.call(payload)
expected = { custom_foo: 'bar', custom_baz: 'foo', type: 'foo' }
expect(actual).to eq(expected)
end
it 'overrides the default has_many relationship deserialization scheme' do
payload = {
'data' => {
'type' => 'foo',
'relationships' => {
'foo' => {
'data' => [{ 'type' => 'bar', 'id' => 'baz' },
{ 'type' => 'foo', 'id' => 'bar' }]
},
'bar' => {
'data' => [{ 'type' => 'baz', 'id' => 'foo' },
{ 'type' => 'baz', 'id' => 'buz' }]
}
}
}
}
JSONAPI::Deserializable::Resource[:c1].configure do |cfg|
cfg.default_has_many = proc do |name, _value, ids, types|
{ "custom_#{name}_ids".to_sym => ids,
"custom_#{name}_types".to_sym => types }
end
end
klass = JSONAPI::Deserializable::Resource[:c1]
actual = klass.call(payload)
expected = { custom_foo_ids: %w(baz bar), custom_foo_types: %w(bar foo),
custom_bar_ids: %w(foo buz), custom_bar_types: %w(baz baz),
type: 'foo' }
expect(actual).to eq(expected)
end
it 'overrides the default has_one relationship deserialization scheme' do
payload = {
'data' => {
'type' => 'foo',
'relationships' => {
'foo' => { 'data' => { 'type' => 'bar', 'id' => 'baz' } },
'bar' => { 'data' => { 'type' => 'foo', 'id' => 'bar' } }
}
}
}
JSONAPI::Deserializable::Resource[:c1].configure do |cfg|
cfg.default_has_one = proc do |name, _value, id, type|
{ "custom_#{name}_id".to_sym => id,
"custom_#{name}_type".to_sym => type }
end
end
klass = JSONAPI::Deserializable::Resource[:c1]
actual = klass.call(payload)
expected = { custom_foo_id: 'baz', custom_foo_type: 'bar',
custom_bar_id: 'bar', custom_bar_type: 'foo',
type: 'foo' }
expect(actual).to eq(expected)
end
end

View File

@ -3,7 +3,9 @@ require 'spec_helper'
describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
it 'generates reverse mapping for default type' do it 'generates reverse mapping for default type' do
payload = { 'data' => { 'type' => 'foo' } } payload = { 'data' => { 'type' => 'foo' } }
klass = JSONAPI::Deserializable::Resource klass = Class.new(JSONAPI::Deserializable::Resource) do
type
end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { type: '/data/type' } expected = { type: '/data/type' }
@ -23,9 +25,11 @@ describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
it 'generates reverse mapping for default id' do it 'generates reverse mapping for default id' do
payload = { 'data' => { 'type' => 'foo', 'id' => 'bar' } } payload = { 'data' => { 'type' => 'foo', 'id' => 'bar' } }
klass = JSONAPI::Deserializable::Resource klass = Class.new(JSONAPI::Deserializable::Resource) do
id
end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { id: '/data/id', type: '/data/type' } expected = { id: '/data/id' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -36,7 +40,7 @@ describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
id { |i| { custom_id: i } } id { |i| { custom_id: i } }
end end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { custom_id: '/data/id', type: '/data/type' } expected = { custom_id: '/data/id' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -51,10 +55,11 @@ describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
} }
} }
} }
klass = JSONAPI::Deserializable::Resource klass = Class.new(JSONAPI::Deserializable::Resource) do
attributes
end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { type: '/data/type', expected = { foo: '/data/attributes/foo',
foo: '/data/attributes/foo',
baz: '/data/attributes/baz' } baz: '/data/attributes/baz' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
@ -74,9 +79,7 @@ describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
attribute(:foo) { |foo| { custom_foo: foo } } attribute(:foo) { |foo| { custom_foo: foo } }
end end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { type: '/data/type', expected = { custom_foo: '/data/attributes/foo' }
custom_foo: '/data/attributes/foo',
baz: '/data/attributes/baz' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
@ -92,22 +95,19 @@ describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
} }
} }
klass = Class.new(JSONAPI::Deserializable::Resource) do klass = Class.new(JSONAPI::Deserializable::Resource) do
attribute(:foo) { |foo| { other_foo: foo } } attributes do |value, key|
end
klass.configure do |config|
config.default_attribute = proc do |key, value|
{ "custom_#{key}".to_sym => value } { "custom_#{key}".to_sym => value }
end end
attribute(:foo) { |foo| { other_foo: foo } }
end end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { type: '/data/type', expected = { other_foo: '/data/attributes/foo',
other_foo: '/data/attributes/foo',
custom_baz: '/data/attributes/baz' } custom_baz: '/data/attributes/baz' }
expect(actual).to eq(expected) expect(actual).to eq(expected)
end end
it 'generates reverse mapping for default has_many' do it 'generates reverse mapping for default has_one' do
payload = { payload = {
'data' => { 'data' => {
'type' => 'foo', 'type' => 'foo',
@ -121,10 +121,11 @@ describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
} }
} }
} }
klass = JSONAPI::Deserializable::Resource klass = Class.new(JSONAPI::Deserializable::Resource) do
has_one
end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { type: '/data/type', expected = { foo_id: '/data/relationships/foo',
foo_id: '/data/relationships/foo',
foo_type: '/data/relationships/foo', foo_type: '/data/relationships/foo',
baz_id: '/data/relationships/baz', baz_id: '/data/relationships/baz',
baz_type: '/data/relationships/baz' } baz_type: '/data/relationships/baz' }
@ -147,20 +148,18 @@ describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
} }
} }
klass = Class.new(JSONAPI::Deserializable::Resource) do klass = Class.new(JSONAPI::Deserializable::Resource) do
has_one do |_val, id, type, key|
{ "custom_#{key}_id".to_sym => id,
"custom_#{key}_type".to_sym => type }
end
has_one(:foo) do |_val, id, type| has_one(:foo) do |_val, id, type|
{ other_foo_id: id, { other_foo_id: id,
other_foo_type: type } other_foo_type: type }
end end
end end
klass.configure do |config|
config.default_has_one = proc do |key, _val, id, type|
{ "custom_#{key}_id".to_sym => id,
"custom_#{key}_type".to_sym => type }
end
end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { type: '/data/type', expected = { other_foo_id: '/data/relationships/foo',
other_foo_id: '/data/relationships/foo',
other_foo_type: '/data/relationships/foo', other_foo_type: '/data/relationships/foo',
custom_baz_id: '/data/relationships/baz', custom_baz_id: '/data/relationships/baz',
custom_baz_type: '/data/relationships/baz' } custom_baz_type: '/data/relationships/baz' }
@ -182,10 +181,11 @@ describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
} }
} }
} }
klass = JSONAPI::Deserializable::Resource klass = Class.new(JSONAPI::Deserializable::Resource) do
has_many
end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { type: '/data/type', expected = { foo_ids: '/data/relationships/foo',
foo_ids: '/data/relationships/foo',
foo_types: '/data/relationships/foo', foo_types: '/data/relationships/foo',
baz_ids: '/data/relationships/baz', baz_ids: '/data/relationships/baz',
baz_types: '/data/relationships/baz' } baz_types: '/data/relationships/baz' }
@ -208,20 +208,17 @@ describe JSONAPI::Deserializable::Resource, '#reverse_mapping' do
} }
} }
klass = Class.new(JSONAPI::Deserializable::Resource) do klass = Class.new(JSONAPI::Deserializable::Resource) do
has_many do |_val, ids, types, key|
{ "custom_#{key}_ids".to_sym => ids,
"custom_#{key}_types".to_sym => types }
end
has_many(:foo) do |_val, ids, types| has_many(:foo) do |_val, ids, types|
{ other_foo_ids: ids, { other_foo_ids: ids,
other_foo_types: types } other_foo_types: types }
end end
end end
klass.configure do |config|
config.default_has_many = proc do |key, _val, ids, types|
{ "custom_#{key}_ids".to_sym => ids,
"custom_#{key}_types".to_sym => types }
end
end
actual = klass.new(payload).reverse_mapping actual = klass.new(payload).reverse_mapping
expected = { type: '/data/type', expected = { other_foo_ids: '/data/relationships/foo',
other_foo_ids: '/data/relationships/foo',
other_foo_types: '/data/relationships/foo', other_foo_types: '/data/relationships/foo',
custom_baz_ids: '/data/relationships/baz', custom_baz_ids: '/data/relationships/baz',
custom_baz_types: '/data/relationships/baz' } custom_baz_types: '/data/relationships/baz' }