module Chic::Formats::ClassMethods

Private Instance Methods

_define_formats_method(attribute, options) click to toggle source
# File lib/chic/formats.rb, line 25
def _define_formats_method(attribute, options)
  define_method attribute do
    value = _formats_options_value(attribute, options)
    format(value, with: options[:with], **(options[:options] || {}))
  end
end
_raise_formats_options_not_valid(message, attributes) click to toggle source
# File lib/chic/formats.rb, line 63
def _raise_formats_options_not_valid(message, attributes)
  raise FormatsOptionsNotValid,
        "#{name} `formats` [#{attributes.map { |a| ":#{a}" }.join(', ')}]: #{message}"
end
_validate_formats_options!(attributes, options) click to toggle source
# File lib/chic/formats.rb, line 32
def _validate_formats_options!(attributes, options)
  return if options.nil?

  _raise_formats_options_not_valid 'options must be a hash', attributes \
    unless options.is_a?(Hash)

  _validate_formats_options_with!(attributes, options)
  _validate_formats_options_value!(attributes, options)
  _validate_formats_options_options!(attributes, options)
end
_validate_formats_options_options!(attributes, options) click to toggle source
# File lib/chic/formats.rb, line 58
def _validate_formats_options_options!(attributes, options)
  _raise_formats_options_not_valid '`options` must be a hash', attributes \
    if options.key?(:options) && !options[:options].is_a?(Hash)
end
_validate_formats_options_value!(attributes, options) click to toggle source

rubocop: enable Metrics/AbcSize

# File lib/chic/formats.rb, line 53
def _validate_formats_options_value!(attributes, options)
  _raise_formats_options_not_valid '`value` must be a symbol or a lambda', attributes \
    if options.key?(:value) && !options[:value].is_a?(Symbol) && !options[:value].is_a?(Proc)
end
_validate_formats_options_with!(attributes, options) click to toggle source

rubocop: disable Metrics/AbcSize

# File lib/chic/formats.rb, line 44
def _validate_formats_options_with!(attributes, options)
  _raise_formats_options_not_valid '`with` must be a symbol or a class', attributes \
    if options.key?(:with) && !options[:with].is_a?(Symbol) && !options[:with].is_a?(Class)

  _raise_formats_options_not_valid "`with` formatter :#{options[:with]} doesn't exist", attributes \
    if options.key?(:with) && options[:with].is_a?(Symbol) && !Chic.configuration.formatters.key?(options[:with])
end
format_options(options = {}) click to toggle source
# File lib/chic/formats.rb, line 14
def format_options(options = {})
  @format_options = options
end
formats(*attributes, **options) click to toggle source
# File lib/chic/formats.rb, line 18
def formats(*attributes, **options)
  _validate_formats_options!(attributes, options)
  attributes.each do |attribute|
    _define_formats_method(attribute, options)
  end
end