lazier 3.5.7 → 4.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.
- checksums.yaml +4 -4
- data/.travis-gemfile +5 -4
- data/.travis.yml +2 -2
- data/CHANGELOG.md +65 -8
- data/Gemfile +9 -8
- data/README.md +4 -0
- data/doc/Lazier.html +178 -110
- data/doc/Lazier/Boolean.html +7 -7
- data/doc/Lazier/Configuration.html +24 -50
- data/doc/Lazier/DateTime.html +50 -305
- data/doc/Lazier/DateTime/ClassMethods.html +116 -806
- data/doc/Lazier/Exceptions.html +2 -2
- data/doc/Lazier/Exceptions/Debug.html +1 -1
- data/doc/Lazier/Exceptions/MissingTranslation.html +18 -14
- data/doc/Lazier/Exceptions/TranslationExceptionHandler.html +213 -0
- data/doc/Lazier/Hash.html +19 -155
- data/doc/Lazier/I18n.html +1735 -230
- data/doc/Lazier/Math.html +1 -1
- data/doc/Lazier/Math/ClassMethods.html +13 -13
- data/doc/Lazier/Object.html +353 -339
- data/doc/Lazier/Pathname.html +4 -4
- data/doc/Lazier/Settings.html +150 -304
- data/doc/Lazier/String.html +39 -199
- data/doc/Lazier/TimeZone.html +244 -746
- data/doc/Lazier/TimeZone/ClassMethods.html +109 -127
- data/doc/Lazier/Version.html +4 -4
- data/doc/_index.html +15 -15
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +5 -1
- data/doc/index.html +5 -1
- data/doc/method_list.html +97 -169
- data/doc/top-level-namespace.html +1 -1
- data/lazier.gemspec +9 -6
- data/lib/lazier.rb +41 -50
- data/lib/lazier/boolean.rb +0 -1
- data/lib/lazier/configuration.rb +26 -28
- data/lib/lazier/datetime.rb +33 -127
- data/lib/lazier/exceptions.rb +14 -6
- data/lib/lazier/hash.rb +7 -15
- data/lib/lazier/i18n.rb +130 -48
- data/lib/lazier/math.rb +6 -7
- data/lib/lazier/object.rb +79 -97
- data/lib/lazier/pathname.rb +0 -1
- data/lib/lazier/settings.rb +12 -25
- data/lib/lazier/string.rb +17 -38
- data/lib/lazier/timezone.rb +168 -164
- data/lib/lazier/version.rb +3 -4
- data/locales/en.yml +52 -51
- data/locales/it.yml +51 -50
- data/spec/coverage_helper.rb +0 -1
- data/spec/lazier/boolean_spec.rb +2 -3
- data/spec/lazier/configuration_spec.rb +3 -5
- data/spec/lazier/datetime_spec.rb +34 -95
- data/spec/lazier/exceptions_spec.rb +25 -0
- data/spec/lazier/hash_spec.rb +0 -21
- data/spec/lazier/i18n_spec.rb +135 -51
- data/spec/lazier/math_spec.rb +0 -1
- data/spec/lazier/object_spec.rb +105 -100
- data/spec/lazier/pathname_spec.rb +0 -1
- data/spec/lazier/settings_spec.rb +25 -28
- data/spec/lazier/string_spec.rb +7 -20
- data/spec/lazier/timezone_spec.rb +101 -87
- data/spec/lazier_spec.rb +25 -8
- data/spec/spec_helper.rb +3 -2
- metadata +15 -44
- data/doc/Lazier/Localizer.html +0 -545
- data/lib/lazier/localizer.rb +0 -41
- data/spec/lazier/localizer_spec.rb +0 -45
data/lib/lazier/exceptions.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
#
|
3
2
|
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
3
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
@@ -11,14 +10,23 @@ module Lazier
|
|
11
10
|
class Debug < ::RuntimeError
|
12
11
|
end
|
13
12
|
|
14
|
-
# This
|
13
|
+
# This is the handler for the core I18n gem.
|
14
|
+
class TranslationExceptionHandler < ::I18n::ExceptionHandler
|
15
|
+
# :nodoc:
|
16
|
+
def call(exception, locale, key, options)
|
17
|
+
exception.is_a?(::I18n::MissingTranslation) ? raise(exception.to_exception) : super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# This exception is raised from {I18n I18n} if a string is not translatable.
|
15
22
|
class MissingTranslation < RuntimeError
|
16
23
|
# Creates a new missing translation exception.
|
17
24
|
#
|
18
|
-
# @param
|
19
|
-
# @param
|
20
|
-
def initialize(
|
21
|
-
|
25
|
+
# @param locale [Array] The locale that was requested to use.
|
26
|
+
# @param message [String] The message that was requested to translate.
|
27
|
+
def initialize(locale, message = nil)
|
28
|
+
locale, message = locale if message.nil?
|
29
|
+
super("Unable to load the translation \"#{message}\" for the locale \"#{locale}\".")
|
22
30
|
end
|
23
31
|
end
|
24
32
|
end
|
data/lib/lazier/hash.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
#
|
3
2
|
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
3
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
@@ -38,11 +37,8 @@ module Lazier
|
|
38
37
|
# @param accesses [Array] The requested access for the keys. Can be `:strings`, `:symbols` or `:indifferent`. If `nil` the keys are not modified.
|
39
38
|
# @return [Hash] The current hash with keys modified.
|
40
39
|
def ensure_access(*accesses)
|
41
|
-
accesses.compact
|
42
|
-
|
43
|
-
rv = rv.send(method) if method
|
44
|
-
rv
|
45
|
-
end
|
40
|
+
methods = accesses.ensure_array(compact: true, no_duplicates: true, flatten: true) { |m| VALID_ACCESSES[m.ensure_string.to_sym] }.compact
|
41
|
+
methods.reduce(self) { |a, e| a.send(e) }
|
46
42
|
end
|
47
43
|
|
48
44
|
# Makes sure that the hash is accessible using dotted notation. This is also applied to every embedded hash.
|
@@ -54,22 +50,18 @@ module Lazier
|
|
54
50
|
extend(Hashie::Extensions::MethodQuery)
|
55
51
|
extend(Hashie::Extensions::MethodWriter) unless readonly
|
56
52
|
|
57
|
-
each
|
58
|
-
enable_dotted_access_for_value(value, readonly)
|
59
|
-
end
|
53
|
+
each { |_, value| enable_dotted_access_for_value(value, readonly) }
|
60
54
|
|
61
55
|
self
|
62
56
|
end
|
63
57
|
|
64
|
-
|
65
|
-
|
66
|
-
#
|
67
|
-
# @param readonly [Boolean] If the dotted notation is only enable for reading. `true` by default.
|
68
|
-
# @return [Hash] The current value enabled for dotted access.
|
58
|
+
private
|
59
|
+
|
60
|
+
# :nodoc:
|
69
61
|
def enable_dotted_access_for_value(value, readonly)
|
70
62
|
if value.is_a?(Hash)
|
71
63
|
value.enable_dotted_access(readonly)
|
72
|
-
elsif value.respond_to?(:each)
|
64
|
+
elsif value.respond_to?(:each)
|
73
65
|
value.each do |element|
|
74
66
|
element.enable_dotted_access(readonly) if element.is_a?(Hash)
|
75
67
|
end
|
data/lib/lazier/i18n.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
#
|
3
2
|
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
3
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
@@ -7,79 +6,162 @@
|
|
7
6
|
module Lazier
|
8
7
|
# Provides an easy way to localized messages in a class.
|
9
8
|
#
|
10
|
-
# @attribute
|
9
|
+
# @attribute locale
|
11
10
|
# @return [String|Symbol|nil] The current locale.
|
12
|
-
# @attribute [r]
|
11
|
+
# @attribute [r] root
|
13
12
|
# @return [Symbol] The root level of the translation.
|
14
|
-
# @attribute [r]
|
13
|
+
# @attribute [r] path
|
15
14
|
# @return [String] The path where the translations are stored.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
# @attribute [r] backend
|
16
|
+
# @return [I18n::Backend] The backend used for translations.
|
17
|
+
class I18n
|
18
|
+
attr_accessor :locale
|
19
|
+
attr_reader :root, :path, :backend
|
20
20
|
|
21
|
-
#
|
21
|
+
# The default locale for new instances.
|
22
|
+
mattr_accessor :default_locale
|
23
|
+
|
24
|
+
# Returns the singleton instance of the settings.
|
22
25
|
#
|
26
|
+
# @param locale [Symbol] The locale to use for translations. Default is the current system locale.
|
23
27
|
# @param root [Symbol] The root level of the translation.
|
24
28
|
# @param path [String] The path where the translations are stored.
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
@
|
29
|
+
# @param force [Boolean] If to force recreation of the instance.
|
30
|
+
# @return [I18n] The singleton instance of the i18n.
|
31
|
+
def self.instance(locale = nil, root: :lazier, path: nil, force: false)
|
32
|
+
@instance = nil if force
|
33
|
+
@instance ||= new(locale, root: root, path: path)
|
29
34
|
end
|
30
35
|
|
31
|
-
#
|
36
|
+
# Creates a new I18n object.
|
32
37
|
#
|
33
|
-
# @
|
34
|
-
|
35
|
-
|
38
|
+
# @param locale [Symbol] The locale to use. Defaults to the current locale.
|
39
|
+
# @param root [Symbol] The root level of the translation.
|
40
|
+
# @param path [String] The path where the translations are stored.
|
41
|
+
def initialize(locale = nil, root: :lazier, path: nil)
|
42
|
+
path ||= Lazier::ROOT + "/locales"
|
43
|
+
@root = root.to_sym
|
44
|
+
@path = File.absolute_path(path.to_s)
|
45
|
+
|
46
|
+
setup_backend
|
47
|
+
|
48
|
+
self.locale = (locale || Lazier::I18n.default_locale || system_locale).to_sym
|
49
|
+
end
|
50
|
+
|
51
|
+
# Reloads all the I18n translations.
|
52
|
+
def reload
|
53
|
+
# Extract the backend to an attribute
|
54
|
+
::I18n.backend.load_translations
|
36
55
|
end
|
37
56
|
|
38
|
-
#
|
57
|
+
# Gets the list of available translation for a locale.
|
39
58
|
#
|
40
|
-
# @param locale [
|
41
|
-
# @return [
|
42
|
-
def
|
43
|
-
|
44
|
-
@
|
59
|
+
# @param locale [Symbol] The locale to list. Defaults to the current locale.
|
60
|
+
# @return [Hash] The available translations for the specified locale.
|
61
|
+
def translations(locale = nil)
|
62
|
+
locale ||= @locale
|
63
|
+
@backend.send(:translations)[locale.to_sym] || {}
|
45
64
|
end
|
46
65
|
|
47
|
-
|
66
|
+
# Sets the current locale.
|
67
|
+
#
|
68
|
+
# @param value [Symbol] The locale to use for translations. Default is the current system locale.
|
69
|
+
def locale=(value)
|
70
|
+
@locale = value.to_sym
|
71
|
+
::I18n.locale = @locale
|
72
|
+
end
|
73
|
+
|
74
|
+
# Get the list of available translation for a locale.
|
75
|
+
#
|
76
|
+
# @return [Array] The list of available locales.
|
77
|
+
def locales
|
78
|
+
::I18n.available_locales
|
79
|
+
end
|
48
80
|
|
49
|
-
#
|
81
|
+
# Localize a message.
|
50
82
|
#
|
51
|
-
# @param
|
52
|
-
# @
|
53
|
-
|
54
|
-
|
55
|
-
|
83
|
+
# @param message [String|Symbol] The message to localize.
|
84
|
+
# @param args [Array] Optional arguments to localize the message.
|
85
|
+
# @return [String] The localized message.
|
86
|
+
def translate(message, **args)
|
87
|
+
# PI: Ignore reek on this.
|
88
|
+
message = "#{root}.#{message}" if message !~ /^(\.|::)/
|
56
89
|
|
57
90
|
begin
|
58
|
-
|
59
|
-
|
60
|
-
raise
|
61
|
-
translation
|
62
|
-
rescue
|
63
|
-
raise Lazier::Exceptions::MissingTranslation.new(locales, path)
|
91
|
+
::I18n.translate(message, **(args.merge(raise: true)))
|
92
|
+
rescue ::I18n::MissingTranslationData
|
93
|
+
raise Lazier::Exceptions::MissingTranslation, [locale, message]
|
64
94
|
end
|
65
95
|
end
|
96
|
+
alias_method :t, :translate
|
66
97
|
|
67
|
-
#
|
98
|
+
# Localize a message in a specific locale.
|
68
99
|
#
|
69
|
-
# @param
|
70
|
-
# @param
|
71
|
-
# @
|
72
|
-
|
73
|
-
|
100
|
+
# @param message [String|Symbol] The message to localize.
|
101
|
+
# @param locale [String|Symbol] The new locale to use for localization.
|
102
|
+
# @param args [Array] Optional arguments to localize the message.
|
103
|
+
# @return [String] The localized message.
|
104
|
+
def translate_in_locale(locale, message, *args)
|
105
|
+
with_locale(locale) { translate(message, *args) }
|
74
106
|
end
|
107
|
+
alias_method :tl, :translate_in_locale
|
75
108
|
|
76
|
-
#
|
109
|
+
# Temporary sets a different locale and execute the given block.
|
77
110
|
#
|
78
|
-
# @param locale [String] The locale to
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
111
|
+
# @param locale [String|Symbol] The new locale to use for localization.
|
112
|
+
def with_locale(locale)
|
113
|
+
old_locale = self.locale
|
114
|
+
|
115
|
+
begin
|
116
|
+
self.locale = locale
|
117
|
+
return yield
|
118
|
+
ensure
|
119
|
+
self.locale = old_locale
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
private
|
124
|
+
|
125
|
+
# :nodoc:
|
126
|
+
OSX_DETECTION = "defaults read .GlobalPreferences AppleLanguages | awk 'NR==2{gsub(/[ ,]/, \"\");print}'"
|
127
|
+
|
128
|
+
# :nodoc:
|
129
|
+
def system_locale
|
130
|
+
platform = Lazier.platform
|
131
|
+
|
132
|
+
if [:java, :osx, :posix].include?(platform)
|
133
|
+
send("system_locale_#{Lazier.platform}")
|
134
|
+
else
|
135
|
+
raise(RuntimeError)
|
136
|
+
end
|
137
|
+
rescue
|
138
|
+
"en"
|
139
|
+
end
|
140
|
+
|
141
|
+
# :nodoc:
|
142
|
+
def system_locale_java
|
143
|
+
Java.java.util.Locale.getDefault.toString
|
144
|
+
end
|
145
|
+
|
146
|
+
# :nodoc:
|
147
|
+
def system_locale_osx
|
148
|
+
`#{OSX_DETECTION}`.strip
|
149
|
+
end
|
150
|
+
|
151
|
+
# :nodoc:
|
152
|
+
def system_locale_posix
|
153
|
+
ENV["LANG"]
|
154
|
+
end
|
155
|
+
|
156
|
+
# :nodoc:
|
157
|
+
def setup_backend
|
158
|
+
::I18n.enforce_available_locales = true
|
159
|
+
::I18n.load_path += Dir["#{@path}/*.yml"]
|
160
|
+
::I18n.load_path.uniq!
|
161
|
+
::I18n.exception_handler = ::Lazier::Exceptions::TranslationExceptionHandler.new
|
162
|
+
reload
|
163
|
+
|
164
|
+
@backend = ::I18n.backend
|
83
165
|
end
|
84
166
|
end
|
85
167
|
end
|
data/lib/lazier/math.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
#
|
3
2
|
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
3
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
@@ -13,18 +12,18 @@ module Lazier
|
|
13
12
|
module ClassMethods
|
14
13
|
# Returns the minimum value in the arguments
|
15
14
|
#
|
16
|
-
# @param args [Array] A
|
17
|
-
# @return [Object] The minimum value or `nil` (if the
|
15
|
+
# @param args [Array] A list of objects to compare (with the `<` operator).
|
16
|
+
# @return [Object] The minimum value or `nil` (if the list is empty).
|
18
17
|
def min(*args)
|
19
|
-
args.ensure_array
|
18
|
+
args.ensure_array(default: [], no_duplicates: true, compact: true, flatten: true).min
|
20
19
|
end
|
21
20
|
|
22
21
|
# Returns the maximum value in the arguments
|
23
22
|
#
|
24
|
-
# @param args [Array] A
|
25
|
-
# @return [Object] The maximum value or `nil` (if the
|
23
|
+
# @param args [Array] A list of objects to compare (with the `>` operator).
|
24
|
+
# @return [Object] The maximum value or `nil` (if the list is empty).
|
26
25
|
def max(*args)
|
27
|
-
args.ensure_array
|
26
|
+
args.ensure_array(default: [], no_duplicates: true, compact: true, flatten: true).max
|
28
27
|
end
|
29
28
|
end
|
30
29
|
end
|
data/lib/lazier/object.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
#
|
3
2
|
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
3
|
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
@@ -25,42 +24,36 @@ module Lazier
|
|
25
24
|
#
|
26
25
|
# @return [String] The normalized number.
|
27
26
|
def normalize_number
|
28
|
-
|
27
|
+
boolean? ? to_i.to_s : ensure_string.strip.gsub(/[\.,](?=(.*[\.,]))/, "").gsub(",", ".")
|
28
|
+
end
|
29
|
+
|
30
|
+
# Checks if the object is of a numeric class of matches a numeric string expression.
|
31
|
+
#
|
32
|
+
# @return [Boolean] `true` is a valid numeric object, `false` otherwise.
|
33
|
+
def number?(klass = Integer, matcher = ::Lazier::Object::FLOAT_MATCHER)
|
34
|
+
nil? || is_a?(klass) || boolean? || normalize_number =~ matcher
|
29
35
|
end
|
30
36
|
|
31
37
|
# Checks if the object is a valid integer.
|
32
38
|
#
|
33
39
|
# @return [Boolean] `true` is a valid integer, `false` otherwise.
|
34
40
|
def integer?
|
35
|
-
|
41
|
+
number?(Integer, ::Lazier::Object::INTEGER_MATCHER)
|
36
42
|
end
|
37
|
-
alias_method :is_integer?, :integer?
|
38
43
|
|
39
44
|
# Checks if the object is a valid float.
|
40
45
|
#
|
41
46
|
# @return [Boolean] `true` is a valid float, `false` otherwise.
|
42
47
|
def float?
|
43
|
-
|
44
|
-
end
|
45
|
-
alias_method :number?, :float?
|
46
|
-
alias_method :is_float?, :float?
|
47
|
-
alias_method :is_number?, :float?
|
48
|
-
|
49
|
-
# Checks if the object is of a numeric class of matches a numeric string expression.
|
50
|
-
#
|
51
|
-
# @return [Boolean] `true` is a valid numeric object, `false` otherwise.
|
52
|
-
def numeric?(klass = Integer, matcher = ::Lazier::Object::INTEGER_MATCHER)
|
53
|
-
is_a?(klass) || is_a?(::TrueClass) || !self || normalize_number =~ matcher
|
48
|
+
number?(Numeric, ::Lazier::Object::FLOAT_MATCHER)
|
54
49
|
end
|
55
|
-
alias_method :is_numeric?, :numeric?
|
56
50
|
|
57
51
|
# Checks if the object is a valid boolean value.
|
58
52
|
#
|
59
53
|
# @return [Boolean] `true` is a valid boolean value, `false` otherwise.
|
60
54
|
def boolean?
|
61
|
-
is_a?(::TrueClass) ||
|
55
|
+
nil? || is_a?(::TrueClass) || is_a?(::FalseClass) || to_s =~ ::Lazier::Object::BOOLEAN_MATCHER
|
62
56
|
end
|
63
|
-
alias_method :is_boolean?, :boolean?
|
64
57
|
|
65
58
|
# Sends a method to the object. If the objects doesn't not respond to the method, it returns `nil` instead of raising an exception.
|
66
59
|
#
|
@@ -69,70 +62,71 @@ module Lazier
|
|
69
62
|
# @param block [Proc] The block to pass to the method.
|
70
63
|
# @return [Object|nil] The return value of the method or `nil`, if the object does not respond to the method.
|
71
64
|
def safe_send(method, *args, &block)
|
72
|
-
|
65
|
+
send(method, *args, &block)
|
66
|
+
rescue NoMethodError
|
67
|
+
nil
|
73
68
|
end
|
74
69
|
|
75
70
|
# Makes sure that the object is set to something meaningful.
|
76
71
|
#
|
77
|
-
# @param
|
72
|
+
# @param default [String] The default value to return if the `verifier` or the block returns true.
|
78
73
|
# @param verifier [Symbol] The method used to verify if the object is NOT meaningful. *Ignored if a block is passed.*
|
79
74
|
# @return [String] The current object or the `default_value`.
|
80
|
-
def ensure(
|
75
|
+
def ensure(default, verifier = :blank?)
|
81
76
|
valid = block_given? ? yield(self) : send(verifier)
|
82
|
-
!valid ? self :
|
77
|
+
!valid ? self : default
|
83
78
|
end
|
84
79
|
|
85
80
|
# Makes sure that the object is a string.
|
86
81
|
#
|
87
|
-
# @param
|
88
|
-
# @param
|
82
|
+
# @param default [String] The default value to return if the object is `nil`. It is also passed to the block stringifier.
|
83
|
+
# @param conversion_method [Symbol] The method used to convert the object to a string. *Ignored if a block is passed.*
|
89
84
|
# @return [String] The string representation of the object.
|
90
|
-
def ensure_string(
|
85
|
+
def ensure_string(default = "", conversion_method = :to_s)
|
91
86
|
if is_a?(NilClass)
|
92
|
-
|
87
|
+
default
|
93
88
|
else
|
94
|
-
block_given? ? yield(self,
|
89
|
+
block_given? ? yield(self, default) : send(conversion_method)
|
95
90
|
end
|
96
91
|
end
|
97
92
|
|
98
93
|
# Makes sure that the object is an array. For non array objects, return a single element array containing the object.
|
99
94
|
#
|
100
|
-
# @param
|
101
|
-
# @param
|
95
|
+
# @param default [Array|NilClass] The default array to use. If not specified, an array containing the object is returned.
|
96
|
+
# @param no_duplicates [Boolean] If to remove duplicates from the array before sanitizing.
|
102
97
|
# @param compact [Boolean] If to compact the array before sanitizing.
|
103
98
|
# @param flatten [Boolean] If to flatten the array before sanitizing.
|
104
99
|
# @param sanitizer [Symbol|nil] If not `nil`, the method to use to sanitize entries of the array. *Ignored if a block is present.*
|
105
100
|
# @param block [Proc] A block to sanitize entries. It must accept the value as unique argument.
|
106
101
|
# @return [Array] If the object is an array, then the object itself, a single element array containing the object otherwise.
|
107
|
-
def ensure_array(
|
102
|
+
def ensure_array(default: nil, no_duplicates: false, compact: false, flatten: false, sanitizer: nil, &block)
|
108
103
|
rv =
|
109
104
|
if is_a?(::Array)
|
110
|
-
|
105
|
+
self
|
111
106
|
else
|
112
|
-
|
107
|
+
default || [self].compact
|
113
108
|
end
|
114
109
|
|
115
|
-
rv = manipulate_array(rv,
|
116
|
-
manipulate_array(rv,
|
110
|
+
rv = manipulate_array(rv, no_duplicates, compact, flatten).map(&(block || sanitizer)) if block_given? || sanitizer
|
111
|
+
manipulate_array(rv, no_duplicates, compact, flatten)
|
117
112
|
end
|
118
113
|
|
119
114
|
# Makes sure that the object is an hash. For non hash objects, return an hash basing on the `default_value` parameter.
|
120
115
|
#
|
121
|
-
# @
|
122
|
-
#
|
123
|
-
# @param
|
116
|
+
# @see Lazier::Hash#ensure_access
|
117
|
+
#
|
118
|
+
# @param accesses [Symbol|NilClass|Array] The requested access for the keys of the returned object.
|
119
|
+
# @param default [Hash|String|Symbol|NilClass] The default value to use. If it is an `Hash`, it is returned as value otherwise it is used to build
|
124
120
|
# as a key to build an hash with the current object as only value (everything but strings and symbols are mapped to `key`).
|
125
121
|
# Passing `nil` is equal to pass an empty Hash.
|
126
122
|
# @param sanitizer [Symbol|nil] If not `nil`, the method to use to sanitize values of the hash. *Ignored if `block` is present.*
|
127
123
|
# @param block [Proc] A block to sanitize entries. It must accept the value as unique argument.
|
128
124
|
# @return [Hash] If the object is an hash, then the object itself, a hash with the object as single value otherwise.
|
129
|
-
def ensure_hash(
|
130
|
-
|
131
|
-
|
132
|
-
rv = convert_to_hash(default_value)
|
125
|
+
def ensure_hash(accesses: nil, default: {}, sanitizer: nil, &block)
|
126
|
+
rv = convert_to_hash(default)
|
133
127
|
rv = sanitize_hash(rv, sanitizer, block) if block || sanitizer
|
134
128
|
|
135
|
-
rv.respond_to?(:ensure_access) ? rv.ensure_access(
|
129
|
+
rv.respond_to?(:ensure_access) ? rv.ensure_access(accesses.ensure_array) : rv
|
136
130
|
end
|
137
131
|
|
138
132
|
# Converts the object to a boolean.
|
@@ -144,30 +138,47 @@ module Lazier
|
|
144
138
|
|
145
139
|
# Converts the object to a integer.
|
146
140
|
#
|
147
|
-
# @param
|
141
|
+
# @param default [Fixnum] The value to return if the conversion is not possible.
|
148
142
|
# @return [Fixnum] The integer representation of the object.
|
149
|
-
def to_integer(
|
150
|
-
to_float(
|
143
|
+
def to_integer(default = 0)
|
144
|
+
to_float(default).to_i
|
151
145
|
end
|
152
146
|
|
153
147
|
# Converts the object to a float.
|
154
148
|
#
|
155
|
-
# @param
|
149
|
+
# @param default [Float] The value to return if the conversion is not possible.
|
156
150
|
# @return [Float] The float representation of the object.
|
157
|
-
def to_float(
|
158
|
-
if
|
151
|
+
def to_float(default = 0.0)
|
152
|
+
if float?
|
159
153
|
::Kernel.Float(is_a?(::Numeric) ? self : normalize_number)
|
160
154
|
else
|
161
|
-
|
155
|
+
default
|
162
156
|
end
|
163
157
|
end
|
164
158
|
|
159
|
+
# Converts an object to a pretty formatted JSON string.
|
160
|
+
#
|
161
|
+
# @return [String] The object as a pretty JSON string.
|
162
|
+
def to_pretty_json
|
163
|
+
Lazier.platform != :java ? Oj.dump(self, mode: :compat, indent: 2) : ::JSON.pretty_generate(self)
|
164
|
+
end
|
165
|
+
|
166
|
+
# Inspects an object.
|
167
|
+
#
|
168
|
+
# @param format The format to use. If different from `:pretty_json`, the object must respond to the `to_#{format}` method.
|
169
|
+
# @param as_exception [Boolean] If raise an exception.
|
170
|
+
# @return [String] The object inspected and formatted.
|
171
|
+
def to_debug(format: :pretty_json, as_exception: true)
|
172
|
+
rv = send("to_#{format}")
|
173
|
+
as_exception ? raise(::Lazier::Exceptions::Debug, rv) : rv
|
174
|
+
end
|
175
|
+
|
165
176
|
# Returns the rounded float representaton of the object.
|
166
177
|
#
|
167
178
|
# @param precision [Fixnum] The precision to keep.
|
168
179
|
# @return [Float] The rounded float representaton of the object.
|
169
180
|
def round_to_precision(precision = 2)
|
170
|
-
|
181
|
+
number? ? to_float.round([precision, 0].max) : nil
|
171
182
|
end
|
172
183
|
|
173
184
|
# Formats a number.
|
@@ -178,8 +189,8 @@ module Lazier
|
|
178
189
|
# @param add_string [String] The string to append to the number.
|
179
190
|
# @param k_separator [String] The string to use as thousands separator.
|
180
191
|
# @return [String] The string representation of the object.
|
181
|
-
def format_number(precision
|
182
|
-
if
|
192
|
+
def format_number(precision: nil, decimal_separator: nil, add_string: nil, k_separator: nil)
|
193
|
+
if number?
|
183
194
|
settings = ::Lazier.settings.format_number
|
184
195
|
add_string ||= settings[:add_string]
|
185
196
|
|
@@ -198,7 +209,7 @@ module Lazier
|
|
198
209
|
# @param true_name [String] The string representation of `true`. Defaults to `Yes`.
|
199
210
|
# @param false_name [String] The string representation of `false`. Defaults to `No`.
|
200
211
|
# @return [String] The string representation of the object.
|
201
|
-
def format_boolean(true_name
|
212
|
+
def format_boolean(true_name: nil, false_name: nil)
|
202
213
|
settings = ::Lazier.settings.boolean_names
|
203
214
|
to_boolean ? (true_name || settings[true]) : (false_name || settings[false])
|
204
215
|
end
|
@@ -209,67 +220,38 @@ module Lazier
|
|
209
220
|
# @param filler [String] The minimum length of the label.
|
210
221
|
# @param formatter [Symbol] The method to use to format the label. Must accept the `length` and the `filler arguments.
|
211
222
|
# @return [String] The object inspected and formatted.
|
212
|
-
def indexize(length
|
223
|
+
def indexize(length: 2, filler: "0", formatter: :rjust)
|
213
224
|
ensure_string.send(formatter, length, filler)
|
214
225
|
end
|
215
226
|
|
216
|
-
# Inspects an object.
|
217
|
-
#
|
218
|
-
# @param format The format to use.
|
219
|
-
# @param as_exception [Boolean] If raise an exception.
|
220
|
-
# @return [String] The object inspected and formatted.
|
221
|
-
def for_debug(format = :yaml, as_exception = true)
|
222
|
-
rv =
|
223
|
-
case format
|
224
|
-
when :pretty_json then ::JSON.pretty_generate(self)
|
225
|
-
else send("to_#{format}")
|
226
|
-
end
|
227
|
-
|
228
|
-
as_exception ? raise(::Lazier::Exceptions::Debug, rv) : rv
|
229
|
-
end
|
230
|
-
|
231
227
|
private
|
232
228
|
|
233
|
-
#
|
234
|
-
|
235
|
-
# @param rv [Array] The input array.
|
236
|
-
# @param uniq [Boolean] If to remove duplicates from the array.
|
237
|
-
# @param compact [Boolean] If to compact the array.
|
238
|
-
# @param flatten [Boolean] If to flatten the array.
|
239
|
-
# @return [Array] The manipulated array.
|
240
|
-
def manipulate_array(rv, uniq, compact, flatten)
|
229
|
+
# :nodoc:
|
230
|
+
def manipulate_array(rv, no_duplicates, compact, flatten)
|
241
231
|
rv = rv.flatten if flatten
|
242
|
-
rv = rv.uniq if
|
232
|
+
rv = rv.uniq if no_duplicates
|
243
233
|
rv = rv.compact if compact
|
244
234
|
rv
|
245
235
|
end
|
246
236
|
|
247
|
-
#
|
248
|
-
|
249
|
-
|
250
|
-
# as a key to build an hash with the current object as only value (everything but strings and symbols are mapped to `key`).
|
251
|
-
# Passing `nil` is equal to pass an empty Hash.
|
252
|
-
# @return [Hash] An hash.
|
253
|
-
def convert_to_hash(default_value)
|
254
|
-
if is_a?(::Hash)
|
237
|
+
# :nodoc:
|
238
|
+
def convert_to_hash(value)
|
239
|
+
if self.is_a?(::Hash)
|
255
240
|
self
|
256
|
-
elsif
|
257
|
-
|
241
|
+
elsif value.is_a?(::Hash)
|
242
|
+
value
|
258
243
|
else
|
259
|
-
key =
|
244
|
+
key = value.is_a?(::String) || value.is_a?(::Symbol) ? value : :key
|
260
245
|
{key => self}
|
261
246
|
end
|
262
247
|
end
|
263
248
|
|
264
|
-
#
|
265
|
-
#
|
266
|
-
# @param hash [Hash] The hash to sanitize.
|
267
|
-
# @param sanitizer [Symbol|nil] If not `nil`, the method to use to sanitize values of the hash. *Ignored if `block` is present.*
|
268
|
-
# @param block [Proc] A block to sanitize entries. It must accept the value as unique argument.
|
269
|
-
# @return [Hash] The sanitized hash.
|
249
|
+
# :nodoc:
|
270
250
|
def sanitize_hash(hash, sanitizer, block)
|
271
|
-
|
272
|
-
|
251
|
+
operator = block ? block : ->(v) { v.send(sanitizer) }
|
252
|
+
|
253
|
+
hash.reduce(hash.class.new) { |h, (k, v)|
|
254
|
+
h[k] = operator.call(v)
|
273
255
|
h
|
274
256
|
}
|
275
257
|
end
|