Sha256: 6c23f2902d2ce9d11359a799c2dad2370af4696f9eb26e09fed94cfa4abd7107

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 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: :datetime,
        category: :datetime,
        suffix: :to_human_datetime
      },
      {
        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

7 entries across 7 versions & 1 rubygems

Version Path
human_attributes-0.7.0 lib/human_attributes/config.rb
human_attributes-0.6.0 lib/human_attributes/config.rb
human_attributes-0.5.0 lib/human_attributes/config.rb
human_attributes-0.4.0 lib/human_attributes/config.rb
human_attributes-0.3.1 lib/human_attributes/config.rb
human_attributes-0.3.0 lib/human_attributes/config.rb
human_attributes-0.2.0 lib/human_attributes/config.rb