Class: Florrick::Formatter
- Inherits:
-
Object
- Object
- Florrick::Formatter
- Defined in:
- lib/florrick/formatter.rb
Class Method Summary collapse
-
.add(name, types = [], &block) ⇒ Object
Add a new global formatter.
-
.convert(name, value) ⇒ Object
Run a string through a given formatter and return the result.
-
.formatters ⇒ Object
Return all formatters available.
Instance Method Summary collapse
- #convert(value) ⇒ Object
-
#initialize(name, types, &block) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(name, types, &block) ⇒ Formatter
Returns a new instance of Formatter.
25 26 27 |
# File 'lib/florrick/formatter.rb', line 25 def initialize(name, types, &block) @name, @types, @block = name, types, block end |
Class Method Details
.add(name, types = [], &block) ⇒ Object
Add a new global formatter
11 12 13 |
# 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) ⇒ Object
Run a string through a given formatter and return the result. If not possible, return false.
16 17 18 19 20 21 22 |
# 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 ⇒ Object
Return all formatters available
6 7 8 |
# File 'lib/florrick/formatter.rb', line 6 def formatters @formatters ||= {} end |
Instance Method Details
#convert(value) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# 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 |