Class: Lazier::Settings
- Inherits:
-
Object
- Object
- Lazier::Settings
- Defined in:
- lib/lazier/settings.rb
Overview
Settings for the extensions.
Instance Attribute Summary collapse
-
#boolean_names ⇒ Hash
readonly
String representations of booleans.
-
#date_formats ⇒ Hash
readonly
Custom date and time formats.
-
#date_names ⇒ Hash
readonly
String representations of days and months.
-
#format_number ⇒ Hash
readonly
Settings for numbers formatting.
-
#i18n ⇒ R18n::Translation
readonly
The translation object.
Class Method Summary collapse
-
.instance(force = false) ⇒ Settings
Returns the singleton instance of the settings.
Instance Method Summary collapse
-
#initialize ⇒ Settings
constructor
Initializes a new settings object.
-
#setup ⇒ Object
Setups the current instance.
-
#setup_boolean_names(true_name: nil, false_name: nil) ⇒ Hash
Setups strings representation of booleans.
-
#setup_date_formats(formats = nil, replace = false) ⇒ Hash
Setups custom formats for dates and times.
-
#setup_date_names(long_months: nil, short_months: nil, long_days: nil, short_days: nil) ⇒ Hash
Setups strings representation of days and months.
-
#setup_format_number(precision: 2, decimal_separator: ".", add_string: nil, k_separator: ",") ⇒ Hash
Setups formatters for a number.
Constructor Details
Instance Attribute Details
#boolean_names ⇒ Hash (readonly)
Returns String representations of booleans.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lazier/settings.rb', line 19 class Settings attr_reader :format_number, :boolean_names, :date_names, :date_formats, :i18n # 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. def self.instance(force = false) @instance = nil if force @instance ||= new end # Initializes a new settings object. def initialize Lazier.load_datetime @i18n = Lazier::I18n.instance setup end # Setups the current instance. def setup setup_format_number setup_boolean_names setup_date_formats setup_date_names end # 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. 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 # 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. 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 # 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. 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 # 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. 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 private # :nodoc: def prepare_definitions definitions = i18n.translate("date") definitions.extend(Lazier::Hash) definitions.ensure_access(:dotted) end end |
#date_formats ⇒ Hash (readonly)
Returns Custom date and time formats.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lazier/settings.rb', line 19 class Settings attr_reader :format_number, :boolean_names, :date_names, :date_formats, :i18n # 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. def self.instance(force = false) @instance = nil if force @instance ||= new end # Initializes a new settings object. def initialize Lazier.load_datetime @i18n = Lazier::I18n.instance setup end # Setups the current instance. def setup setup_format_number setup_boolean_names setup_date_formats setup_date_names end # 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. 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 # 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. 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 # 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. 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 # 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. 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 private # :nodoc: def prepare_definitions definitions = i18n.translate("date") definitions.extend(Lazier::Hash) definitions.ensure_access(:dotted) end end |
#date_names ⇒ Hash (readonly)
Returns String representations of days and months.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lazier/settings.rb', line 19 class Settings attr_reader :format_number, :boolean_names, :date_names, :date_formats, :i18n # 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. def self.instance(force = false) @instance = nil if force @instance ||= new end # Initializes a new settings object. def initialize Lazier.load_datetime @i18n = Lazier::I18n.instance setup end # Setups the current instance. def setup setup_format_number setup_boolean_names setup_date_formats setup_date_names end # 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. 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 # 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. 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 # 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. 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 # 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. 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 private # :nodoc: def prepare_definitions definitions = i18n.translate("date") definitions.extend(Lazier::Hash) definitions.ensure_access(:dotted) end end |
#format_number ⇒ Hash (readonly)
Returns Settings for numbers formatting.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lazier/settings.rb', line 19 class Settings attr_reader :format_number, :boolean_names, :date_names, :date_formats, :i18n # 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. def self.instance(force = false) @instance = nil if force @instance ||= new end # Initializes a new settings object. def initialize Lazier.load_datetime @i18n = Lazier::I18n.instance setup end # Setups the current instance. def setup setup_format_number setup_boolean_names setup_date_formats setup_date_names end # 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. 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 # 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. 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 # 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. 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 # 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. 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 private # :nodoc: def prepare_definitions definitions = i18n.translate("date") definitions.extend(Lazier::Hash) definitions.ensure_access(:dotted) end end |
#i18n ⇒ R18n::Translation (readonly)
Returns The translation object.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lazier/settings.rb', line 19 class Settings attr_reader :format_number, :boolean_names, :date_names, :date_formats, :i18n # 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. def self.instance(force = false) @instance = nil if force @instance ||= new end # Initializes a new settings object. def initialize Lazier.load_datetime @i18n = Lazier::I18n.instance setup end # Setups the current instance. def setup setup_format_number setup_boolean_names setup_date_formats setup_date_names end # 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. 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 # 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. 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 # 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. 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 # 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. 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 private # :nodoc: def prepare_definitions definitions = i18n.translate("date") definitions.extend(Lazier::Hash) definitions.ensure_access(:dotted) end end |
Class Method Details
.instance(force = false) ⇒ Settings
Returns the singleton instance of the settings.
26 27 28 29 |
# File 'lib/lazier/settings.rb', line 26 def self.instance(force = false) @instance = nil if force @instance ||= new end |
Instance Method Details
#setup ⇒ Object
Setups the current instance.
39 40 41 42 43 44 |
# 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) ⇒ Hash
Setups strings representation of booleans.
66 67 68 69 |
# 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) ⇒ Hash
Setups custom formats for dates and times.
77 78 79 80 81 82 83 84 85 |
# 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) ⇒ Hash
Setups strings representation of days and months.
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# 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: ",") ⇒ Hash
Setups formatters for a number.
54 55 56 57 58 |
# 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 |