class Lazier::Settings

Settings for the extensions.

@attribute [r] format_number

@return [Hash] Settings for numbers formatting.

@attribute [r] boolean_names

@return [Hash] String representations of booleans.

@attribute [r] date_names

@return [Hash] String representations of days and months.

@attribute [r] date_formats

@return [Hash] Custom date and time formats.

@attribute [r] i18n

@return [R18n::Translation] The translation object.

Attributes

boolean_names[R]
date_formats[R]
date_names[R]
format_number[R]
i18n[R]

Public Class Methods

instance(force = false) click to toggle source

Returns the singleton instance of the settings.

@param force [Boolean] Whether to force recreation of the instance. @return [Settings] The singleton instance of the settings.

# File lib/lazier/settings.rb, line 26
def self.instance(force = false)
  @instance = nil if force
  @instance ||= new
end
new() click to toggle source

Initializes a new settings object.

# File lib/lazier/settings.rb, line 32
def initialize
  Lazier.load_datetime
  @i18n = Lazier::I18n.instance
  setup
end

Public Instance Methods

setup() click to toggle source

Setups the current instance.

# File lib/lazier/settings.rb, line 39
def setup
  setup_format_number
  setup_boolean_names
  setup_date_formats
  setup_date_names
end
setup_boolean_names(true_name: nil, false_name: nil) click to toggle source

Setups strings representation of booleans. @see Object#format_boolean

@param true_name [String|NilClass] The string representation of `true`. Defaults to `Yes`. @param false_name [String|NilClass] The string representation of `false`. Defaults to `No`. @return [Hash] The new representations.

# File lib/lazier/settings.rb, line 66
def setup_boolean_names(true_name: nil, false_name: nil)
  names = i18n.translate("boolean")
  @boolean_names = {true => true_name || names[0], false => false_name || names[1]}
end
setup_date_formats(formats = nil, replace = false) click to toggle source

Setups custom formats for dates and times. @see DateTime#lstrftime

@param formats [Hash|NilClass] The format to add or replace. @param replace [Boolean] Whether to discard current formats. @return [Hash] The new formats.

# File lib/lazier/settings.rb, line 77
def setup_date_formats(formats = nil, replace = false)
  @date_formats = HashWithIndifferentAccess.new if replace || !@date_formats

  formats = {ct_date: "%Y-%m-%d", ct_time: "%H:%M:%S", ct_date_time: "%F %T", ct_iso_8601: "%FT%T%z" } unless formats.is_a?(::Hash)
  @date_formats.merge!(formats)
  ::Time::DATE_FORMATS.merge!(@date_formats)

  @date_formats
end
setup_date_names(long_months: nil, short_months: nil, long_days: nil, short_days: nil) click to toggle source

Setups strings representation of days and months. @see DateTime::ClassMethods#days @see DateTime::ClassMethods#months @see Lazier::DateTime#format

@param long_months [Array|NilClass] The string representation of months. @param short_months [Array|NilClass] The abbreviated string representation of months. @param long_days [Array|NilClass] The string representation of days. @param short_days [Array|NilClass] The abbreviated string representation of days. @return [Hash] The new representations.

# File lib/lazier/settings.rb, line 97
def setup_date_names(long_months: nil, short_months: nil, long_days: nil, short_days: nil)
  definitions = prepare_definitions

  @date_names = {
    long_months: long_months.ensure(definitions.long_months),
    short_months: short_months.ensure(definitions.short_months),
    long_days: long_days.ensure(definitions.long_days),
    short_days: short_days.ensure(definitions.short_days)
  }

  @date_names.extend(Lazier::Hash)
  @date_names = @date_names.ensure_access(:dotted, :indifferent)
end
setup_format_number(precision: 2, decimal_separator: ".", add_string: nil, k_separator: ",") click to toggle source

Setups formatters for a number. @see Object#format_number

@param precision [Fixnum] The precision to show. @param decimal_separator [String] The string to use as decimal separator. @param add_string [String] The string to append to the number. @param k_separator [String] The string to use as thousands separator. @return [Hash] The new formatters.

# File lib/lazier/settings.rb, line 54
def setup_format_number(precision: 2, decimal_separator: ".", add_string: nil, k_separator: ",")
  @format_number = ::HashWithIndifferentAccess.new(
    precision: precision, decimal_separator: decimal_separator, add_string: add_string, k_separator: k_separator
  )
end