class HumanAttributes::FormattersBuilder

Public Class Methods

new(attributes, config) click to toggle source
# File lib/human_attributes/formatters_builder.rb, line 5
def initialize(attributes, config)
  raise_error('InvalidHumanizeConfig') unless config.is_a?(Hash)
  @attributes = attributes
  @config = config
end

Public Instance Methods

build() click to toggle source
# File lib/human_attributes/formatters_builder.rb, line 11
def build
  formatters = []

  get_types(@config).each do |type|
    raise_error('InvalidType') unless known_type?(type)
    options = get_options(@config[type])
    formatters << @attributes.map do |attribute|
      formatter_class(type).new(attribute, type, options)
    end
  end

  formatters.flatten
end

Private Instance Methods

formatter_class(type) click to toggle source
# File lib/human_attributes/formatters_builder.rb, line 27
def formatter_class(type)
  category = category_by_type(type)
  "HumanAttributes::Formatters::#{category.to_s.classify}".constantize
end
get_options(options) click to toggle source
# File lib/human_attributes/formatters_builder.rb, line 38
def get_options(options)
  return {} if options == true

  raise_error('InvalidAttributeOptions') unless options.is_a?(Hash)
  options
end
get_types(options) click to toggle source
# File lib/human_attributes/formatters_builder.rb, line 32
def get_types(options)
  size = options.keys.count
  raise_error('RequiredAttributeType') if size.zero?
  options.keys
end