Module: HumanAttributes::Config

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

Constant Summary collapse

TYPES =
[
  [:currency, :numeric, :to_currency, :number_to_currency],
  [:number, :numeric, :to_human, :number_to_human],
  [:size, :numeric, :to_human_size, :number_to_human_size],
  [:percentage, :numeric, :to_percentage, :number_to_percentage],
  [:phone, :numeric, :to_phone, :number_to_phone],
  [:delimiter, :numeric, :with_delimiter, :number_with_delimiter],
  [:precision, :numeric, :with_precision, :number_with_precision],
  [:date, :date, :to_human_date],
  [:datetime, :datetime, :to_human_datetime],
  [:boolean, :boolean, :to_human_boolean],
  [:enumerize, :enumerize, :to_human_enum],
  [:enum, :enum, :to_human_enum],
  [:custom, :custom, :to_custom_value]
].map { |config_type| build_type(*config_type) }
OPTIONS =
{
  boolean: [
    { boolean: true }
  ],
  date: [
    { date: true },
    { date: { format: :short, suffix: "to_short_date" } },
    { date: { format: :short, suffix: "to_long_date" } },
    { datetime: true },
    { datetime: { format: :short, suffix: "to_short_datetime" } },
    { datetime: { format: :short, suffix: "to_long_datetime" } }
  ],
  datetime: [
    { datetime: true },
    { datetime: { format: :short, suffix: "to_short_datetime" } },
    { datetime: { format: :short, suffix: "to_long_datetime" } }
  ],
  decimal: [{ delimiter: true }],
  enum: [{ enum: true }],
  float: [{ delimiter: true }],
  id: [{ custom: { formatter: ->(_o, value) { "#{_o.model_name.human}: ##{value}" } } }],
  integer: [{ delimiter: true }]
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_type(name, category, suffix, formatter = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/human_attributes/config.rb', line 3

def self.build_type(name, category, suffix, formatter = nil)
  config_type = {
    name: name,
    category: category,
    suffix: suffix
  }
  if formatter
    config_type[:formatter] = formatter
  end
  config_type
end

Instance Method Details

#category_by_type(type) ⇒ Object



55
56
57
# File 'lib/human_attributes/config.rb', line 55

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

#formatter_by_type(type) ⇒ Object



59
60
61
# File 'lib/human_attributes/config.rb', line 59

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

#known_type?(type) ⇒ Boolean

Returns:



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

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

#raise_error(error_class) ⇒ Object

Raises:



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

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

#suffix_by_type(type) ⇒ Object



63
64
65
# File 'lib/human_attributes/config.rb', line 63

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

#type_config(type) ⇒ Object



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

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