Sha256: 474d7e6d6b6d6df8e331e49c34f880fe6f8a9268b1f4b3acbb7d3391391f703a

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

module HumanAttributes
  module Config
    TYPES = [
      {
        name: :currency,
        category: :numeric,
        formatter: :number_to_currency,
        suffix: :to_currency
      },
      {
        name: :number,
        category: :numeric,
        formatter: :number_to_human,
        suffix: :to_human
      },
      {
        name: :size,
        category: :numeric,
        formatter: :number_to_human_size,
        suffix: :to_human_size
      },
      {
        name: :percentage,
        category: :numeric,
        formatter: :number_to_percentage,
        suffix: :to_percentage
      },
      {
        name: :phone,
        category: :numeric,
        formatter: :number_to_phone,
        suffix: :to_phone
      },
      {
        name: :delimiter,
        category: :numeric,
        formatter: :number_with_delimiter,
        suffix: :with_delimiter
      },
      {
        name: :precision,
        category: :numeric,
        formatter: :number_with_precision,
        suffix: :with_precision
      },
      {
        name: :date,
        category: :date,
        suffix: :to_human_date
      },
      {
        name: :boolean,
        category: :boolean,
        suffix: :to_human_boolean
      },
      {
        name: :enumerize,
        category: :enumerize,
        suffix: :to_human_enum
      },
      {
        name: :custom,
        category: :custom,
        suffix: :to_custom_value
      }
    ]

    def category_by_type(type)
      type_config(type)[:category]
    end

    def formatter_by_type(type)
      type_config(type)[:formatter]
    end

    def suffix_by_type(type)
      type_config(type)[:suffix]
    end

    def known_type?(type)
      !!type_config(type)
    end

    def type_config(type)
      TYPES.select { |t| t[:name] == type }.first
    end

    def raise_error(error_class)
      raise "HumanAttributes::Error::#{error_class}".constantize.new
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
human_attributes-0.1.1 lib/human_attributes/config.rb
human_attributes-0.1.0 lib/human_attributes/config.rb