Class: Porridge::SerializerDefiner
- Inherits:
-
Object
- Object
- Porridge::SerializerDefiner
- Defined in:
- lib/porridge/serializer_definer.rb
Overview
SerializerDefiner is a class that wraps a Factory and allows serializes to be easily defined with an elegant DSL.
Constant Summary collapse
- FACTORY_PREFIX =
'create_'
- SERIALIZER_SUFFIX =
'_serializer'
- FIELD_SERIALIZER_SUFFIX =
'_field_serializer'
Instance Attribute Summary collapse
-
#factory ⇒ Object
readonly
private
Returns the value of attribute factory.
Instance Method Summary collapse
- #add_serializer(serializer) ⇒ Object private
- #added_serializers ⇒ Object
- #create_method?(method_name) ⇒ Boolean private
- #defined_serializer ⇒ Object
- #field_serializer_method?(method_name) ⇒ Boolean private
-
#initialize(factory = Factory.new) ⇒ SerializerDefiner
constructor
A new instance of SerializerDefiner.
- #method_missing(method_name, *args, **kwargs, &block) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #serializer ⇒ Object
- #serializer_method?(method_name) ⇒ Boolean private
Constructor Details
#initialize(factory = Factory.new) ⇒ SerializerDefiner
Returns a new instance of SerializerDefiner.
16 17 18 19 |
# File 'lib/porridge/serializer_definer.rb', line 16 def initialize(factory = Factory.new) @factory = factory @serializers = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/porridge/serializer_definer.rb', line 21 def method_missing(method_name, *args, **kwargs, &block) method_name = method_name.to_s if create_method? method_name return factory.send(method_name.delete_prefix(FACTORY_PREFIX), *args, **kwargs, &block) end to_send = SERIALIZER_SUFFIX if serializer_method? method_name to_send = FIELD_SERIALIZER_SUFFIX if field_serializer_method? method_name return add_serializer(factory.send(method_name + to_send, *args, **kwargs, &block)) if to_send super(method_name.to_sym, *args, &block) end |
Instance Attribute Details
#factory ⇒ Object (readonly, private)
Returns the value of attribute factory.
73 74 75 |
# File 'lib/porridge/serializer_definer.rb', line 73 def factory @factory end |
Instance Method Details
#add_serializer(serializer) ⇒ Object (private)
69 70 71 |
# File 'lib/porridge/serializer_definer.rb', line 69 def add_serializer(serializer) @serializers << serializer end |
#added_serializers ⇒ Object
47 48 49 |
# File 'lib/porridge/serializer_definer.rb', line 47 def added_serializers @serializers end |
#create_method?(method_name) ⇒ Boolean (private)
57 58 59 |
# File 'lib/porridge/serializer_definer.rb', line 57 def create_method?(method_name) method_name.start_with?(FACTORY_PREFIX) && factory.respond_to?(method_name.delete_prefix(FACTORY_PREFIX)) end |
#defined_serializer ⇒ Object
43 44 45 |
# File 'lib/porridge/serializer_definer.rb', line 43 def defined_serializer factory.serializers(*added_serializers) end |
#field_serializer_method?(method_name) ⇒ Boolean (private)
65 66 67 |
# File 'lib/porridge/serializer_definer.rb', line 65 def field_serializer_method?(method_name) factory.respond_to?(method_name + FIELD_SERIALIZER_SUFFIX) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
35 36 37 38 39 40 41 |
# File 'lib/porridge/serializer_definer.rb', line 35 def respond_to_missing?(method_name, include_private = false) method_name = method_name.to_s super(method_name.to_sym, include_private) || create_method?(method_name) || serializer_method?(method_name) || field_serializer_method?(method_name) end |
#serializer ⇒ Object
51 52 53 |
# File 'lib/porridge/serializer_definer.rb', line 51 def serializer(...) add_serializer(factory.serializer(...)) end |
#serializer_method?(method_name) ⇒ Boolean (private)
61 62 63 |
# File 'lib/porridge/serializer_definer.rb', line 61 def serializer_method?(method_name) factory.respond_to?(method_name + SERIALIZER_SUFFIX) end |