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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.travis-gemfile +5 -4
  3. data/.travis.yml +2 -2
  4. data/CHANGELOG.md +65 -8
  5. data/Gemfile +9 -8
  6. data/README.md +4 -0
  7. data/doc/Lazier.html +178 -110
  8. data/doc/Lazier/Boolean.html +7 -7
  9. data/doc/Lazier/Configuration.html +24 -50
  10. data/doc/Lazier/DateTime.html +50 -305
  11. data/doc/Lazier/DateTime/ClassMethods.html +116 -806
  12. data/doc/Lazier/Exceptions.html +2 -2
  13. data/doc/Lazier/Exceptions/Debug.html +1 -1
  14. data/doc/Lazier/Exceptions/MissingTranslation.html +18 -14
  15. data/doc/Lazier/Exceptions/TranslationExceptionHandler.html +213 -0
  16. data/doc/Lazier/Hash.html +19 -155
  17. data/doc/Lazier/I18n.html +1735 -230
  18. data/doc/Lazier/Math.html +1 -1
  19. data/doc/Lazier/Math/ClassMethods.html +13 -13
  20. data/doc/Lazier/Object.html +353 -339
  21. data/doc/Lazier/Pathname.html +4 -4
  22. data/doc/Lazier/Settings.html +150 -304
  23. data/doc/Lazier/String.html +39 -199
  24. data/doc/Lazier/TimeZone.html +244 -746
  25. data/doc/Lazier/TimeZone/ClassMethods.html +109 -127
  26. data/doc/Lazier/Version.html +4 -4
  27. data/doc/_index.html +15 -15
  28. data/doc/class_list.html +1 -1
  29. data/doc/file.README.html +5 -1
  30. data/doc/index.html +5 -1
  31. data/doc/method_list.html +97 -169
  32. data/doc/top-level-namespace.html +1 -1
  33. data/lazier.gemspec +9 -6
  34. data/lib/lazier.rb +41 -50
  35. data/lib/lazier/boolean.rb +0 -1
  36. data/lib/lazier/configuration.rb +26 -28
  37. data/lib/lazier/datetime.rb +33 -127
  38. data/lib/lazier/exceptions.rb +14 -6
  39. data/lib/lazier/hash.rb +7 -15
  40. data/lib/lazier/i18n.rb +130 -48
  41. data/lib/lazier/math.rb +6 -7
  42. data/lib/lazier/object.rb +79 -97
  43. data/lib/lazier/pathname.rb +0 -1
  44. data/lib/lazier/settings.rb +12 -25
  45. data/lib/lazier/string.rb +17 -38
  46. data/lib/lazier/timezone.rb +168 -164
  47. data/lib/lazier/version.rb +3 -4
  48. data/locales/en.yml +52 -51
  49. data/locales/it.yml +51 -50
  50. data/spec/coverage_helper.rb +0 -1
  51. data/spec/lazier/boolean_spec.rb +2 -3
  52. data/spec/lazier/configuration_spec.rb +3 -5
  53. data/spec/lazier/datetime_spec.rb +34 -95
  54. data/spec/lazier/exceptions_spec.rb +25 -0
  55. data/spec/lazier/hash_spec.rb +0 -21
  56. data/spec/lazier/i18n_spec.rb +135 -51
  57. data/spec/lazier/math_spec.rb +0 -1
  58. data/spec/lazier/object_spec.rb +105 -100
  59. data/spec/lazier/pathname_spec.rb +0 -1
  60. data/spec/lazier/settings_spec.rb +25 -28
  61. data/spec/lazier/string_spec.rb +7 -20
  62. data/spec/lazier/timezone_spec.rb +101 -87
  63. data/spec/lazier_spec.rb +25 -8
  64. data/spec/spec_helper.rb +3 -2
  65. metadata +15 -44
  66. data/doc/Lazier/Localizer.html +0 -545
  67. data/lib/lazier/localizer.rb +0 -41
  68. data/spec/lazier/localizer_spec.rb +0 -45
