class Lolita::Support::Formatter

Containes different kind of formaters. Change output format of different input types. To define, pass block, or String.

Exmaple

Lolita::Support::Formatter.new do |value|
  value.to_i**2
end
# or as String
Lolita::Support::Formatter.new("%U")

To format any value with defined formater call with

Example

# Previous examples may be called like this
formatter.with(1)
formatter.with(Date.today)

Public Class Methods

new(format=nil,&block) click to toggle source
# File lib/lolita/support/formatter.rb, line 19
def initialize(format=nil,&block)
  @format=format
  @block=block if block_given?
end

Public Instance Methods

block() click to toggle source
# File lib/lolita/support/formatter.rb, line 28
def block
  @block
end
format() click to toggle source
# File lib/lolita/support/formatter.rb, line 24
def format
  @format
end
with(value,*optional_values) click to toggle source
# File lib/lolita/support/formatter.rb, line 32
def with(value,*optional_values)
  if @block
    @block.call(value,*optional_values)
  elsif @format
    use_format_for(value,*optional_values)
  else
    use_default_format(value,*optional_values)
  end
end

Private Instance Methods

call_block(value,*optional_values) click to toggle source
# File lib/lolita/support/formatter.rb, line 56
def call_block(value,*optional_values)
  value.send(:format,value,*optional_values)
end
use_default_format(value,*optional_values) click to toggle source
# File lib/lolita/support/formatter.rb, line 44
def use_default_format(value,*optional_values)
  value
end
use_format_for(value, *optional_values) click to toggle source
# File lib/lolita/support/formatter.rb, line 48
def use_format_for(value, *optional_values)
  if value.respond_to?(:format)
    call_block(value,*optional_values)
  else
    @format ? (@format % value) : value
  end
end