Sha256: 552b930beff936cf19821c04e6dfc2bf2c0a926af6631a4d7476bdec1c032560

Contents?: true

Size: 1.06 KB

Versions: 8

Compression:

Stored size: 1.06 KB

Contents

module HumanAttributes
  class FormattersBuilder
    include HumanAttributes::Config

    def initialize(attributes, config)
      raise_error('InvalidHumanizeConfig') unless config.is_a?(Hash)
      @attributes = attributes
      @config = config
    end

    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

    def formatter_class(type)
      category = category_by_type(type)
      "HumanAttributes::Formatters::#{category.to_s.classify}".constantize
    end

    def get_types(options)
      size = options.keys.count
      raise_error('RequiredAttributeType') if size.zero?
      options.keys
    end

    def get_options(options)
      return {} if options == true
      raise_error('InvalidAttributeOptions') unless options.is_a?(Hash)
      options
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
human_attributes-0.6.0 lib/human_attributes/formatters_builder.rb
human_attributes-0.5.0 lib/human_attributes/formatters_builder.rb
human_attributes-0.4.0 lib/human_attributes/formatters_builder.rb
human_attributes-0.3.1 lib/human_attributes/formatters_builder.rb
human_attributes-0.3.0 lib/human_attributes/formatters_builder.rb
human_attributes-0.2.0 lib/human_attributes/formatters_builder.rb
human_attributes-0.1.1 lib/human_attributes/formatters_builder.rb
human_attributes-0.1.0 lib/human_attributes/formatters_builder.rb