Class: FunWithJsonApi::Deserializer

Inherits:
Object
  • Object
show all
Extended by:
DeserializerClassMethods
Defined in:
lib/fun_with_json_api/deserializer.rb

Defined Under Namespace

Classes: ResourceAuthorizerDummy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeserializerClassMethods

attribute, attribute_names, belongs_to, build_attributes, build_relationships, has_many, relationship_names

Constructor Details

#initialize(options = {}) ⇒ Deserializer

Returns a new instance of Deserializer.



27
28
29
30
31
32
33
34
35
# 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

Instance Attribute Details

#id_paramObject (readonly)

Returns the value of attribute id_param.



24
25
26
# File 'lib/fun_with_json_api/deserializer.rb', line 24

def id_param
  @id_param
end

#typeObject (readonly)

Returns the value of attribute type.



25
26
27
# File 'lib/fun_with_json_api/deserializer.rb', line 25

def type
  @type
end

Class Method Details

.create(options = {}) ⇒ Object

Creates a new instance of a



17
18
19
# File 'lib/fun_with_json_api/deserializer.rb', line 17

def self.create(options = {})
  new(options)
end

Instance Method Details

#attribute_for(attribute_name) ⇒ Object



83
84
85
# File 'lib/fun_with_json_api/deserializer.rb', line 83

def attribute_for(attribute_name)
  attribute_lookup.fetch(attribute_name)
end

#attributesObject



75
76
77
# File 'lib/fun_with_json_api/deserializer.rb', line 75

def attributes
  attribute_lookup.values
end

#format_collection_ids(collection) ⇒ Object



46
47
48
# 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) ⇒ Object



42
43
44
# 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) ⇒ Object

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



38
39
40
# 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) ⇒ Object

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



51
52
53
# 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) ⇒ Object



87
88
89
# File 'lib/fun_with_json_api/deserializer.rb', line 87

def relationship_for(resource_name)
  relationship_lookup.fetch(resource_name)
end

#relationshipsObject



79
80
81
# File 'lib/fun_with_json_api/deserializer.rb', line 79

def relationships
  relationship_lookup.values
end

#resource_authorizerObject



71
72
73
# File 'lib/fun_with_json_api/deserializer.rb', line 71

def resource_authorizer
  @resource_authorizer ||= ResourceAuthorizerDummy.new
end

#resource_classObject



63
64
65
# File 'lib/fun_with_json_api/deserializer.rb', line 63

def resource_class
  @resource_class ||= self.class.resource_class
end

#resource_collectionObject



67
68
69
# File 'lib/fun_with_json_api/deserializer.rb', line 67

def resource_collection
  @resource_collection ||= resource_class
end

#sanitize_params(params) ⇒ Object

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



56
57
58
59
60
61
# 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