@@ -103,7 +103,7 @@
103
103
  </div>
104
104
 
105
105
  <div id="footer">
106
- Generated on Thu Jun 19 11:49:04 2014 by
106
+ Generated on Sun Jun 1 15:50:05 2014 by
107
107
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
108
  0.8.7.4 (ruby-2.1.2).
109
109
  </div>
@@ -23,11 +23,14 @@ Gem::Specification.new do |gem|
23
23
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
24
24
  gem.require_paths = ["lib"]
25
25
 
26
- gem.required_ruby_version = ">= 1.9.3"
26
+ gem.required_ruby_version = ">= 2.1"
27
27
 
28
- gem.add_dependency("json", "~> 1.8.1")
29
- gem.add_dependency("activesupport", ">= 3.2.13") # We don't use ~> to enable use with 4.0
30
- gem.add_dependency("tzinfo", ">= 0.3.37") # We don't use ~> to enable use with 0.3.37 (required by activesupport 4.0) and 1.x, which is the latest available
31
- gem.add_dependency("r18n-desktop", "~> 1.1.10")
32
- gem.add_dependency("hashie", "~> 2.1.1")
28
+ gem.add_dependency("activesupport", "~> 4.1")
29
+ gem.add_dependency("hashie", "~> 2.1")
30
+
31
+ if RUBY_ENGINE != "jruby"
32
+ gem.add_dependency("oj", "~> 2.9")
33
+ else
34
+ gem.add_dependency("json", "~> 1.8")
35
+ end
33
36
  end
@@ -1,20 +1,19 @@
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.
5
4
  #
6
5
 
7
- require "json"
6
+ require(RUBY_ENGINE != "jruby" ? "oj" : "json")
7
+ require "English"
8
8
  require "tzinfo"
9
9
  require "active_support"
10
10
  require "active_support/core_ext"
11
- require "r18n-desktop"
11
+ require "i18n"
12
12
  require "hashie"
13
13
 
14
14
  require "lazier/version" unless defined?(Lazier::Version)
15
15
  require "lazier/exceptions"
16
16
  require "lazier/i18n"
17
- require "lazier/localizer"
18
17
  require "lazier/configuration"
19
18
  require "lazier/settings"
20
19
  require "lazier/object"
@@ -28,6 +27,9 @@ require "lazier/pathname"
28
27
 
29
28
  # Several Ruby object enhancements.
30
29
  module Lazier
30
+ # The root directory of the library
31
+ ROOT = File.absolute_path(__dir__ + "/../")
32
+
31
33
  # Returns the settings for the extensions.
32
34
  #
33
35
  # @return [Settings] The settings for the extensions.
@@ -49,8 +51,9 @@ module Lazier
49
51
  # @option pathname Extensions for path objects.
50
52
  # @return [Settings] The settings for the extensions.
51
53
  def self.load!(*what)
52
- modules = what.present? ? what.flatten.uniq.compact.map(&:to_s) : %w(object boolean string hash datetime math pathname)
53
- modules.each { |w| ::Lazier.send("load_#{w}") }
54
+ valid_modules = [:object, :boolean, :string, :hash, :datetime, :math, :pathname]
55
+ modules = what.present? ? what.flatten.uniq.compact.map(&:to_s) : valid_modules
56
+ (modules & valid_modules).each { |w| ::Lazier.send("load_#{w}") }
54
57
 
55
58
  yield if block_given?
56
59
  ::Lazier::Settings.instance
@@ -82,14 +85,10 @@ module Lazier
82
85
  # Loads Hash extensions.
83
86
  def self.load_hash
84
87
  clean_hash_compact
88
+ Lazier.load_object
85
89
  ::Hash.class_eval { include ::Lazier::Hash }
86
90
  end
87
91
 
88
- # Loads Hash method access extensions.
89
- def self.load_hash_method_access
90
- ::Hash.class_eval { include Hashie::Extensions::MethodAccess }
91
- end
92
-
93
92
  # Loads DateTime extensions.
