Class: Lolita::Support::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/lolita/support/formatter.rb,
lib/lolita/support/formatter/rails.rb

Overview

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)

Direct Known Subclasses

Rails

Defined Under Namespace

Classes: Rails

Instance Method Summary collapse

Constructor Details

#initialize(format = nil, &block) ⇒ Formatter

Returns a new instance of Formatter.



19
20
21
22
# File 'lib/lolita/support/formatter.rb', line 19

def initialize(format=nil,&block)
  @format=format
  @block=block if block_given?
end

Instance Method Details

#blockObject



28
29
30
# File 'lib/lolita/support/formatter.rb', line 28

def block
  @block
end

#formatObject



24
25
26
# File 'lib/lolita/support/formatter.rb', line 24

def format
  @format
end

#with(value, *optional_values) ⇒ Object



32
33
34
35
36
37
38
39
40
# 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