Class: SimpleJsonapi::SerializerInferrer
- Inherits:
-
Object
- Object
- SimpleJsonapi::SerializerInferrer
- Defined in:
- lib/simple_jsonapi/helpers/serializer_inferrer.rb
Overview
Identifies the serializer class that should be used for a resource or error object.
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Class Method Summary collapse
Instance Method Summary collapse
- #add(resource_class, serializer_class) ⇒ void
- #default_serializer ⇒ Class?
- #default_serializer? ⇒ Boolean
-
#infer(resource) ⇒ Class
A serializer class.
-
#initialize(explicit_mappings: nil, namespace: nil) {|object| ... } ⇒ SerializerInferrer
constructor
A new instance of SerializerInferrer.
- #merge(explicit_mappings) ⇒ self
Constructor Details
#initialize(explicit_mappings: nil, namespace: nil) {|object| ... } ⇒ SerializerInferrer
Returns a new instance of SerializerInferrer.
38 39 40 41 42 43 44 |
# File 'lib/simple_jsonapi/helpers/serializer_inferrer.rb', line 38 def initialize(explicit_mappings: nil, namespace: nil, &block) @explicit_mappings = {} @explicit_mappings.merge!(explicit_mappings) if explicit_mappings @namespace = namespace @infer_proc = block end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
6 7 8 |
# File 'lib/simple_jsonapi/helpers/serializer_inferrer.rb', line 6 def namespace @namespace end |
Class Method Details
.wrap(serializer) ⇒ SerializerInferrer
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/simple_jsonapi/helpers/serializer_inferrer.rb', line 23 def self.wrap(serializer) if serializer.is_a?(SerializerInferrer) serializer elsif serializer.present? klass = serializer_class(serializer) SerializerInferrer.new { |_resource| klass } else SimpleJsonapi.serializer_inferrer end end |
Instance Method Details
#add(resource_class, serializer_class) ⇒ void
This method returns an undefined value.
60 61 62 |
# File 'lib/simple_jsonapi/helpers/serializer_inferrer.rb', line 60 def add(resource_class, serializer_class) @explicit_mappings[resource_class.name] = serializer_class end |
#default_serializer ⇒ Class?
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/simple_jsonapi/helpers/serializer_inferrer.rb', line 74 def default_serializer unless defined?(@default_serializer) begin @default_serializer = infer(nil) rescue SerializerInferenceError @default_serializer = nil end end @default_serializer end |
#default_serializer? ⇒ Boolean
85 86 87 |
# File 'lib/simple_jsonapi/helpers/serializer_inferrer.rb', line 85 def default_serializer? default_serializer != nil end |
#infer(resource) ⇒ Class
Returns A serializer class.
67 68 69 70 71 |
# File 'lib/simple_jsonapi/helpers/serializer_inferrer.rb', line 67 def infer(resource) serializer = (@infer_proc || default_infer_proc).call(resource) raise SerializerInferenceError.new(resource) unless serializer serializer end |
#merge(explicit_mappings) ⇒ self
50 51 52 53 54 55 |
# File 'lib/simple_jsonapi/helpers/serializer_inferrer.rb', line 50 def merge(explicit_mappings) explicit_mappings.each do |resource_class, serializer_class| add(resource_class, serializer_class) end self end |