Class: Jackal::Formatter
- Inherits:
-
Object
- Object
- Jackal::Formatter
- Defined in:
- lib/jackal/formatter.rb
Overview
Payload formatter
Constant Summary collapse
- SOURCE =
nil
- DESTINATION =
nil
Class Method Summary collapse
-
.descendants ⇒ Array<Class>
Registered formatters.
-
.inherited(klass) ⇒ Object
Register formatter.
Instance Method Summary collapse
- #destination ⇒ Symbol
-
#format(payload) ⇒ Object
Apply format to payload.
-
#initialize(callback = nil) ⇒ self
constructor
Create a new instance.
-
#method_missing(m_name, *args, &block) ⇒ Object
Provide a simple proxy out to originating callback if provided to access helpers.
- #source ⇒ Symbol
Constructor Details
#initialize(callback = nil) ⇒ self
Create a new instance
29 30 31 32 33 34 35 36 |
# File 'lib/jackal/formatter.rb', line 29 def initialize(callback=nil) @callback = callback [:SOURCE, :DESTINATION].each do |key| unless(self.class.const_get(key)) raise NotImplementedError.new("Formatter class must define #{key} constant") end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m_name, *args, &block) ⇒ Object
Provide a simple proxy out to originating callback if provided to access helpers
40 41 42 43 44 45 46 |
# File 'lib/jackal/formatter.rb', line 40 def method_missing(m_name, *args, &block) if(@callback && @callback.respond_to?(m_name)) @callback.send(m_name, *args, &block) else super end end |
Class Method Details
.descendants ⇒ Array<Class>
Returns registered formatters.
15 16 17 |
# File 'lib/jackal/formatter.rb', line 15 def descendants @_descendants ||= [] end |
.inherited(klass) ⇒ Object
Register formatter
10 11 12 |
# File 'lib/jackal/formatter.rb', line 10 def inherited(klass) Formatter.descendants.push(klass).uniq! end |
Instance Method Details
#destination ⇒ Symbol
54 55 56 |
# File 'lib/jackal/formatter.rb', line 54 def destination self.class.const_get(:DESTINATION).to_sym end |
#format(payload) ⇒ Object
Apply format to payload
62 63 64 |
# File 'lib/jackal/formatter.rb', line 62 def format(payload) raise NotImplementedError end |
#source ⇒ Symbol
49 50 51 |
# File 'lib/jackal/formatter.rb', line 49 def source self.class.const_get(:SOURCE).to_sym end |