lazier 2.8.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/Gemfile +3 -3
  2. data/doc/Lazier.html +69 -99
  3. data/doc/Lazier/Boolean.html +6 -6
  4. data/doc/Lazier/DateTime.html +35 -39
  5. data/doc/Lazier/DateTime/ClassMethods.html +59 -85
  6. data/doc/Lazier/Exceptions.html +6 -6
  7. data/doc/Lazier/Exceptions/Debug.html +133 -0
  8. data/doc/Lazier/Exceptions/MissingTranslation.html +8 -8
  9. data/doc/Lazier/Hash.html +5 -270
  10. data/doc/Lazier/I18n.html +68 -26
  11. data/doc/Lazier/Localizer.html +17 -21
  12. data/doc/Lazier/Math.html +5 -5
  13. data/doc/Lazier/Math/ClassMethods.html +5 -5
  14. data/doc/Lazier/Object.html +681 -218
  15. data/doc/Lazier/Pathname.html +7 -11
  16. data/doc/Lazier/Settings.html +166 -262
  17. data/doc/Lazier/String.html +21 -25
  18. data/doc/Lazier/TimeZone.html +119 -130
  19. data/doc/Lazier/TimeZone/ClassMethods.html +75 -83
  20. data/doc/Lazier/Version.html +8 -8
  21. data/doc/_index.html +7 -7
  22. data/doc/class_list.html +2 -2
  23. data/doc/file.README.html +5 -5
  24. data/doc/file_list.html +1 -1
  25. data/doc/frames.html +1 -1
  26. data/doc/index.html +5 -5
  27. data/doc/js/full_list.js +6 -1
  28. data/doc/method_list.html +134 -324
  29. data/doc/top-level-namespace.html +5 -5
  30. data/lazier.gemspec +4 -3
  31. data/lib/lazier.rb +9 -23
  32. data/lib/lazier/boolean.rb +1 -1
  33. data/lib/lazier/datetime.rb +47 -79
  34. data/lib/lazier/exceptions.rb +4 -4
  35. data/lib/lazier/hash.rb +0 -24
  36. data/lib/lazier/i18n.rb +13 -6
  37. data/lib/lazier/localizer.rb +3 -5
  38. data/lib/lazier/object.rb +107 -78
  39. data/lib/lazier/pathname.rb +1 -3
  40. data/lib/lazier/settings.rb +22 -30
  41. data/lib/lazier/string.rb +3 -5
  42. data/lib/lazier/version.rb +3 -3
  43. data/spec/lazier/datetime_spec.rb +8 -8
  44. data/spec/lazier/hash_spec.rb +9 -10
  45. data/spec/lazier/object_spec.rb +85 -11
  46. data/spec/lazier/settings_spec.rb +10 -17
  47. data/spec/lazier_spec.rb +1 -1
  48. metadata +26 -9
@@ -7,18 +7,18 @@
7
7
  module Lazier
8
8
  # Exceptions for lazier.
9
9
  module Exceptions
10
- # This exception is raised from {Object#debug_dump} when `must_raise` is `true`.
11
- class Dump < ::StandardError
10
+ # This exception is raised to debug code.
11
+ class Debug < ::StandardError
12
12
  end
13
13
 
14
14
  # This exception is raised from {I18n I18n} if no valid translation are found in the specified path.
15
- class MissingTranslation < Exception
15
+ class MissingTranslation < StandardError
16
16
  # Creates a new missing translation exception.
17
17
  #
18
18
  # @param locales [Array] The locales that was requested to load.
19
19
  # @param path [String] The path where was request to search for translations.
20
20
  def initialize(locales, path)
21
- super("Unable to load any of the following translation in #{path}: #{locales.to_json}.")
21
+ super("Unable to load any of the following translation in #{path}: #{locales.join(", ")}.")
22
22
  end
23
23
  end
24
24
  end
data/lib/lazier/hash.rb CHANGED
@@ -8,29 +8,5 @@ module Lazier
8
8
  # Extensions for Hash objects.
