Module: HumanAttributes::Config

Included in:
Formatters::Base, FormattersBuilder, MethodBuilder
Defined in:
lib/human_attributes/config.rb

Constant Summary collapse

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
  }
]

Instance Method Summary collapse

Instance Method Details

#category_by_type(type) ⇒ Object



68
69
70
# File 'lib/human_attributes/config.rb', line 68

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

#formatter_by_type(type) ⇒ Object



72
73
74
# File 'lib/human_attributes/config.rb', line 72

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

#known_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/human_attributes/config.rb', line 80

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

#raise_error(error_class) ⇒ Object

Raises:



88
89
90
# File 'lib/human_attributes/config.rb', line 88

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

#suffix_by_type(type) ⇒ Object



76
77
78
# File 'lib/human_attributes/config.rb', line 76

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

#type_config(type) ⇒ Object



84
85
86
# File 'lib/human_attributes/config.rb', line 84

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