class FunWithJsonApi::Deserializer
Attributes
Use DeserializerClass.create to build new instances
Public Class Methods
Creates a new instance of a
# File lib/fun_with_json_api/deserializer.rb, line 17 def self.create(options = {}) new(options) end
# 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
# File lib/fun_with_json_api/deserializer.rb, line 83 def attribute_for(attribute_name) attribute_lookup.fetch(attribute_name) end
# File lib/fun_with_json_api/deserializer.rb, line 75 def attributes attribute_lookup.values end
# File lib/fun_with_json_api/deserializer.rb, line 46 def format_collection_ids(collection) collection.map { |resource| format_resource_id(resource) } end
# File lib/fun_with_json_api/deserializer.rb, line 42 def format_resource_id(resource) resource.public_send(id_param).to_s end
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
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
# File lib/fun_with_json_api/deserializer.rb, line 87 def relationship_for(resource_name) relationship_lookup.fetch(resource_name) end
# File lib/fun_with_json_api/deserializer.rb, line 79 def relationships relationship_lookup.values end
# File lib/fun_with_json_api/deserializer.rb, line 63 def resource_class @resource_class ||= self.class.resource_class end
# File lib/fun_with_json_api/deserializer.rb, line 67 def resource_collection @resource_collection ||= resource_class end
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
# 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
# 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
# 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
# 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
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
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