Class: Porridge::Factory
- Inherits:
-
Object
- Object
- Porridge::Factory
- Defined in:
- lib/porridge/factory.rb
Overview
Factory is a class that is capable of instantiating various porridge serializers and extractors. All extractor- creation methods are suffixed with _extractor
, all serializer-creation methods are suffixed with _serializer
, and all field serializer-creation methods are suffixed with _field_serializer
.
You may subclass this class if you wish to change its behavior. For example, if you wished to substitute your own “from name” extractor, that accesses a hash value rather than sends a method call, for the default one:
class CustomPorridgeFactory < Porridge::Factory
def from_name_extractor(name)
extractor HashValueExtractor.new(name)
end
end
#from_name_field_serializer, #attribute_extractor, and #attribute_field_serializer would then be automatically updated in the process, along with any other methods that depend on the aforementioned ones.
This method is rarely used directly. Typically, it is used in conjunction with a SerializerDefiner and/or SerializerDefinition instance.
Instance Method Summary collapse
- #association_extractor(serializer:, extractor: nil, extraction_name: nil, callback: nil, &block) ⇒ Object
- #association_field_serializer(name, options = {}, &block) ⇒ Object
- #attribute_extractor(name: nil, callback: nil, &block) ⇒ Object
- #attribute_field_serializer(name, callback = nil, extraction_name: nil, &block) ⇒ Object
- #attributes_field_serializer(*names) ⇒ Object
- #belongs_to_extractor ⇒ Object
- #belongs_to_field_serializer ⇒ Object
- #chain_serializer(*bases) ⇒ Object
- #custom_extractor(callback) ⇒ Object
- #extractor(base) ⇒ Object
- #field_serializer(name, extractor) ⇒ Object
- #for_extracted_serializer(serializer, extractor) ⇒ Object
- #from_name_extractor(name) ⇒ Object
-
#has_many_extractor ⇒ Object
rubocop:disable Naming/PredicateName.
-
#has_many_field_serializer ⇒ Object
rubocop:disable Naming/PredicateName.
- #serializer(base) ⇒ Object
- #serializer_for_extracted ⇒ Object
- #serializers ⇒ Object
Instance Method Details
#association_extractor(serializer:, extractor: nil, extraction_name: nil, callback: nil, &block) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/porridge/factory.rb', line 42 def association_extractor(serializer:, extractor: nil, extraction_name: nil, callback: nil, &block) extractor SerializingExtractor.new( extractor || attribute_extractor(name: extraction_name, callback: callback, &block), serializer ) end |
#association_field_serializer(name, options = {}, &block) ⇒ Object
92 93 94 95 |
# File 'lib/porridge/factory.rb', line 92 def association_field_serializer(name, = {}, &block) [:extraction_name] ||= name field_serializer(name, association_extractor(**, &block)) end |
#attribute_extractor(name: nil, callback: nil, &block) ⇒ Object
38 39 40 |
# File 'lib/porridge/factory.rb', line 38 def attribute_extractor(name: nil, callback: nil, &block) custom_extractor(callback || block) || from_name_extractor(name) end |
#attribute_field_serializer(name, callback = nil, extraction_name: nil, &block) ⇒ Object
84 85 86 |
# File 'lib/porridge/factory.rb', line 84 def attribute_field_serializer(name, callback = nil, extraction_name: nil, &block) field_serializer(name, attribute_extractor(name: extraction_name || name, callback: callback, &block)) end |
#attributes_field_serializer(*names) ⇒ Object
88 89 90 |
# File 'lib/porridge/factory.rb', line 88 def attributes_field_serializer(*names) serializers(*names.map { |name| attribute_field_serializer(name) }) end |
#belongs_to_extractor ⇒ Object
49 50 51 |
# File 'lib/porridge/factory.rb', line 49 def belongs_to_extractor(...) association_extractor(...) end |
#belongs_to_field_serializer ⇒ Object
97 98 99 |
# File 'lib/porridge/factory.rb', line 97 def belongs_to_field_serializer(...) association_field_serializer(...) end |
#chain_serializer(*bases) ⇒ Object
64 65 66 |
# File 'lib/porridge/factory.rb', line 64 def chain_serializer(*bases) serializer ChainSerializer.new(*bases) end |
#custom_extractor(callback) ⇒ Object
34 35 36 |
# File 'lib/porridge/factory.rb', line 34 def custom_extractor(callback) extractor callback end |
#extractor(base) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/porridge/factory.rb', line 23 def extractor(base) return nil if base.nil? Extractor.ensure_valid!(base) base end |
#field_serializer(name, extractor) ⇒ Object
80 81 82 |
# File 'lib/porridge/factory.rb', line 80 def field_serializer(name, extractor) serializer FieldSerializer.new(name, extractor) end |
#for_extracted_serializer(serializer, extractor) ⇒ Object
72 73 74 |
# File 'lib/porridge/factory.rb', line 72 def for_extracted_serializer(serializer, extractor) serializer SerializerForExtracted.new(serializer, extractor) end |
#from_name_extractor(name) ⇒ Object
30 31 32 |
# File 'lib/porridge/factory.rb', line 30 def from_name_extractor(name) extractor SendExtractor.new(name) end |
#has_many_extractor ⇒ Object
rubocop:disable Naming/PredicateName
53 54 55 |
# File 'lib/porridge/factory.rb', line 53 def has_many_extractor(...) # rubocop:disable Naming/PredicateName association_extractor(...) end |
#has_many_field_serializer ⇒ Object
rubocop:disable Naming/PredicateName
101 102 103 |
# File 'lib/porridge/factory.rb', line 101 def has_many_field_serializer(...) # rubocop:disable Naming/PredicateName association_field_serializer(...) end |
#serializer(base) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/porridge/factory.rb', line 57 def serializer(base) return nil if base.nil? Serializer.ensure_valid!(base) base end |
#serializer_for_extracted ⇒ Object
76 77 78 |
# File 'lib/porridge/factory.rb', line 76 def serializer_for_extracted(...) for_extracted_serializer(...) end |
#serializers ⇒ Object
68 69 70 |
# File 'lib/porridge/factory.rb', line 68 def serializers(...) chain_serializer(...) end |