9
9
  module Hash
10
10
  extend ::ActiveSupport::Concern
11
-
12
- # This is called when the user access a member using dotted notation.
13
- #
14
- # @param method [String|Symbol] Key to search.
15
- # @param args [Array] *Unused.*
16
- # @param block [Proc] *Unused.*
17
- # @return [Object] The value for the key.
18
- def method_missing(method, *args, &block)
19
- if self.has_key?(method.to_sym) then
20
- self[method.to_sym]
21
- elsif self.has_key?(method.to_s) then
22
- self[method.to_s]
23
- else
24
- ::Hash.method_missing(method, *args, &block)
25
- end
26
- end
27
-
28
- # This is called when the user access a member using dotted notation.
29
- #
30
- # @param method [String|Symbol] Key to search.
31
- # @return [Boolean] `true` if the key exists, `false` otherwise.
32
- def respond_to?(method)
33
- (self.has_key?(method.to_sym) || self.has_key?(method.to_s)) ? true : ::Hash.respond_to?(method)
34
- end
35
11
  end
36
12
  end
data/lib/lazier/i18n.rb CHANGED
@@ -49,13 +49,11 @@ module Lazier
49
49
  # @param locale [Symbol] The new locale. Default is the current system locale.
50
50
  # @return [R18n::Translation] The new translation object.
51
51
  def i18n_load_locale(locale)
52
- path = (@i18n_locales_path || "").to_s
53
- locales = [locale, ENV["LANG"], R18n::I18n.system_locale].collect { |l| find_locale_in_path(l.to_s, path)}
54
- locales << "en" # Add English as a fallback
55
- locales = locales.uniq.compact
52
+ path = @i18n_locales_path.ensure_string
53
+ locales = validate_locales([locale], path)
56
54
 
57
55
  begin
58
- translation = R18n::I18n.new(locales, path).t.send((@i18n_root || "").to_s)
56
+ translation = R18n::I18n.new(locales, path).t.send(@i18n_root)
59
57
  raise ArgumentError if translation.is_a?(R18n::Untranslated)
60
58
  translation
61
59
  rescue
@@ -63,13 +61,22 @@ module Lazier
63
61
  end
64
62
  end
65
63
 
64
+ # Validates locales for messages.
65
+ #
66
+ # @param locales [Array] The list of locales to validate. English is added as fallback.
67
+ # @param path [String] The path where look into.
68
+ # @return [Array] The list of valid locales.
69
+ def validate_locales(locales, path)
70
+ (locales + [ENV["LANG"], R18n::I18n.system_locale, "en"]).select { |l| find_locale_in_path(l, path)}.uniq.collect(&:to_s)
71
+ end
72
+
66
73
  # Find a locale file in a path.
67
74
  #
68
75
  # @param locale [String] The locale to find.
69
76
  # @param path [String] The path where look into.
70
77
  # @return [String|nil] The version of the locale found or `nil`, if nothing was found.
71
78
  def find_locale_in_path(locale, path)
72
- [locale, locale[0, 5], locale[0, 2]].select {|l| File.exists?("#{path}/#{l}.yml") }.first
79
+ locale ? [locale, locale[0, 5], locale[0, 2]].select {|l| File.exists?("#{path}/#{l}.yml") }.first : nil
73
80
  end
74
81
  end
75
82
  end
@@ -15,9 +15,7 @@ module Lazier
15
15
  # @param path [String] The path where the translations are stored.
16
16
  # @param locale [String|Symbol] The locale to use for localization.
17
17
  def initialize(root = nil, path = nil, locale = nil)