94
93
  def self.load_datetime
95
94
  Lazier.load_object
@@ -117,14 +116,14 @@ module Lazier
117
116
  #
118
117
  # @param cls [Symbol|String|Object] If a `String` or a `Symbol` or a `Class`, then it will be the class to instantiate.
119
118
  # Otherwise the class of the object will returned.
120
- # @param scope [String] An additional scope to find the class. `%CLASS%`, `%`, `$`, `?` and `@` will be substituted with the class name.
121
- # @param only_in_scope [Boolean] If only try to instantiate the class in the scope.
119
+ # @param scope [String] The scope where to find the class. `%CLASS%`, `%`, `$`, `?` and `@` will be substituted with the class name.
120
+ # @param only_in_scope [Boolean] If only search inside the scope.
122
121
  # @return [Class] The found class.
123
- def self.find_class(cls, scope = "::%CLASS%", only_in_scope = false)
124
- if cls.is_a?(::String) || cls.is_a?(::Symbol)
125
- rv, cls = perform_initial_class_search(cls, only_in_scope)
126
- rv = search_class_inside_scope(rv, cls, scope) # Search inside scope
127
- rv || raise(NameError.new("", cls))
122
+ def self.find_class(cls, scope = "::@", only_in_scope = false)
123
+ if [::String, ::Symbol].include?(cls.class)
124
+ cls = cls.to_s.camelize
125
+ cls.gsub!(/^::/, "") if scope && only_in_scope
126
+ search_class(cls, scope) || raise(NameError, ["", cls])
128
127
  else
129
128
  cls.is_a?(::Class) ? cls : cls.class
130
129
  end
@@ -137,14 +136,30 @@ module Lazier
137
136
  # @param block [Proc] The block to evaluate.
138
137
  # @return [Float|String] If a `message` is provided, then the message itself plus the duration under parenthesis will be returned,
139
138
  # otherwise the duration alone as a number.
140
- def self.benchmark(message = nil, precision = 0, &block)
139
+ def self.benchmark(message: nil, precision: 0, &block)
141
140
  rv = Benchmark.ms(&block)
142
141
  message ? format("%s (%0.#{precision}f ms)", message, rv) : rv
143
142
  end
144
143
 
144
+ # Returns which platform are we running on. Can be `:java`, `:osx`, `:posix` or `:win32`
145
+ #
146
+ # @return [Boolean] If force detection again.
147
+ # @return [Symbol] The current platform.
148
+ def self.platform(force = false)
149
+ @platform = nil if force
150
+
151
+ @platform ||=
152
+ case RUBY_PLATFORM
153
+ when /cygwin|mingw|win32/ then :win32
154
+ when /java/ then :java
155
+ when /darwin/ then :osx
156
+ else :posix
157
+ end
158
+ end
159
+
145
160
  private
146
161
 
147
- # Removes existing `compact` and `compact!` methods from the Hash class.
162
+ # Removes existing `compact` and `compact!` methods from the Hash class to avoid conflicts with ActiveSupport.
148
163
  def self.clean_hash_compact
149
164
  ::Hash.class_eval do
150
165
  begin
@@ -156,39 +171,15 @@ module Lazier
156
171
  end
157
172
  end
158
173
 
159
- # Performs the initial search to find a class.
160
- # @param cls [Symbol|String|Object] If a `String` or a `Symbol` or a `Class`, then it will be the class to instantiate.
161
- # @param only_in_scope [Boolean] If only try to instantiate the class in the scope.
162
- # @return [Array] The found class (if any) and the sanitized name.
163
- def self.perform_initial_class_search(cls, only_in_scope)
164
- rv = nil
165
- cls = cls.to_s.camelize
166
-
167
- if only_in_scope
168
- cls.gsub!(/^::/, "") # Mark only search only inside scope
169
- else
170
- rv = search_class(cls) # Search outside scope
171
- end
172
-
173
- [rv, cls]
174
- end
175
-
176
174
  # Tries to search a class.
