Class: View::Formatter Abstract
- Inherits:
-
Object
- Object
- View::Formatter
- Defined in:
- lib/view/formatter.rb
Overview
Subclass and override #format to implement your own formatter.
Direct Known Subclasses
Array, Auto, Blank, Boolean, Currency, Date, Datetime, DefinitionList, Delimited, FileLink, Guess, HtmlSafe, Human, Image, Link, Percentage, Phone, Precision, Self, Size, Table
Instance Attribute Summary (collapse)
-
- (Object) block
readonly
Returns the value of attribute block.
-
- (Object) template
readonly
Returns the value of attribute template.
-
- (Object) value
readonly
Returns the value of attribute value.
Class Method Summary (collapse)
-
+ (Object) as(type)
Specify your own name for the formatter.
-
+ (Object) inherited(formatter)
When you inherit from View::Formatter, the formatters goes on the list, but in reverse order, so that newer formatters kan override older ones.
-
+ (Object) skip_blank_formatter
By default, blank values (nil, empty strings, etc), will override any formatter you specified.
-
+ (String) type
The type of the formatter, either set via .as or automatically deducted from the class name.
Instance Method Summary (collapse)
-
- (Object) all_options
All options, unfiltered.
-
- (String) format
abstract
If you didn't specify a format instance method inside specific formatters, this will raise an error.
-
- (String) format!
The final result of the formatter, with the block captured if given.
-
- (Hash) options
The "safe" options that you can toss around to helper methods.
-
- (Object) to_s
A hook for formatters to override so they can inject code between above the formatting, without all their inherited classes knowing about it.
Instance Attribute Details
- (Object) block (readonly)
Returns the value of attribute block
6 7 8 |
# File 'lib/view/formatter.rb', line 6 def block @block end |
- (Object) template (readonly)
Returns the value of attribute template
6 7 8 |
# File 'lib/view/formatter.rb', line 6 def template @template end |
- (Object) value (readonly)
Returns the value of attribute value
6 7 8 |
# File 'lib/view/formatter.rb', line 6 def value @value end |
Class Method Details
+ (Object) as(type)
Specify your own name for the formatter. By default the name of the class will be used (without any namespacing), but you can override it yourself by calling this method.
36 37 38 |
# File 'lib/view/formatter.rb', line 36 def self.as(type) @type = type end |
+ (Object) inherited(formatter)
When you inherit from View::Formatter, the formatters goes on the list, but in reverse order, so that newer formatters kan override older ones.
19 20 21 22 |
# File 'lib/view/formatter.rb', line 19 def self.inherited(formatter) super formatters.unshift(formatter) end |
+ (Object) skip_blank_formatter
By default, blank values (nil, empty strings, etc), will override any formatter you specified. This way empty values are handled globally.
If you don't want this, you can either turn it off per formatter. Call this method to turn it off.
61 62 63 |
# File 'lib/view/formatter.rb', line 61 def self.skip_blank_formatter @skip_blank_formatter = true end |
+ (String) type
The type of the formatter, either set via .as or automatically deducted from the class name.
44 45 46 |
# File 'lib/view/formatter.rb', line 44 def self.type @type || name.split('::').last.underscore end |
Instance Method Details
- (Object) all_options
All options, unfiltered.
130 131 132 |
# File 'lib/view/formatter.rb', line 130 def @options end |
- (String) format
Subclass and override #format to implement your own formatter.
If you didn't specify a format instance method inside specific formatters, this will raise an error.
70 71 72 73 |
# File 'lib/view/formatter.rb', line 70 def format msg = "The '#{self.class.type}' formatter needs to implement the #format method." raise NotImplementedError.new(msg) end |
- (String) format!
The final result of the formatter, with the block captured if given.
If a template is given, use it to capture the block, for maximum integration with ActionView.
120 121 122 123 124 125 126 |
# File 'lib/view/formatter.rb', line 120 def format! if block captured_value else to_s end end |
- (Hash) options
The "safe" options that you can toss around to helper methods.
You can specify which methods are "safe" by white listing or black listing. White listing takes precedence over black listing.
To access options that are filtered out, use all_options. It's generally a good idea to black list options that you use inside your formatter.
The option :as is black listed by default.
108 109 110 111 112 |
# File 'lib/view/formatter.rb', line 108 def .merge().delete_if do |key, value| option_not_allowed?(key) end end |
- (Object) to_s
A hook for formatters to override so they can inject code between above the formatting, without all their inherited classes knowing about it.
136 137 138 |
# File 'lib/view/formatter.rb', line 136 def to_s format end |