Module: HasSetting::Formatters

Defined in:
lib/has_setting/formatters.rb

Defined Under Namespace

Classes: BooleanFormatter, BooleansFormatter, FloatFormatter, FloatsFormatter, IntFormatter, IntsFormatter, NilSafeFormatter, StrictBooleanFormatter, StrictBooleansFormatter, StringFormatter, StringsFormatter

Constant Summary collapse

@@formatters =
{}

Class Method Summary collapse

Class Method Details

.for_type(type) ⇒ Object

Lookup a Formatter by type symbol raises ArgumentError if the formatter is not found

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/has_setting/formatters.rb', line 17

def self.for_type(type)
  formatter = @@formatters[type]
  raise ArgumentError.new("Can not find a formatter for #{type}") unless formatter
  formatter
end

.register_formatter(type, formatter) ⇒ Object

register a formatter:

* type: a Symbol that is used to identify this formatter in the has_setting options hash via the :type option
* formatter: the formatter, an object which supports to_type and to_s methods


8
9
10
11
12
13
# File 'lib/has_setting/formatters.rb', line 8

def self.register_formatter(type, formatter)
  if !formatter.respond_to?(:to_s) || !formatter.respond_to?(:to_type)
    raise ArgumentError.new('Formatter does not support to_s/to_type')
  end
  @@formatters[type] = formatter
end