177
175
  #
178
176
  # @param cls [String] The class to search.
177
+ # @param scope [String] Scope to find the class. `%CLASS%`, `%`, `$`, `?` and `@` will be substituted with the class name.
179
178
  # @return [Class] The instantiated class.
180
- def self.search_class(cls)
181
- cls.constantize rescue nil
182
- end
183
-
184
- # Finds a class inside a specific scope.
185
- #
186
- # @param current [Class] The class found outside the scope.
187
- # @param cls [String] The class to search.
188
- # @param scope [String] The scope to search the class into.
189
- # @return [Class] The found class.
190
- def self.search_class_inside_scope(current, cls, scope)
191
- cls = cls.ensure_string
192
- !current && cls !~ /^::/ && scope.present? ? search_class(scope.to_s.gsub(/%CLASS%|[@%$?]/, cls)) : current
179
+ def self.search_class(cls, scope = nil)
180
+ cls = scope.gsub(/%CLASS%|[@%$?]/, cls)
181
+ cls.constantize
182
+ rescue
183
+ nil
193
184
  end
194
185
  end
@@ -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.
@@ -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,14 +6,11 @@
7
6
  module Lazier
8
7
  # A configuration class to set properties.
9
8
  class Configuration < Hashie::Dash
10
- include ::Lazier::I18n
11
-
12
9
  # Initializes a new configuration object.
13
10
  #
14
11
  # @param attributes [Hash] The initial values of properties of this configuration.
15
12
  def initialize(attributes = {}, &block)
