class Florrick::Formatter

Public Class Methods

add(name, types = [], &block) click to toggle source

Add a new global formatter

# File lib/florrick/formatter.rb, line 11
def add(name, types = [], &block)
  formatters[name.to_sym] = self.new(name, types, &block)
end
convert(name, value) click to toggle source

Run a string through a given formatter and return the result. If not possible, return false.

# File lib/florrick/formatter.rb, line 16
def convert(name, value)
  if formatter = formatters[name.to_sym]
    formatter.convert(value)
  else
    false
  end
end
formatters() click to toggle source

Return all formatters available

# File lib/florrick/formatter.rb, line 6
def formatters
  @formatters ||= {}
end
new(name, types, &block) click to toggle source
# File lib/florrick/formatter.rb, line 25
def initialize(name, types, &block)
  @name, @types, @block = name, types, block
end

Public Instance Methods

convert(value) click to toggle source
# File lib/florrick/formatter.rb, line 29
def convert(value)
  if @types.empty? || @types.any? { |t| value.is_a?(t)}
    @block.call(value)
  else
    false
  end
rescue
  '???'
end