Class: Jackal::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/jackal/formatter.rb

Overview

Payload formatter

Constant Summary collapse

SOURCE =

Returns:

  • (String, Symbol)
nil
DESTINATION =

Returns:

  • (String, Symbol)
nil

Class Method Summary collapse

Instance Method Summary collapse

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

.descendantsArray<Class>

Returns registered formatters.

Returns:

  • (Array<Class>)

    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

#destinationSymbol

Returns:

  • (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

Parameters:

  • payload (Smash)

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/jackal/formatter.rb', line 62

def format(payload)
  raise NotImplementedError
end

#sourceSymbol

Returns:

  • (Symbol)


49
50
51
# File 'lib/jackal/formatter.rb', line 49

def source
  self.class.const_get(:SOURCE).to_sym
end