18
- root ||= :lazier
19
- path ||= ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")
20
- self.i18n_setup(root, path)
18
+ i18n_setup(root || :lazier, path || ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
21
19
  self.i18n = locale
22
20
  end
23
21
 
@@ -27,7 +25,7 @@ module Lazier
27
25
  # @param args [Array] Optional arguments to localize the message.
28
26
  # @return [String|R18n::Untranslated] The localized message.
29
27
  def self.localize(message, *args)
30
- self.new.i18n.send(message, *args)
28
+ Lazier::Localizer.new.i18n.send(message, *args)
31
29
  end
32
30
 
33
31
  # Localize a message in a specified locale.
@@ -37,7 +35,7 @@ module Lazier
37
35
  # @param args [Array] Optional arguments to localize the message.
38
36
  # @return [String|R18n::Untranslated] The localized message.
39
37
  def self.localize_on_locale(locale, message, *args)
40
- self.new(nil, nil, locale).i18n.send(message, *args)
38
+ Lazier::Localizer.new(nil, nil, locale).i18n.send(message, *args)
41
39
  end
42
40
  end
43
41
  end
data/lib/lazier/object.rb CHANGED
@@ -10,128 +10,156 @@ module Lazier
10
10
  include ::ActionView::Helpers::NumberHelper
11
11
  extend ::ActiveSupport::Concern
12
12
 
13
+ # Expression to match a boolean value.
14
+ BOOLEAN_MATCHER = /^(\s*(1|0|true|false|yes|no|t|f|y|n)\s*)$/i
15
+
16
+ # Expression to match a true value.
17
+ BOOLEAN_TRUE_MATCHER = /^(\s*(1|true|yes|t|y)\s*)$/i
18
+
19
+ # Expression to match a integer value.
20
+ INTEGER_MATCHER = /^([+-]?)(\d+)$/
21
+
22
+ # Expression to match a float value.
23
+ FLOAT_MATCHER = /^([+-]?)(\d+)([.,]\d+)?$/
24
+
13
25
  # Normalizes a number for conversion. Basically this methods removes all separator and ensures that `.` is used for decimal separator.
14
26
  #
15
27
  # @return [String] The normalized number.
16
28
  def normalize_number
17
- if self.is_a?(TrueClass) then
18
- "1"
19
- elsif !self then
20
- "0"
21
- else
22
- rv = self.ensure_string.strip.split(/[\.,]/)
23
- rv[-1] = "." + rv[-1] if rv.length > 1
24
- rv.join("")
25
- end
26
- end
27
-
28
- # Checks if the object is a valid number.
29
- #
30
- # @return [Boolean] `true` is a valid number, `false` otherwise.
31
- def is_number?
32
- self.is_float?
29
+ is_boolean? ? to_i.to_s : ensure_string.strip.gsub(/[\.,](?=(.*[\.,]))/, "").gsub(",", ".")
33
30
  end
34
31
 
35
32
  # Checks if the object is a valid integer.
36
33
  #
37
34
  # @return [Boolean] `true` is a valid integer, `false` otherwise.
38
35
  def is_integer?
39
- self.is_a?(::Integer) || /^([+-]?)(\d+)$/.match(self.normalize_number)
36
+ is_a?(::Integer) || is_a?(::TrueClass) || !self || normalize_number =~ ::Lazier::Object::INTEGER_MATCHER
40
37
  end
41
38
 
42
39
  # Checks if the object is a valid float.
43
40
  #
44
41
  # @return [Boolean] `true` is a valid float, `false` otherwise.
45
42
  def is_float?
46
- self.is_a?(::Float) || /^([+-]?)(\d+)([.,]\d+)?$/.match(self.normalize_number)
43
+ is_a?(::Numeric) || is_a?(::TrueClass) || !self || normalize_number =~ ::Lazier::Object::FLOAT_MATCHER
47
44
  end
45
+ alias :is_number? :is_float?
48
46
 
49
47
  # Checks if the object is a valid boolean value.
50
48
  #
51
49
  # @return [Boolean] `true` is a valid boolean value, `false` otherwise.
52
50
  def is_boolean?
53
- self.is_a?(::TrueClass) || self.is_a?(::FalseClass) || self.is_a?(::NilClass) || (self.ensure_string.strip =~ /^(1|0|true|false|yes|no|t|f|y|n)$/i)
51
+ is_a?(::TrueClass) || !self || to_s =~ ::Lazier::Object::BOOLEAN_MATCHER
52
+ end
53
+
54
+ # Makes sure that the object is set to something meaningful.
55
+ #
56
+ # @param default_value [String] The default value to return if the `verifier` or the block returns true.
57
+ # @param verifier [Symbol] The method used to verify if the object is NOT meaningful. *Ignored if a block is passed.*
58
+ # @return [String] The current object or the `default_value`.
59
+ def ensure(default_value, verifier = :blank?)
60
+ valid = block_given? ? yield(self) : send(verifier)
61
+ !valid ? self : default_value
62
+ end
63
+
64
+ # Makes sure that the object is a string.
65
+ #
66
+ # @param default_value [String] The default value to return if the object is `nil`. It is also passed to the block stringifier.
67
+ # @param stringifier [Symbol] The method used to convert the object to a string. *Ignored if a block is passed.*
68
+ # @return [String] The string representation of the object.
69
+ def ensure_string(default_value = "", stringifier = :to_s)
70
+ !nil? ? (block_given? ? yield(self, default_value) : send(stringifier)) : default_value
54
71
  end
55
72
 
56
73
  # Makes sure that the object is an array. For non array objects, return a single element array containing the object.
57
74
  #
75
+ # @param default_value [Array|NilClass] The default array to use. If not specified, an array containing the object is returned.
76
+ # @param uniq [Boolean] If to remove duplicates from the array before sanitizing.
77
+ # @param compact [Boolean] If to compact the array before sanitizing.
78
+ # @param sanitizer [Symbol|nil] If not `nil`, the method to use to sanitize entries of the array. *Ignored if a block is present.*
79
+ # @param block [Proc] A block to sanitize entries. It must accept the value as unique argument.
58
80
  # @return [Array] If the object is an array, then the object itself, a single element array containing the object otherwise.
59
- def ensure_array
60
- self.is_a?(::Array) ? self : [self]
81
+ def ensure_array(default_value = nil, uniq = false, compact = false, sanitizer = nil, &block)
82
+ rv = is_a?(::Array) ? self : (default_value || [self])
83
+ rv.collect!(&(block || sanitizer))
84
+ rv.uniq! if uniq
85
+ rv.compact! if compact
86
+ rv
61
87
  end
62
88
 
63
- # Makes sure that the object is a string. For `nil`, it returns "".
89
+ # Makes sure that the object is an hash. For non hash objects, return an hash basing on the `default_value` parameter.
64
90
  #
65
- # @return [String] The string representation of the object.
66
- def ensure_string
67
- if self.is_a?(::String) then
91
+ # @param default_value [Hash|Object|NilClass] The default value to use. If it is an `Hash`, it is returned as value otherwise it is used to build as a key to build an hash with the current object as only value (everything but strings and symbols are mapped to `key`).
92
+ # @param sanitizer [Symbol|nil] If not `nil`, the method to use to sanitize values of the hash. *Ignored if a block is present.*
93
+ # @return [Hash] If the object is an hash, then the object itself, a hash with the object as single value otherwise.
94
+ def ensure_hash(default_value = nil, sanitizer = nil)
95
+ rv = if is_a?(::Hash) then
68
96
  self
97
+ elsif default_value.is_a?(::Hash) then
98
+ default_value
69
99
  else
70
- self.present? ? self.to_s : ""
100
+ key = default_value.is_a?(::String) || default_value.is_a?(::Symbol) ? default_value : :key
101
+ {key => self}
71
102
  end
72
- end
73
103
 
74
- # Converts the object to a float.
75
- #
76
- # @param default_value [Float] The value to return if the conversion is not possible.
77
- # @return [Float] The float representation of the object.
78
- def to_float(default_value = 0.0)
79
- if self.is_a?(::Float)
80
- self
81
- elsif self.is_a?(::Integer)
82
- self.to_f
104
+ if block_given? || sanitizer then
105
+ rv.inject({}) {|h, (k, v)|
106
+ h[k] = block_given? ? yield(v) : v.send(sanitizer)
107
+ h
108
+ }
83
109
  else
84
- self.is_float? ? ::Kernel.Float(self.normalize_number) : default_value
110
+ rv
85
111
  end
86
112
  end
87
113
 
114
+ # Converts the object to a boolean.
115
+ #
116
+ # @return [Boolean] The boolean representation of the object.
117
+ def to_boolean
118
+ is_a?(TrueClass) || self == 1.0 || self == 1 || ensure_string =~ ::Lazier::Object::BOOLEAN_TRUE_MATCHER
119
+ end
120
+
88
121
  # Converts the object to a integer.
89
122
  #
90
123
  # @param default_value [Fixnum] The value to return if the conversion is not possible.
91
124
  # @return [Fixnum] The integer representation of the object.
92
125
  def to_integer(default_value = 0)
93
- self.is_a?(::Integer) ? self : self.to_float(default_value).to_i
126
+ to_float(default_value).to_i
94
127
  end
95
128
 
96
- # Converts the object to a boolean.
129
+ # Converts the object to a float.
97
130
  #
98
- # @return [Boolean] The boolean representation of the object.
99
- def to_boolean
100
- rv = self
101
- rv = rv.to_i if rv.is_a?(::Float)
102
- (rv.is_a?(TrueClass) || /^(1|on|true|yes|t|y)$/i.match(rv.ensure_string.strip)) ? true : false
131
+ # @param default_value [Float] The value to return if the conversion is not possible.
132
+ # @return [Float] The float representation of the object.
133
+ def to_float(default_value = 0.0)
134
+ is_float? ? ::Kernel.Float(is_a?(::Numeric) ? self : normalize_number) : default_value
103
135
  end
104
136
 
105
137
  # Returns the rounded float representaton of the object.
106
138
  #
107
- # @param prec [Fixnum] The precision to keep.
139
+ # @param precision [Fixnum] The precision to keep.
108
140
  # @return [Float] The rounded float representaton of the object.
109
- def round_to_precision(prec = 2)
110
- (self.is_number? && prec >= 0) ? number_with_precision(self, precision: prec) : nil
141
+ def round_to_precision(precision = 2)
142
+ is_number? ? number_with_precision(to_float, precision: [precision, 0].max) : nil
111
143
  end
112
144
 
113
145
  # Formats a number.
114
146
  # @see Settings#setup_format_number
115
147
  #
116
- # @param prec [Fixnum] The precision to show.
148
+ # @param precision [Fixnum] The precision to show.
117
149
  # @param decimal_separator [String] The string to use as decimal separator.
118
150
  # @param add_string [String] The string to append to the number.
119
151
  # @param k_separator [String] The string to use as thousands separator.
120
152
  # @return [String] The string representation of the object.
121
- def format_number(prec = nil, decimal_separator = nil, add_string = nil, k_separator = nil)
122
- prec ||= ::Lazier.settings.format_number[:prec]
123
- decimal_separator ||= ::Lazier.settings.format_number[:decimal_separator]
124
- add_string ||= ::Lazier.settings.format_number[:add_string]
125
- k_separator ||= ::Lazier.settings.format_number[:k_separator]
126
- format = "%n"
127
- unit = ""
128
-
129
- if add_string.present? then
130
- format = "%n %u"
131
- unit = add_string
132
- end
153
+ def format_number(precision = nil, decimal_separator = nil, add_string = nil, k_separator = nil)
154
+ if is_number? then
155
+ settings = ::Lazier.settings.format_number
156
+ add_string ||= settings[:add_string]
157
+ format, unit = (add_string ? ["%n %u", add_string] : ["%n", ""])
133
158
 
134
- (self.is_number? && prec >= 0) ? number_to_currency(self, {precision: prec, separator: decimal_separator, delimiter: k_separator, format: format, unit: unit}) : nil
159
+ number_to_currency(self, {precision: [precision || settings[:precision], 0].max, separator: decimal_separator || settings[:decimal_separator], delimiter: k_separator || settings[:k_separator], format: format, unit: unit})
160
+ else
161
+ nil
162
+ end
135
163
  end
136
164
 
137
165
  # Formats a boolean.
@@ -141,33 +169,34 @@ module Lazier
141
169
  # @param false_name [String] The string representation of `false`. Defaults to `No`.
142
170
  # @return [String] The string representation of the object.
143
171
  def format_boolean(true_name = nil, false_name = nil)
144
- names = {
145
- true => true_name || ::Lazier.settings.boolean_names[true],
146
- false => false_name || ::Lazier.settings.boolean_names[false]
147
- }
172
+ settings = ::Lazier.settings.boolean_names
173
+ to_boolean ? (true_name || settings[true]) : (false_name || settings[false])
174
+ end
148
175
 
149
- names[self.to_boolean]
176
+ # Prepares an object to be printed in list summaries, like `[01/04] Opening this...`.
177
+ #
178
+ # @param length [Fixnum] The minimum length of the label.
179
+ # @param filler [String] The minimum length of the label.
180
+ # @param formatter [Symbol] The method to use to format the label. Must accept the `length` and the `filler arguments.
181
+ # @return [String] The object inspected and formatted.
182
+ def indexize(length = 2, filler = "0", formatter = :rjust)
183
+ self.ensure_string.send(formatter, length, filler)
150
184
  end
151
185
 
152
186
  # Inspects an object.
153
187
  #
154
188
  # @param format The format to use.
155
- # @param must_raise [Boolean] If raise a Dump exception.
189
+ # @param as_exception [Boolean] If raise an exception.
156
190
  # @return [String] The object inspected and formatted.
157
- def debug_dump(format = :yaml, must_raise = true)
158
- rv = ""
159
-
160
- begin
161
- if format == :pretty_json then
162
- rv = ::JSON.pretty_generate(self)
191
+ def analyze(format = :yaml, as_exception = true)
192
+ rv = case format
193
+ when :pretty_json
194
+ ::JSON.pretty_generate(self)
163
195
  else
164
- rv = self.send("to_#{format}")
165
- end
166
- rescue
167
- rv = self.inspect
196
+ send("to_#{format}")
168
197
  end
169
198
 
170
- must_raise ? raise(::Lazier::Exceptions::Dump.new(rv)) : rv
199
+ as_exception ? raise(::Lazier::Exceptions::Debug.new(rv)) : rv
171
200
  end
172
201
  end
173
202
  end
@@ -18,9 +18,7 @@ module Lazier
18
18
  #
19
19
  # @return [Array] A list of all components that are included in this path.
20
20
  def components
21
- rv = []
22
- self.each_filename { |p| rv << p }
23
- rv
21
+ each_filename.to_a
24
22
  end
25
23
  end
26
24
  end
@@ -31,21 +31,21 @@ module Lazier
31
31
  # @return [Settings] The singleton instance of the settings.
32
32
  def self.instance(force = false)
33
33
  @instance = nil if force
34
- @instance ||= self.new
34
+ @instance ||= ::Lazier::Settings.new
35
35
  end
36
36
 
37
37
  # Initializes a new settings object.
38
38
  def initialize
39
- self.i18n_setup(:lazier, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
40
- self.setup
39
+ i18n_setup(:lazier, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
40
+ setup
41
41
  end
42
42
 
43
43
  # Setups the current instance.
44
44
  def setup
45
- self.setup_format_number
46
- self.setup_boolean_names
47
- self.setup_date_formats
48
- self.setup_date_names
45
+ setup_format_number
46
+ setup_boolean_names
47
+ setup_date_formats
48
+ setup_date_names
49
49
  end
50
50
 
51
51
  # Set the current locale for messages.
@@ -54,19 +54,19 @@ module Lazier
54
54
  # @return [R18n::Translation] The new translation object.
55
55
  def i18n=(locale)
56
56
  super(locale)
57
- self.setup
57
+ setup
58
58
  end
59
59
 
60
60
  # Setups formatters for a number.
61
61
  # @see Object#format_number
62
62
  #
63
- # @param prec [Fixnum] The precision to show.
63
+ # @param precision [Fixnum] The precision to show.
64
64
  # @param decimal_separator [String] The string to use as decimal separator.
65
65
  # @param add_string [String] The string to append to the number.
66
66
  # @param k_separator [String] The string to use as thousands separator.
67
67
  # @return [Hash] The new formatters.
68
- def setup_format_number(prec = 2, decimal_separator = ".", add_string = "", k_separator = ",")
69
- @format_number = { prec: prec, decimal_separator: decimal_separator, add_string: add_string, k_separator: k_separator}
68
+ def setup_format_number(precision = 2, decimal_separator = ".", add_string = nil, k_separator = ",")
69
+ @format_number = ::HashWithIndifferentAccess.new({precision: precision, decimal_separator: decimal_separator, add_string: add_string, k_separator: k_separator})
70
70
  end
71
71
 
72
72
  # Setups strings representation of booleans.
@@ -76,9 +76,7 @@ module Lazier
76
76
  # @param false_name [String] The string representation of `false`. Defaults to `No`.
77
77
  # @return [Hash] The new representations.
78
78
  def setup_boolean_names(true_name = nil, false_name = nil)
79
- true_name ||= self.i18n.boolean[0]
80
- false_name ||= self.i18n.boolean[1]
81
- @boolean_names = {true => true_name, false => false_name}
79
+ @boolean_names = {true => true_name || i18n.boolean[0], false => false_name || i18n.boolean[1]}
82
80
  end
83
81
 
84
82
  # Setups custom formats for dates and times.
@@ -88,18 +86,10 @@ module Lazier
88
86
  # @param replace [Boolean] If to discard current formats.
89
87
  # @return [Hash] The new formats.
90
88
  def setup_date_formats(formats = nil, replace = false)
91
- 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?
89
+ @date_formats = HashWithIndifferentAccess.new if replace || !@date_formats
92
90
 
93
- if formats.is_a?(::Hash) then
94
- if !replace then
95
- @date_formats ||= {}
96
- @date_formats.merge!(formats)
97
- else
98
- @date_formats = formats
99
- end
100
-
101
- @date_formats.each_pair do |k, v| ::Time::DATE_FORMATS[k] = v end
102
- end
91
+ @date_formats.merge!(formats.ensure_hash({ct_date: "%Y-%m-%d", ct_time: "%H:%M:%S", ct_date_time: "%F %T", ct_iso_8601: "%FT%T%z" }))
92
+ ::Time::DATE_FORMATS.merge!(@date_formats)
103
93
 
104
94
  @date_formats
105
95
  end
@@ -115,12 +105,14 @@ module Lazier
115
105
  # @param short_days [Array] The abbreviated string representation of days.
116
106
  # @return [Hash] The new representations.
117
107
  def setup_date_names(long_months = nil, short_months = nil, long_days = nil, short_days = nil)
118
- long_months = self.i18n.date.long_months if long_months.blank?
119
- short_months = self.i18n.date.short_months if short_months.blank?
120
- long_days = self.i18n.date.long_days if long_days.blank?
121
- short_days = self.i18n.date.short_days if short_days.blank?
108
+ definitions = self.i18n.date
122
109
 
123
- @date_names = { long_months: long_months, short_months: short_months, long_days: long_days, short_days: short_days }
110
+ @date_names = {
111
+ long_months: long_months.ensure(definitions.long_months),
112
+ short_months: short_months.ensure(definitions.short_months),
113
+ long_days: long_days.ensure(definitions.long_days),
114
+ short_days: short_days.ensure(definitions.short_days)
115
+ }.with_indifferent_access
124
116
  end
125
117
  end
126
118
  end