16
- @lazier_i18n = Lazier::Localizer.new(:lazier, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/")).i18n
17
- i18n_setup(:lazier, ::File.absolute_path(::Pathname.new(::File.dirname(__FILE__)).to_s + "/../../locales/"))
13
+ @i18n = Lazier::I18n.instance
18
14
  super(attributes, &block)
19
15
  end
20
16
 
@@ -25,41 +21,43 @@ module Lazier
25
21
  # * :required - Specify the value as required for this property, to raise an error if a value is unset in a new or existing configuration.
26
22
  # * :readonly - Specify if the property is readonly, which means that it can only defined during creation of the configuration.
27
23
  #
28
- # @param property_name [String|Symbol] The new property name.
24
+ # @param name [String|Symbol] The new property name.
29
25
  # @param options [Hash] The options for the property.
30
- def self.property(property_name, options = {})
31
- super(property_name, options)
26
+ def self.property(name, options = {})
27
+ super(name, options)
32
28
 
33
29
  if options[:readonly]
34
- class_eval <<-ACCESSOR
35
- def #{property_name}=(_)
36
- raise ArgumentError.new(@lazier_i18n.configuration.readonly("#{property_name}", "#{name}"))
37
- end
38
- ACCESSOR
30
+ send(:define_method, "#{name}=") do |_|
31
+ assert_readonly_property!(name)
32
+ end
39
33
  end
40
34
  end
41
35
 
42
36
  private
43
37
 
44
- # Checks if a property exists.
45
- #
46
- # @param property [String|Symbol] The property to check.
47
- def assert_property_exists!(property)
48
- raise(ArgumentError, @lazier_i18n.configuration.not_found(property, self.class.name)) unless self.class.property?(property)
38
+ # :nodoc:
39
+ def assert_readonly_property!(name)
40
+ raise(ArgumentError, assertion_error("configuration.readonly", name))
49
41
  end
50
42
 
51
- # Checks if a property has been set.
52
- #
53
- # @param property [String|Symbol] The property to check.
54
- def assert_property_set!(property)
55
- raise(ArgumentError, @lazier_i18n.configuration.required(property, self.class.name)) if send(property).is_a?(NilClass)
43
+ # :nodoc:
44
+ def assert_property_exists!(name)
45
+ raise(ArgumentError, assertion_error("configuration.not_found", name)) unless self.class.property?(name)
56
46
  end
57
47
 
58
- # Checks if a property is required.
59
- #
60
- # @param property [String|Symbol] The property to check.
61
- def assert_property_required!(property, value)
62
- raise(ArgumentError, @lazier_i18n.configuration.required(property, self.class.name)) if self.class.required?(property) && value.is_a?(NilClass)
48
+ # :nodoc:
49
+ def assert_property_set!(name)
50
+ raise(ArgumentError, assertion_error("configuration.required", name)) if send(name).nil?
51
+ end
52
+
53
+ # :nodoc:
54
+ def assert_property_required!(name, value)
55
+ raise(ArgumentError, assertion_error("configuration.required", name)) if value.nil? && self.class.required?(name)
56
+ end
57
+
58
+ # :nodoc:
59
+ def assertion_error(label, name)
60
+ @i18n.translate(label, name: name, class: self.class.name)
63
61
  end
64
62
  end
65
63
  end
@@ -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.
@@ -51,66 +50,31 @@ module Lazier
51
50
  # @param reference [Fixnum] The ending (or middle, if `also_future` is `true`) value of the range. Defaults to the current year.
52
51
  # @param as_objects [Boolean] If to return years in hashes with `:value` and `label` keys.
53
52
  # @return [Array] A range of years. Every entry is
54
- def years(offset = 10, also_future = true, reference = nil, as_objects = false)
53
+ def years(offset: 10, also_future: true, reference: nil, as_objects: false)
55
54
  y = reference || ::Date.today.year
56
55
  (y - offset..(also_future ? y + offset : y)).map { |year| as_objects ? {value: year, label: year} : year }
57
56
  end
58
57
 
59
- # Returns all the availabe timezones.
60
- #
61
- # @return [Array]All the zone available.
62
- def timezones
63
- ::ActiveSupport::TimeZone.all
64
- end
65
-
66
- # Returns a list of names of all timezones.
67
- #
68
- # @param with_dst [Boolean] If include DST version of the zones.
69
- # @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
70
- # @return [Array] A list of names of timezones.
71
- def list_timezones(with_dst = true, dst_label = nil)
72
- ::ActiveSupport::TimeZone.list_all(with_dst, dst_label)
73
- end
74
-
75
- # Find a zone by its name.
76
- #
77
- # @param name [String] The zone name.
78
- # @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
79
- # @return [TimeZone] A timezone or `nil` if no zone was found.
80
- def find_timezone(name = true, dst_label = nil)
81
- ::ActiveSupport::TimeZone.find(name, dst_label)
82
- end
83
-
84
- # Returns a string representation of a timezone.
85
- #
86
- # ```ruby
87
- # DateTime.parameterize_zone(ActiveSupport::TimeZone["Pacific Time (US & Canada)"])
88
- # # => "-0800@pacific-time-us-canada"
89
- # ```
90
- # @param tz [TimeZone] The zone to represent.
91
- # @param with_offset [Boolean] If to include offset into the representation.
92
- # @return [String] A string representation which can be used for searches.
93
- def parameterize_zone(tz, with_offset = true)
94
- ::ActiveSupport::TimeZone.parameterize_zone(tz, with_offset)
95
- end
96
-
97
- # Finds a parameterized timezone.
98
- # @see DateTime#parameterize_zone
58
+ # Lookups a custom datetime format.
59
+ # @see Settings#setup_date_formats
99
60
  #
100
- # @param tz [String] The zone to unparameterize.
101
- # @param as_string [Boolean] If return just the zone name.
102
- # @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
103
- # @return [String|TimeZone] The found timezone or `nil` if the zone is not valid.
104
- def unparameterize_zone(tz, as_string = false, dst_label = nil)
105
- ::ActiveSupport::TimeZone.unparameterize_zone(tz, as_string, dst_label)
61
+ # @param key [Symbol] The name of the format to search.
62
+ # @return [String] The format or the name itself (if the format has not been found).
63
+ def custom_format(key)
64
+ ::Lazier.settings.date_formats.fetch(key, key)
106
65
  end
107
66
 
108
- # Returns an offset in rational value.
67
+ # Checks if the date is valid against to a specific format.
68
+ # @see DateTime#custom_format
109
69
  #
110
- # @param offset [Fixnum] The offset to convert.
111
- # @return [Rational] The converted offset.
112
- def rationalize_offset(offset)
113
- ::ActiveSupport::TimeZone.rationalize_offset(offset)
70
+ # @param value [String] The value to check.
71
+ # @param format [String] The format to check the value against.
72
+ # @return [Boolean] `true` if the value is valid against the format, `false` otherwise.
73
+ def valid?(value, format = "%F %T")
74
+ ::DateTime.strptime(value, custom_format(format))
75
+ true
76
+ rescue
77
+ false
114
78
  end
115
79
 
116
80
  # Returns the Easter (according to Gregorian calendar) date for the year.
@@ -119,7 +83,7 @@ module Lazier
119
83
  # @param year [Fixnum] The year to compute the date for. Defaults to the current year.
120
84
  # @return [Date] The Easter date for the year.
121
85
  def easter(year = nil)
122
- year = ::Date.today.year unless year.is_integer?
86
+ year = ::Date.today.year unless year.integer?
123
87
 
124
88
  # Compute using Anonymous Gregorian Algorithm: http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm
125
89
  data = easter_start(year)
@@ -131,42 +95,14 @@ module Lazier
131
95
  ::Date.civil(year, month, day)
132
96
  end
133
97
 
134
- # Lookups a custom datetime format.
135
- # @see Settings#setup_date_formats
136
- #
137
- # @param key [Symbol] The name of the format to search.
138
- # @return [String] The format or the name itself (if the format has not been found).
139
- def custom_format(key)
140
- ::Lazier.settings.date_formats.fetch(key, key)
141
- end
142
-
143
- # Checks if the date is valid against to a specific format.
144
- # @see DateTime#custom_format
145
- #
146
- # @param value [String] The value to check.
147
- # @param format [String] The format to check the value against.
148
- # @return [Boolean] `true` if the value is valid against the format, `false` otherwise.
149
- def valid?(value, format = "%F %T")
150
- ::DateTime.strptime(value, custom_format(format))
151
- true
152
- rescue
153
- false
154
- end
155
- alias_method :is_valid?, :valid?
156
-
157
98
  private
158
99
 
159
- # Part one of Easter calculation.
160
- #
161
- # @param year [Fixnum] The year to compute the date for.
162
- # @return [Array] Partial variables for #easter_divide.
100
+ # :nodoc:
163
101
  def easter_start(year)
164
102
  [year % 19, (year / 100.0).floor, year % 100]
165
103
  end
166
104
 
167
- # Part two of Easter calculation.
168
- # @param data [Fixnum] Partial variables from #easter_start.
169
- # @return [Array] Partial variables for #easter_aggregate.
105
+ # :nodoc:
170
106
  def easter_divide(data)
171
107
  _, b, c = data
172
108
 
@@ -178,10 +114,7 @@ module Lazier
178
114
  ]
