module JsonAPIObjectMapper::Deserialize::DSL

Constants

DEFAULT_BLOCK
HAS_MANY_BLOCK

Public Class Methods

extended(klass) click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 9
def self.extended(klass)
  klass.include ClassMethods
end

Public Instance Methods

attribute(attribute_name, &block) click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 23
def attribute(attribute_name, &block)
  attr_blocks[attribute_name.to_s] = block || DEFAULT_BLOCK
  define_method(attribute_name.to_sym) { fetch_attribute(attribute_name) }
end
attributes(*attributes_names) click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 28
def attributes(*attributes_names)
  attributes_names.each(&method(:attribute))
end
belongs_to(relationship_name, **options, &block)
Alias for: has_one
has_many(relationship_name, **options, &block) click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 39
def has_many(relationship_name, **options, &block)
  rel_options_process!(relationship_name, options)
  rel_has_many_blocks[relationship_name.to_s] = block || HAS_MANY_BLOCK
  define_method(relationship_name.to_sym) { fetch_relationship(relationship_name) }
end
has_one(relationship_name, **options, &block) click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 32
def has_one(relationship_name, **options, &block)
  rel_options_process!(relationship_name, options)
  rel_has_one_blocks[relationship_name.to_s] = block || DEFAULT_BLOCK
  define_method(relationship_name.to_sym) { fetch_relationship(relationship_name) }
end
Also aliased as: belongs_to
id(&block) click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 13
def id(&block)
  self.id_block = block || DEFAULT_BLOCK
  define_method(:id) { fetch_attribute(:id) }
end
kind_of_resource?(klass) click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 45
def kind_of_resource?(klass)
  !klass.nil? && klass < Resource
end
type(&block) click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 18
def type(&block)
  self.type_block = block || DEFAULT_BLOCK
  define_method(:type) { fetch_attribute(:type) }
end

Private Instance Methods

rel_options_process!(relationship_name, **options) click to toggle source
# File lib/jsonapi-object-mapper/deserialize/dsl.rb, line 51
def rel_options_process!(relationship_name, **options)
  embed_klass = options.delete(:embed_with)
  return if embed_klass.nil?

  embed_klass = embed_klass.is_a?(String) ? Kernel.const_get(embed_klass) : embed_klass
  if kind_of_resource?(embed_klass)
    rel_options[relationship_name.to_s] = { embed_with: embed_klass }
  else
    raise InvalidEmbedKlass
  end
rescue NameError # Rescue from `Kernel.const_get/1`
  raise InvalidEmbedKlass
end