class FunWithJsonApi::Deserializer

Attributes

attribute_lookup[R]
id_param[R]

Use DeserializerClass.create to build new instances

relationship_lookup[R]
type[R]

Public Class Methods

create(options = {}) click to toggle source

Creates a new instance of a

# File lib/fun_with_json_api/deserializer.rb, line 17
def self.create(options = {})
  new(options)
end
new(options = {}) click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 27
def initialize(options = {})
  @id_param = options.fetch(:id_param) { self.class.id_param }
  @type = options.fetch(:type) { self.class.type }
  @resource_class = options[:resource_class]
  @resource_collection = options[:resource_collection] if @type
  @resource_authorizer = options[:resource_authorizer]
  load_attributes_from_options(options)
  load_relationships_from_options(options)
end

Public Instance Methods

attribute_for(attribute_name) click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 83
def attribute_for(attribute_name)
  attribute_lookup.fetch(attribute_name)
end
attributes() click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 75
def attributes
  attribute_lookup.values
end
format_collection_ids(collection) click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 46
def format_collection_ids(collection)
  collection.map { |resource| format_resource_id(resource) }
end
format_resource_id(resource) click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 42
def format_resource_id(resource)
  resource.public_send(id_param).to_s
end
load_collection_from_id_values(id_values) click to toggle source

Loads a collection of of ‘resource_class` instances with `id_param` matching `id_values`

# File lib/fun_with_json_api/deserializer.rb, line 38
def load_collection_from_id_values(id_values)
  resource_collection.where(id_param => id_values)
end
load_resource_from_id_value(id_value) click to toggle source

Loads a single instance of ‘resource_class` with a `id_param` matching `id_value`

# File lib/fun_with_json_api/deserializer.rb, line 51
def load_resource_from_id_value(id_value)
  resource_collection.find_by(id_param => id_value)
end
relationship_for(resource_name) click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 87
def relationship_for(resource_name)
  relationship_lookup.fetch(resource_name)
end
relationships() click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 79
def relationships
  relationship_lookup.values
end
resource_authorizer() click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 71
def resource_authorizer
  @resource_authorizer ||= ResourceAuthorizerDummy.new
end
resource_class() click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 63
def resource_class
  @resource_class ||= self.class.resource_class
end
resource_collection() click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 67
def resource_collection
  @resource_collection ||= resource_class
end
sanitize_params(params) click to toggle source

Takes a parsed params hash from ActiveModelSerializers::Deserialization and sanitizes values

# File lib/fun_with_json_api/deserializer.rb, line 56
def sanitize_params(params)
  Hash[
    serialize_attribute_values(attributes, params) +
    serialize_attribute_values(relationships, params)
  ]
end

Private Instance Methods

filter_attributes_by_name(attribute_names, attributes) click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 121
def filter_attributes_by_name(attribute_names, attributes)
  if attribute_names
    attributes.keep_if { |attribute| attribute_names.include?(attribute) }
  else
    attributes
  end
end
filter_relationships_by_name(relationship_names, relationships) click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 129
def filter_relationships_by_name(relationship_names, relationships)
  if relationship_names
    relationships.keep_if { |relationship| relationship_names.include?(relationship) }
  else
    relationships
  end
end
load_attributes_from_options(options) click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 96
def load_attributes_from_options(options)
  attributes = filter_attributes_by_name(options[:attributes], self.class.attribute_names)
  @attribute_lookup = {}
  self.class.build_attributes(attributes).each do |attribute|
    @attribute_lookup[attribute.name] = attribute
  end
end
load_relationships_from_options(options = {}) click to toggle source
# File lib/fun_with_json_api/deserializer.rb, line 104
def load_relationships_from_options(options = {})
  options_config = {}

  # Filter resources and build an options hash for each
  filter_relationships_by_name(
    options[:relationships], self.class.relationship_names
  ).each do |relationship|
    options_config[relationship] = options.fetch(relationship, {})
  end

  # Build the relationships and store them into a lookup hash
  @relationship_lookup = {}
  self.class.build_relationships(options_config).each do |relationship|
    @relationship_lookup[relationship.name] = relationship
  end
end
serialize_attribute(attribute, params) click to toggle source

Calls <attribute.as> on the current instance, override the

# File lib/fun_with_json_api/deserializer.rb, line 144
def serialize_attribute(attribute, params)
  raw_value = params.fetch(attribute.param_value)
  [attribute.param_value, public_send(attribute.sanitize_attribute_method, raw_value)]
end
serialize_attribute_values(attributes, params) click to toggle source

Calls <attribute.as> on the current instance, override the

# File lib/fun_with_json_api/deserializer.rb, line 138
def serialize_attribute_values(attributes, params)
  attributes.select { |attribute| params.key?(attribute.param_value) }
            .map { |attribute| serialize_attribute(attribute, params) }
end