179
115
  end
180
116
 
181
- # Part three of Easter calculation.
182
- #
183
- # @param data [Fixnum] Partial variables from #easter_divide.
184
- # @return [Array] Partial variables for #easter_prepare.
117
+ # :nodoc:
185
118
  def easter_aggregate(year, data)
186
119
  a = year % 19
187
120
  x, e, i, k = data
@@ -189,32 +122,20 @@ module Lazier
189
122
  [h, (32 + (2 * e) + (2 * i) - h - k) % 7]
190
123
  end
191
124
 
192
- # Part four of Easter calculation
193
- # @param data [Arrays] Partial variables from #easter_aggregate.
194
- # @return [Array] Partial variables for #easter_end.
125
+ # :nodoc:
195
126
  def easter_prepare(year, data)
196
127
  a = year % 19
197
128
  h, l = data
198
129
  [h, l, ((a + (11 * h) + (22 * l)) / 451.0).floor]
199
130
  end
200
131
 
201
- # Final part of Easter calculation.
202
- #
203
- # @param data [Fixnum] Variable from #easter_prepare.
204
- # @return [Array] Day and month of Easter day.
132
+ # :nodoc:
205
133
  def easter_end(data)
206
134
  h, l, m = data
207
135
  [((h + l - (7 * m) + 114) % 31) + 1, ((h + l - (7 * m) + 114) / 31.0).floor]
