Class: Cowtech::Extensions::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/cowtech-extensions/settings.rb

Overview

Settings for the extensions.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSettings

Initializes a new settings object.



31
32
33
34
35
36
# File 'lib/cowtech-extensions/settings.rb', line 31

def initialize
  self.setup_format_number
  self.setup_boolean_names
  self.setup_date_formats
  self.setup_date_names
end

Instance Attribute Details

#boolean_namesObject (readonly)

String representations of booleans.



15
16
17
# File 'lib/cowtech-extensions/settings.rb', line 15

def boolean_names
  @boolean_names
end

#date_formatsObject (readonly)

Custom date and time formats.



21
22
23
# File 'lib/cowtech-extensions/settings.rb', line 21

def date_formats
  @date_formats
end

#date_namesObject (readonly)

String representations of days and months.



18
19
20
# File 'lib/cowtech-extensions/settings.rb', line 18

def date_names
  @date_names
end

#format_numberObject (readonly)

Settings for numbers formatting.



12
13
14
# File 'lib/cowtech-extensions/settings.rb', line 12

def format_number
  @format_number
end

Class Method Details

.instanceSettings

Returns the singleton instance of the settings.

Returns:

  • (Settings)

    The singleton instance of the settings.



26
27
28
# File 'lib/cowtech-extensions/settings.rb', line 26

def self.instance
  @instance ||= self.new
end

Instance Method Details

#setup_boolean_names(true_name = nil, false_name = nil) ⇒ Hash

Setups strings representation of booleans.

Parameters:

  • true_name (String) (defaults to: nil)

    The string representation of true. Defaults to Yes.

  • false_name (String) (defaults to: nil)

    The string representation of false. Defaults to No.

Returns:

  • (Hash)

    The new representations.

See Also:



61
62
63
64
65
# File 'lib/cowtech-extensions/settings.rb', line 61

def setup_boolean_names(true_name = nil, false_name = nil)
  true_name ||= "Yes"
  false_name ||= "No"
  @boolean_names = {true => true_name, false => false_name}
end

#setup_date_formats(formats = nil, replace = false) ⇒ Hash

Setups custom formats for dates and times.

Parameters:

  • formats (Hash) (defaults to: nil)

    The format to add or replace.

  • replace (Boolean) (defaults to: false)

    If to discard current formats.

Returns:

  • (Hash)

    The new formats.

See Also:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cowtech-extensions/settings.rb', line 73

def setup_date_formats(formats = nil, replace = false)
  formats = {
      :ct_date => "%Y-%m-%d",
      :ct_time => "%H:%M:%S",
      :ct_date_time => "%F %T",
      :ct_iso_8601 => "%FT%T%z"
  } if formats.blank?

  if formats.is_a?(::Hash) then
    if !replace then
      @date_formats ||= {}
      @date_formats.merge!(formats)
    else
      @date_formats = formats
    end

    @date_formats.each_pair do |k, v| ::Time::DATE_FORMATS[k] = v end
  end

  @date_formats
end

#setup_date_names(long_months = nil, short_months = nil, long_days = nil, short_days = nil) ⇒ Hash

Setups strings representation of days and months.

Parameters:

  • long_months (Array) (defaults to: nil)

    The string representation of months.

  • short_months (Array) (defaults to: nil)

    The abbreviated string representation of months.

  • long_days (Array) (defaults to: nil)

    The string representation of days.

  • short_days (Array) (defaults to: nil)

    The abbreviated string representation of days.

Returns:

  • (Hash)

    The new representations.

See Also:



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cowtech-extensions/settings.rb', line 105

def setup_date_names(long_months = nil, short_months = nil, long_days = nil, short_days = nil)
  long_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] if long_months.blank?
  short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] if short_months.blank?
  long_days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]  if long_days.blank?
  short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]  if short_days.blank?

  @date_names = {
    :long_months => long_months,
    :short_months => short_months,
    :long_days => long_days,
    :short_days => short_days
  }
end

#setup_format_number(prec = 2, decimal_separator = ".", add_string = "", k_separator = ",") ⇒ Hash

Setups formatters for a number.

Parameters:

  • prec (Fixnum) (defaults to: 2)

    The precision to show.

  • decimal_separator (String) (defaults to: ".")

    The string to use as decimal separator.

  • add_string (String) (defaults to: "")

    The string to append to the number.

  • k_separator (String) (defaults to: ",")

    The string to use as thousands separator.

Returns:

  • (Hash)

    The new formatters.

See Also:



46
47
48
49
50
51
52
53
# File 'lib/cowtech-extensions/settings.rb', line 46

def setup_format_number(prec = 2, decimal_separator = ".", add_string = "", k_separator = ",")
  @format_number = {
    :prec => prec,
    :decimal_separator => decimal_separator,
    :add_string => add_string,
    :k_separator => k_separator
  }
end