Class: FunWithJsonApi::Deserializer
- Inherits:
-
Object
- Object
- FunWithJsonApi::Deserializer
- Extended by:
- DeserializerClassMethods
- Defined in:
- lib/fun_with_json_api/deserializer.rb
Defined Under Namespace
Classes: ResourceAuthorizerDummy
Instance Attribute Summary collapse
-
#id_param ⇒ Object
readonly
Returns the value of attribute id_param.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.create(options = {}) ⇒ Object
Creates a new instance of a.
Instance Method Summary collapse
- #attribute_for(attribute_name) ⇒ Object
- #attributes ⇒ Object
- #format_collection_ids(collection) ⇒ Object
- #format_resource_id(resource) ⇒ Object
-
#initialize(options = {}) ⇒ Deserializer
constructor
A new instance of Deserializer.
-
#load_collection_from_id_values(id_values) ⇒ Object
Loads a collection of of ‘resource_class` instances with `id_param` matching `id_values`.
-
#load_resource_from_id_value(id_value) ⇒ Object
Loads a single instance of ‘resource_class` with a `id_param` matching `id_value`.
- #relationship_for(resource_name) ⇒ Object
- #relationships ⇒ Object
- #resource_authorizer ⇒ Object
- #resource_class ⇒ Object
- #resource_collection ⇒ Object
-
#sanitize_params(params) ⇒ Object
Takes a parsed params hash from ActiveModelSerializers::Deserialization and sanitizes values.
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( = {}) @id_param = .fetch(:id_param) { self.class.id_param } @type = .fetch(:type) { self.class.type } @resource_class = [:resource_class] @resource_collection = [:resource_collection] if @type @resource_authorizer = [:resource_authorizer] () () end |
Instance Attribute Details
#id_param ⇒ Object (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 |
#type ⇒ Object (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( = {}) new() 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 |
#attributes ⇒ Object
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 |
#relationships ⇒ Object
79 80 81 |
# File 'lib/fun_with_json_api/deserializer.rb', line 79 def relationships relationship_lookup.values end |
#resource_authorizer ⇒ Object
71 72 73 |
# File 'lib/fun_with_json_api/deserializer.rb', line 71 def @resource_authorizer ||= ResourceAuthorizerDummy.new end |
#resource_class ⇒ Object
63 64 65 |
# File 'lib/fun_with_json_api/deserializer.rb', line 63 def resource_class @resource_class ||= self.class.resource_class end |
#resource_collection ⇒ Object
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 |