module Chic::Formats

Public Class Methods

included(parent) click to toggle source
# File lib/chic/formats.rb, line 6
def included(parent)
  parent.extend ClassMethods
end

Private Instance Methods

_formats_options_value(attribute, options) click to toggle source

rubocop: enable Metrics/MethodLength

# File lib/chic/formats.rb, line 88
def _formats_options_value(attribute, options)
  return object&.public_send(attribute) unless options.is_a?(Hash) && options.key?(:value)

  case options[:value]
  when Symbol
    send(options[:value])
  when Proc
    instance_exec(&options[:value])
  else
    raise FormatsOptionsNotValid, "Options value for #{attribute} must be a symbol or a Proc"
  end
end
_formatter_class(with) click to toggle source
# File lib/chic/formats.rb, line 101
def _formatter_class(with)
  case with
  when Class
    with
  when Symbol
    _formatters[with] || raise(FormatsOptionsNotValid, "Formatter :#{with} is not supported")
  else
    _formatters[:nil]
  end
end
_formatters() click to toggle source

rubocop: disable Metrics/MethodLength

# File lib/chic/formats.rb, line 72
def _formatters
  @_formatters ||= Chic.configuration.formatters.reduce({}) do |result, (key, klass)|
    formatter = case klass
                when Class
                  klass
                when String
                  klass.constantize
                else
                  raise ConfigFormatterNotValid, "Configured formatter for #{key} must be a class or class-string"
                end

    result.merge(key => formatter)
  end
end
format(value, with: nil, **options) click to toggle source
# File lib/chic/formats.rb, line 112
def format(value, with: nil, **options)
  _formatter_class(with).new(value).tap do |formatter|
    options.each do |option, argument|
      formatter.public_send(option, argument) if formatter.respond_to?(option)
    end
  end
end