208
136
  end
209
137
  end
210
138
 
211
- # Returns the UTC::Time representation of the current datetime.
212
- #
213
- # @return [UTC::Time] The UTC::Time representation of the current datetime.
214
- def utc_time
215
- utc.to_time
216
- end
217
-
218
139
  # Returns the number of months passed between the beginning of the base year and the current date.
219
140
  #
220
141
  # ```ruby
@@ -224,7 +145,7 @@ module Lazier
224
145
  #
225
146
  # @param base [DateTime] The base year to start computation from. Default to current year.
226
147
  # @return [Fixnum] Returns the number of months passed between the beginning of the base year and the current date.
227
- def in_months(base = nil)
148
+ def months_since_year(base = nil)
228
149
  (year - (base || ::Date.today.year)) * 12 + month
229
150
  end
230
151
 
@@ -235,41 +156,26 @@ module Lazier
235
156
  month.indexize
236
157
  end
237
158
 
238
- # Formats a datetime, looking up also custom formats.
159
+ # Formats a datetime, eventually looking up also custom formats and/or moving to the current timezone.
239
160
  # @see Settings#setup_date_formats
240
161
  #
241
162
  # @param format [String] A format or a custom format name to use for formatting.
163
+ # @param custom [Boolean] Whether to use custom formats.
164
+ # @param change_time_zone [Boolean] Whether to move the date to the current timezone.
242
165
  # @return [String] The formatted date.
243
- def lstrftime(format = nil)
244
- strftime(::DateTime.custom_format(format.to_s).gsub(/(?<!%)(%[ab])/i) { |mo| localize_time_component(mo) })
245
- end
246
-
247
- # Formats a datetime in the current timezone.
248
- #
249
- # @param format [String] The format to use for formatting.
250
- # @return [String] The formatted date.
251
- def local_strftime(format = nil)
252
- (respond_to?(:in_time_zone) ? in_time_zone : self).strftime(::DateTime.custom_format(format))
253
- end
254
-
255
- # Formats a datetime in the current timezone, looking up also custom formats.
256
- # @see Settings#setup_date_formats
257
- #
258
- # @param format [String] A format or a custom format name.
259
- # @return [String] The formatted date.
260
- def local_lstrftime(format = nil)
261
- (respond_to?(:in_time_zone) ? in_time_zone : self).lstrftime(format)
166
+ def format(format = nil, custom: true, change_time_zone: false)
167
+ target = change_time_zone && respond_to?(:in_time_zone) ? in_time_zone : self
168
+ format = custom ? ::DateTime.custom_format(format.to_s).gsub(/(?<!%)(%[ab])/i) { |mo| localize_time_component(mo) } : format.to_s
169
+ target.strftime(format)
262
170
  end
263
171
 
264
172
  private
265
173
 
266
- # Returns a component of the date in the current locale.
267
- #
268
- # @param component [String] The component to localize.
269
- # @return [String] The localized component.
174
+ # :nodoc:
270
175
  def localize_time_component(component)
271
176
  type = {"%a" => :short_days, "%A" => :long_days, "%b" => :short_months, "%B" => :long_months}.fetch(component, "")
272
177
  index = component =~ /%a/i ? wday : month - 1
178
+
273
179
  ::Lazier.settings.date_names[type][index]
274
180
  end
275
181
  end