useful_utilities 5.4.5 → 5.4.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e5e4bbec43984ce6756ade1190a5f21e4dfbaf5e
4
- data.tar.gz: 236d91abef2bedcfaaab5e49c7c78cc998a5eedd
3
+ metadata.gz: 79bd418ad8787d74738d6fd2b99030e6c698ee1c
4
+ data.tar.gz: 1f227b9d219366903e323a70b043bf951eb12f6a
5
5
  SHA512:
6
- metadata.gz: 79972ec7576ea4d937a16a113cdf10082c7bad7a0b415b46a6adf56ead972caf76c3edc17286e6640c84b726fc48884726ba276c9647f20358b97b04a4cc94d7
7
- data.tar.gz: 35b254e47c6638587ae79491a8eb013cbc39ea5fd5503a668f7847588eee3a8d2af74d32538e7fd52a0bb9f21236b0daf5048f73d4480ac74a4fdc1c92a58cb2
6
+ metadata.gz: f09b24ef2b0714e6d53638984f206f04a89335fbdc37df399ae65ee66307b2094684629c0ded231edbd46947a705fcd4a0d3931ee9071a5943e1931c29069905
7
+ data.tar.gz: 490c74b2afb5ac686e72f145739a29e8af0a5fc6dce80fb38985a3e4880cb8b5b65bcffa3def2626e1be001ed190ab44550b09a7aff3a3ae72a7c2ef378b94d2
@@ -2,16 +2,25 @@ require 'socket'
2
2
  require 'timeout'
3
3
 
4
4
  module UsefulUtilities
5
+ # API utilities
5
6
  module Api
6
7
  extend self
7
8
 
9
+ # @note Exceptions to handle
10
+ # @see #port_open?
8
11
  RESCUE_PORT_OPEN_EXCEPTIONS = [Errno::ENETUNREACH, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError, Timeout::Error].freeze
9
12
 
13
+ # @param value [Numeric]
14
+ # @return [FalseClass/Numeric] false if value is infinity or value if not
10
15
  def convert_limit(value)
11
16
  value == Float::INFINITY ? false : value
12
17
  end
13
18
 
14
- # checks if a port is open or not on a remote host
19
+ # @param ip [String]
20
+ # @param port [Integer]
21
+ # @option sleep_time [Integer] :sleep_time (1)
22
+ # @option max_attempts [Integer] :max_attempts (3)
23
+ # @return [Boolean] check if a port is open or not on a remote host
15
24
  def port_open?(ip, port, sleep_time: 1, max_attempts: 3)
16
25
  try_to(max_attempts: max_attempts, sleep_time: sleep_time, rescue_what: RESCUE_PORT_OPEN_EXCEPTIONS) do
17
26
  Timeout::timeout(sleep_time) do
@@ -2,8 +2,14 @@ require 'active_record'
2
2
  require 'active_support/all'
3
3
 
4
4
  module UsefulUtilities
5
+ # ActiveRecord utilities
5
6
  module AR
7
+
8
+ # Includable AR extension
9
+ # To use it include this module to your model
6
10
  module Methods
11
+ # @param association_class [Class]
12
+ # @return [String] foreign key
7
13
  def foreign_key(association_class)
8
14
  reflections.values.find do |reflection|
9
15
  reflection.class_name == association_class.name
@@ -18,6 +24,8 @@ module UsefulUtilities
18
24
  TYPE_SUFFIX = '%s_type'
19
25
  ID_SUFFIX = '%s_id'
20
26
 
27
+ # @param value
28
+ # @return [Integer] value as Integer
21
29
  def value_to_integer(value)
22
30
  return 0 if value.nil?
23
31
  return 0 if value == false
@@ -26,27 +34,41 @@ module UsefulUtilities
26
34
  ActiveRecord::Type::Integer.new.type_cast_from_database(value)
27
35
  end
28
36
 
37
+ # @param value
38
+ # @return [BigDecimal] value as BigDecimal
29
39
  def value_to_decimal(value)
30
40
  return BigDecimal.new(0) if value.nil?
31
41
 
32
42
  ActiveRecord::Type::Decimal.new.type_cast_from_database(value)
33
43
  end
34
44
 
45
+ # @param value
46
+ # @return [Boolean] value as boolean
35
47
  def value_to_boolean(value)
36
48
  return false if value.nil?
37
49
 
38
50
  ActiveRecord::Type::Boolean.new.type_cast_from_database(value)
39
51
  end
40
52
 
53
+ # @param value
54
+ # @return [Float] value as float
55
+ # @example
56
+ # UsefulUtilities::AR.boolean_to_float(false) #=> 0.0
57
+ # UsefulUtilities::AR.boolean_to_float(true) #=> 1.0
41
58
  def boolean_to_float(value)
42
59
  value ? 1.0 : 0.0
43
60
  end
44
61
 
62
+ # @param record
63
+ # Validates record and associations
45
64
  def deep_validation(record)
46
65
  record.valid?
47
66
  nested_associations_validation(record)
48
67
  end
49
68
 
69
+ # @param record
70
+ # @param nested [Boolean]
71
+ # Validates nested associations of a record
50
72
  def nested_associations_validation(record, nested = false)
51
73
  record.class.reflections.each do |name, reflection|
52
74
  next unless NESTED_ASSOCIATIONS.include?(reflection.macro)
@@ -63,12 +85,19 @@ module UsefulUtilities
63
85
  end
64
86
  end
65
87
 
88
+ # @param scope [ActiveRecord::Relation]
89
+ # @param key [Symbol]
90
+ # @param value value to search
91
+ # @return [ActiveRecord::Relation]
66
92
  def by_polymorphic(scope, key, value)
67
93
  type = to_polymorphic_type(value)
68
94
  type_column, id_column = TYPE_SUFFIX % key, ID_SUFFIX % key
69
95
  scope.where(type_column => type, id_column => value)
70
96
  end
71
97
 
98
+ # @param value
99
+ # @return value if value does not respond to base_class
100
+ # @return [Class] base class
72
101
  def to_polymorphic_type(value)
73
102
  klass =
74
103
  if value.is_a?(Class) || value.nil? then value
@@ -79,23 +108,37 @@ module UsefulUtilities
79
108
  klass.respond_to?(:base_class) ? klass.base_class : klass
80
109
  end
81
110
 
111
+ # @param klass [Class]
112
+ # @param columns list of columns
113
+ # @return [String] qualified columns splitted by comma
82
114
  def [](klass, *columns)
83
115
  columns.map { |column| "#{ klass.table_name }.#{ column }" }.join(', ')
84
116
  end
85
117
 
118
+ # @param klass [Class]
119
+ # @param column [String/Symbol]
120
+ # @return [String] ASC statement for ORDER BY
86
121
  def asc(klass, column)
87
122
  "#{ self[klass, column] } ASC"
88
123
  end
89
124
 
125
+ # @param klass [Class]
126
+ # @param column [String/Symbol]
127
+ # @return [String] DESC statement for ORDER BY
90
128
  def desc(klass, column)
91
129
  "#{ self[klass, column] } DESC"
92
130
  end
93
131
 
132
+ # @param scope [ActiveRecord::Relation]
133
+ # @param _value class or column
134
+ # @return [ActiveRecord::Relation]
94
135
  def older_than(scope, _value)
95
136
  value = _value.is_a?(ActiveRecord::Base) ? _value.id : _value
96
137
  value.present? ? scope.where("#{ self[scope, :id] } < ?", value) : scope
97
138
  end
98
139
 
140
+ # @param args list of columns
141
+ # @return COALESCE statement
99
142
  def null_to_zero(*args)
100
143
  sql_column =
101
144
  case args.size
@@ -108,14 +151,21 @@ module UsefulUtilities
108
151
  "COALESCE(#{ sql_column }, 0)"
109
152
  end
110
153
 
154
+ # @param value
155
+ # @return CEILING statement
111
156
  def ceil(value)
112
157
  "CEILING(#{value})"
113
158
  end
114
159
 
160
+ # @param args
161
+ # @param result alias
162
+ # @return SUM statement
115
163
  def sql_sum(*args, result)
116
164
  "SUM(#{ null_to_zero(*args) }) AS #{ result }"
117
165
  end
118
166
 
167
+ # @param scope [ActiveRecord::Relation]
168
+ # @param all_columns list of columns
119
169
  # Can be used for not NULL columns only
120
170
  # sum_by_columns(Virtual, :column_1, :column2, column3: :column3_alias...)
121
171
  def sum_by_columns(scope, *all_columns)
@@ -123,16 +173,29 @@ module UsefulUtilities
123
173
  columns_with_aliases = columns.extract_options!
124
174
 
125
175
  sql_query =
126
- columns.reduce(columns_with_aliases) { |res, column| res.merge!(column => column) }.
127
- map { |column, column_alias| "SUM(#{ column }) AS #{ column_alias }" }.join(', ')
176
+ columns.reduce(columns_with_aliases) do |res, column|
177
+ res.merge!(column => column)
178
+ end.
179
+ map do |column, column_alias|
180
+ "COALESCE(SUM(#{ column }), 0) AS #{ column_alias }"
181
+ end.
182
+ join(', ')
128
183
 
129
184
  scope.select(sql_query).first
130
185
  end
131
186
 
132
- def count(table, column, result)
133
- "COUNT(#{ self[table, column] }) AS #{ result }"
187
+ # @param klass [Class]
188
+ # @param column [String/Symbol]
189
+ # @param result [String/Symbol] alias
190
+ # @return [String] count statement
191
+ def count(klass, column, result)
192
+ "COUNT(#{ self[klass, column] }) AS #{ result }"
134
193
  end
135
194
 
195
+ # @param owner
196
+ # @param self_class [Class]
197
+ # @param dependent_class [Class]
198
+ # Deletes dependent records
136
199
  def delete_dependents(owner, self_class, dependent_class)
137
200
  dependent_fk = dependent_class.foreign_key(self_class)
138
201
  self_fk = self_class.foreign_key(owner.class)
@@ -1,4 +1,5 @@
1
1
  module UsefulUtilities
2
+ # CentOS versions utilities
2
3
  class CentOSVersion
3
4
  attr_reader :version
4
5
 
@@ -1,4 +1,5 @@
1
1
  module UsefulUtilities
2
+ # Hash utilities
2
3
  module Hash
3
4
  extend self
4
5
 
@@ -1,7 +1,10 @@
1
1
  module UsefulUtilities
2
+ # I18n utilities
2
3
  module I18n
3
4
  extend self
4
5
 
6
+ # @param object [Object]
7
+ # @return [String] humanized name
5
8
  def humanize_type(object)
6
9
  klass = case object
7
10
  when Class then object
@@ -1,4 +1,5 @@
1
1
  module UsefulUtilities
2
+ # Numeric utilities
2
3
  module Numeric
3
4
  extend self
4
5
 
@@ -7,21 +8,33 @@ module UsefulUtilities
7
8
  MILLION = 1_000_000
8
9
  BILLION = 1_000_000_000
9
10
 
11
+ # @param value [Numeric]
12
+ # @return [Numeric]
13
+ # @example
14
+ # UsefulUtilities::Numeric.positive_or_zero(1) #=> 1
15
+ # UsefulUtilities::Numeric.positive_or_zero(-1) #=> 0
10
16
  def positive_or_zero(value)
11
17
  (value > ZERO) ? value : ZERO
12
18
  end
13
19
 
20
+ # @param value [Numeric]
21
+ # @return [Numeric] value if value can not be coerced to integer
14
22
  def float_or_integer(value)
15
23
  value == value.to_i ? value.to_i : value
16
24
  end
17
25
 
26
+ # @param value [Numeric]
27
+ # @option scale [Integer] :scale (nil)
28
+ # @return [BigDecimal] value as BigDecimale rounded to scale
18
29
  def to_decimal(value, scale: nil)
19
30
  result = value.to_f.to_d
20
31
 
21
32
  scale ? result.round(scale) : result
22
33
  end
23
34
 
24
- # @note: used SI metric prefixes http://en.wikipedia.org/wiki/SI_prefix
35
+ # @param value [Numeric]
36
+ # @param unit [Symbol]
37
+ # @return [Numeric] value converted to kilo
25
38
  def to_kilo(value, unit)
26
39
  if unit == :M then value * THOUSAND
27
40
  elsif unit == :G then value * MILLION
@@ -29,6 +42,9 @@ module UsefulUtilities
29
42
  end
30
43
  end
31
44
 
45
+ # @param value [Numeric]
46
+ # @param unit [Symbol]
47
+ # @return [Numeric] value converted to giga
32
48
  def to_giga(value, unit)
33
49
  if unit == :k then value.fdiv(MILLION)
34
50
  elsif unit == :M then value.fdiv(THOUSAND)
@@ -36,6 +52,10 @@ module UsefulUtilities
36
52
  end
37
53
  end
38
54
 
55
+
56
+ # @param value [Numeric]
57
+ # @param unit [Symbol]
58
+ # @return [Numeric] value converted to number
39
59
  def to_number(value, unit)
40
60
  if unit == :M then value * MILLION
41
61
  elsif unit == :G then value * BILLION
@@ -1,4 +1,5 @@
1
1
  module UsefulUtilities
2
+ # Redhat releases utilities
2
3
  class RedhatRelease
3
4
  LEGACY_DISTRO_TEMPLATE = 'centos%{major_version}'.freeze
4
5
 
@@ -2,35 +2,57 @@ require_relative 'standard/decimal'
2
2
 
3
3
  module UsefulUtilities
4
4
  module Size
5
+ # Possible units:
6
+ # :bit - bits
7
+ # :kbit - kilobits
8
+ # :Mbit - megabits
9
+ # :Gbit - gigabits
10
+ # :Tbit - terabits
11
+ # Used SI standard http://en.wikipedia.org/wiki/Binary_prefix
12
+ # Decimal
13
+ # 1 K = 1000
5
14
  module Bit
6
- # @note: used SI standard http://en.wikipedia.org/wiki/Binary_prefix
7
- # Decimal
8
- # 1 K = 1000
9
15
  include UsefulUtilities::Size::Standard::Decimal
10
16
 
17
+ # @param size [Numeric]
18
+ # @param unit [Symbol]
19
+ # @return [Numeric] size in terabits
11
20
  def to_terabits(size, unit)
12
21
  to_tera(size, bit_prefix(unit))
13
22
  end
14
23
 
24
+ # @param size [Numeric]
25
+ # @param unit [Symbol]
26
+ # @return [Numeric] size in gigabits
15
27
  def to_gigabits(size, unit)
16
28
  to_giga(size, bit_prefix(unit))
17
29
  end
18
30
 
31
+ # @param size [Numeric]
32
+ # @param unit [Symbol]
33
+ # @return [Numeric] size in megabits
19
34
  def to_megabits(size, unit)
20
35
  to_mega(size, bit_prefix(unit))
21
36
  end
22
37
 
38
+ # @param size [Numeric]
39
+ # @param unit [Symbol]
40
+ # @return [Numeric] size in kilobits
23
41
  def to_kilobits(size, unit)
24
42
  to_kilo(size, bit_prefix(unit))
25
43
  end
26
44
 
45
+ # @param size [Numeric]
46
+ # @param unit [Symbol]
47
+ # @return [Numeric] size in bits
27
48
  def to_bits(size, unit)
28
49
  to_decimal_bi(size, bit_prefix(unit))
29
50
  end
30
51
 
31
- # Convert bits to manually defined format
32
- # by using parameter 'unit'
33
- # ATTENTION: by default round is eq to 3 digits
52
+ # @param size [Numeric]
53
+ # @param unit [Symbol]
54
+ # @param rounder [Integer]
55
+ # @return [Numeric] humanized size in provided unit
34
56
  def bits_to_human_size(size, unit, rounder = 3)
35
57
  case unit
36
58
  when :bit then size.round(rounder)
@@ -42,8 +64,9 @@ module UsefulUtilities
42
64
  end
43
65
  end
44
66
 
45
- # Ignore any fractional part of the number
46
- # Bit can be only integer !!!
67
+ # @param value [Integer]
68
+ # @return [Symbol] suitable unit
69
+ # @raise [ArgumentError] if value is not integer or less than zero
47
70
  def suitable_unit_for_bits(value)
48
71
  return not_integer!(value) unless value.is_a?(Integer)
49
72
  return not_positive_integer!(value) unless value > -1
@@ -56,6 +79,8 @@ module UsefulUtilities
56
79
  end
57
80
  end
58
81
 
82
+ # @param value_in_bits [Integer] value in bits
83
+ # @return [String] human readable value
59
84
  def bits_per_sec_to_human_readable(value_in_bits)
60
85
  unit = suitable_unit_for_bits(value_in_bits)
61
86
  value = bits_to_human_size(value_in_bits, unit.to_sym)
@@ -2,26 +2,44 @@ require_relative 'standard/binary'
2
2
 
3
3
  module UsefulUtilities
4
4
  module Size
5
+ # Possible units:
6
+ # :B - bytes
7
+ # :KB - kilobytes
8
+ # :MB - megabytes
9
+ # :GB - gigabytes
10
+ # :TB - terabytes
11
+ # Used ISO standard http://en.wikipedia.org/wiki/Binary_prefix
12
+ # Binary
13
+ # 1 K = 1024
5
14
  module Byte
6
- # @note: used ISO standard http://en.wikipedia.org/wiki/Binary_prefix
7
- # Binary
8
- # 1 K = 1024
9
15
  include UsefulUtilities::Size::Standard::Binary
10
16
 
11
17
  HALF_OF_SECTOR = 0.5
12
18
 
19
+ # @param size [Numeric]
20
+ # @param unit [Symbol]
21
+ # @return [Numeric] size in terabytes
13
22
  def to_terabytes(size, unit)
14
23
  to_tebi(size, byte_prefix(unit))
15
24
  end
16
25
 
26
+ # @param size [Numeric]
27
+ # @param unit [Symbol]
28
+ # @return [Numeric] size in gigabytes
17
29
  def to_gigabytes(size, unit)
18
30
  to_gibi(size, byte_prefix(unit))
19
31
  end
20
32
 
33
+ # @param size [Numeric]
34
+ # @param unit [Symbol]
35
+ # @return [Numeric] size in megabytes
21
36
  def to_megabytes(size, unit)
22
37
  to_mebi(size, byte_prefix(unit))
23
38
  end
24
39
 
40
+ # @param size [Numeric]
41
+ # @param unit [Symbol]
42
+ # @return [Numeric] size in kilobytes
25
43
  def to_kilobytes(size, unit)
26
44
  if unit == :sector
27
45
  return (size * HALF_OF_SECTOR).round # http://en.wikipedia.org/wiki/Disk_sector
@@ -30,13 +48,17 @@ module UsefulUtilities
30
48
  to_kibi(size, byte_prefix(unit))
31
49
  end
32
50
 
51
+ # @param size [Numeric]
52
+ # @param unit [Symbol]
53
+ # @return [Numeric] size in bytes
33
54
  def to_bytes(size, unit)
34
55
  to_binary_bi(size, byte_prefix(unit))
35
56
  end
36
57
 
37
- # Convert bytes to manually defined format
38
- # by using parameter 'unit'
39
- # ATTENTION: by default round is eq to 3 digits
58
+ # @param size [Numeric]
59
+ # @param unit [Symbol]
60
+ # @param rounder [Integer]
61
+ # @return [Numeric] humanized size in provided unit
40
62
  def bytes_to_human_size(size, unit, rounder = 3)
41
63
  case unit
42
64
  when :B then size.round(rounder)
@@ -2,12 +2,21 @@ require_relative 'standard/decimal'
2
2
 
3
3
  module UsefulUtilities
4
4
  module Size
5
+ # Possible units:
6
+ # :bit - bits
7
+ # :kbit - kilobits
8
+ # :Mbit - megabits
9
+ # :Gbit - gigabits
10
+ # :Tbit - terabits
11
+ # Used SI standard http://en.wikipedia.org/wiki/Binary_prefix
12
+ # Decimal
13
+ # 1 K = 1000
5
14
  module CdnSpeed
6
- # @note: used SI standard http://en.wikipedia.org/wiki/Binary_prefix
7
- # Decimal
8
- # 1 K = 1000
9
15
  include UsefulUtilities::Size::Standard::Decimal
10
16
 
17
+ # @param speed [Numeric]
18
+ # @param unit [Symbol]
19
+ # @return [Numeric] size in CDN gigabits per second
11
20
  def to_cdn_gbps(speed, unit)
12
21
  to_giga(speed, bit_prefix(unit))/8
13
22
  end
@@ -2,16 +2,29 @@ require_relative 'standard/decimal'
2
2
 
3
3
  module UsefulUtilities
4
4
  module Size
5
+ # Frequency utilities
6
+ # Possible units:
7
+ # :Hz - hertz
8
+ # :KHz - kilohertz
9
+ # :MHz - megahertz
10
+ # :GHz - gigahertz
11
+ # :THz - terahertz
12
+ # Used SI standard http://en.wikipedia.org/wiki/Binary_prefix
13
+ # Decimal
14
+ # 1 K = 1000
5
15
  module Frequency
6
- # @note: used SI standard http://en.wikipedia.org/wiki/Binary_prefix
7
- # Decimal
8
- # 1 K = 1000
9
16
  include UsefulUtilities::Size::Standard::Decimal
10
17
 
18
+ # @param size [Numeric]
19
+ # @param unit [Symbol]
20
+ # @return [Numeric] size in megahertz
11
21
  def to_megahertz(size, unit)
12
22
  to_mega(size, frequency_prefix(unit))
13
23
  end
14
24
 
25
+ # @param size [Numeric]
26
+ # @param unit [Symbol]
27
+ # @return [Numeric] size in gigahertz
15
28
  def to_gigahertz(size, unit)
16
29
  to_giga(size, frequency_prefix(unit))
17
30
  end
@@ -1,12 +1,17 @@
1
1
  module UsefulUtilities
2
2
  module Size
3
+ # Percentage utilities
3
4
  module Percentage
4
5
  HUNDRED = 100
5
6
 
7
+ # @param fraction [Numeric]
8
+ # @return [Numeric] fraction converted to percentage
6
9
  def fraction_to_percentage(fraction)
7
10
  fraction * HUNDRED
8
11
  end
9
12
 
13
+ # @param percentage [Numeric]
14
+ # @return [Float] percentage converted to fraction
10
15
  def percentage_to_fraction(percentage)
11
16
  percentage.fdiv(HUNDRED)
12
17
  end
@@ -1,14 +1,23 @@
1
1
  module UsefulUtilities
2
2
  module Size
3
3
  module Standard
4
+ # Possible prefixes:
5
+ # :B - bytes
6
+ # :KiB - kibibytes
7
+ # :MiB - mebibytes
8
+ # :GiB - gibibytes
9
+ # :TiB - tebibytes
10
+ # Used ISO standard http://en.wikipedia.org/wiki/Binary_prefix
4
11
  module Binary
5
- # @note: used ISO standard http://en.wikipedia.org/wiki/Binary_prefix
6
12
 
7
13
  KIBI = 1024 # KiB
8
14
  MEBI = KIBI ** 2 # MiB
9
15
  GIBI = KIBI ** 3 # GiB
10
16
  TEBI = KIBI ** 4 # TiB
11
17
 
18
+ # @param val [Numeric]
19
+ # @param prefix [Symbol]
20
+ # @return [Numeric] val in tebibytes
12
21
  def to_tebi(val, prefix)
13
22
  case prefix
14
23
  when :B then val.fdiv(TEBI)
@@ -20,6 +29,9 @@ module UsefulUtilities
20
29
  end
21
30
  end
22
31
 
32
+ # @param val [Numeric]
33
+ # @param prefix [Symbol]
34
+ # @return [Numeric] val in gibibytes
23
35
  def to_gibi(val, prefix)
24
36
  case prefix
25
37
  when :B then val.fdiv(GIBI)
@@ -31,6 +43,9 @@ module UsefulUtilities
31
43
  end
32
44
  end
33
45
 
46
+ # @param val [Numeric]
47
+ # @param prefix [Symbol]
48
+ # @return [Numeric] val in mebibytes
34
49
  def to_mebi(val, prefix)
35
50
  case prefix
36
51
  when :B then val.fdiv(MEBI)
@@ -42,6 +57,9 @@ module UsefulUtilities
42
57
  end
43
58
  end
44
59
 
60
+ # @param val [Numeric]
61
+ # @param prefix [Symbol]
62
+ # @return [Numeric] val in kibibytes
45
63
  def to_kibi(val, prefix)
46
64
  case prefix
47
65
  when :B then val.fdiv(KIBI)
@@ -53,6 +71,9 @@ module UsefulUtilities
53
71
  end
54
72
  end
55
73
 
74
+ # @param val [Numeric]
75
+ # @param prefix [Symbol]
76
+ # @return [Numeric] val in bytes
56
77
  def to_binary_bi(val, prefix)
57
78
  case prefix
58
79
  when :B then val
@@ -1,8 +1,8 @@
1
1
  module UsefulUtilities
2
2
  module Size
3
3
  module Standard
4
+ # Used SI standard http://en.wikipedia.org/wiki/Binary_prefix
4
5
  module Decimal
5
- # @note: used SI standard http://en.wikipedia.org/wiki/Binary_prefix
6
6
 
7
7
  KILO = 1000 # KB
8
8
  MEGA = KILO ** 2 # MB
@@ -1,40 +1,66 @@
1
1
  require 'active_support/all'
2
2
 
3
3
  module UsefulUtilities
4
+ # Utilities for time
5
+
6
+ # @author OnApp Ltd.
4
7
  module Time
5
8
  extend self
6
9
 
7
10
  SECONDS_IN_HOUR = 3_600
8
11
  MILLISECONDS_IN_SECOND = 1_000
9
12
 
10
- # Whatever time is passed it will return it without any GMT offset and the passed time will be in UTC
11
- # t = Time.now #=> 2013-12-19 14:01:22 +0200
12
- # UsefulUtilities::Time.assume_utc(t) #=> 2013-12-19 14:01:22 UTC
13
+ # @param time [Time]
14
+ # @return [DateTime] time in UTC
15
+ # @example
16
+ # time #=> 2013-12-19 14:01:22 +0200
17
+ #
18
+ # UsefulUtilities::Time.assume_utc(time) #=> 2013-12-19 14:01:22 UTC
13
19
  def assume_utc(time)
14
20
  time.to_datetime.change(offset: 0)
15
21
  end
16
22
 
17
- # Examples of usage:
18
- # 2014-01-04 14:29:59 #=> 2014-01-04 14:00:00
19
- # 2014-01-04 14:30:00 #=> 2014-01-04 15:00:00
23
+ # @param time [Time]
24
+ # @return [Time] time rounded to hours
25
+ # @example
26
+ # time #=> 2014-01-04 14:29:59
27
+ #
28
+ # UsefulUtilities::Time.round_to_hours(time) #=> 2014-01-04 14:00:00
20
29
  def round_to_hours(time)
21
30
  (time.min < 30) ? time.beginning_of_hour : time.end_of_hour
22
31
  end
23
32
 
33
+ # @param time [Time]
34
+ # @return [Time] beginning of the next hour
35
+ # @example
36
+ # time #=> 2018-03-07 15:51:58 +0200
37
+ # UsefulUtilities::Time.beginning_of_next_hour(time) #=> 2018-03-07 16:00:00 +0200
24
38
  def beginning_of_next_hour(time)
25
39
  time.beginning_of_hour.in(1.hour)
26
40
  end
27
41
 
42
+ # @param time [Time]
43
+ # @return [Time] beginning of the next day
44
+ # @example
45
+ # time #=> 2018-03-07 15:51:58 +0200
46
+ # UsefulUtilities::Time.beginning_of_next_day(time) #=> 2018-03-08 00:00:00 +0200
28
47
  def beginning_of_next_day(time)
29
48
  time.beginning_of_day.in(1.day)
30
49
  end
31
50
 
51
+ # @param time [Time]
52
+ # @return [Time] beginning of the next month
53
+ # @example
54
+ # time #=> 2018-03-07 15:51:58 +0200
55
+ # UsefulUtilities::Time.beginning_of_next_month(time) #=> 2018-04-01 00:00:00 +0300
32
56
  def beginning_of_next_month(time)
33
57
  time.beginning_of_month.in(1.month)
34
58
  end
35
59
 
36
- # yields local time of the beginning of each hour
37
- # between start time & current time
60
+ # @param from [Time]
61
+ # @param till [Time]
62
+ # Yields local time of the beginning of each hour
63
+ # between from & till
38
64
  def each_hour_from(from, till = ::Time.now)
39
65
  cursor = from.dup.utc.beginning_of_hour
40
66
  end_time = till.dup.utc.beginning_of_hour
@@ -46,6 +72,10 @@ module UsefulUtilities
46
72
  end
47
73
  end
48
74
 
75
+ # @param from [Time]
76
+ # @param till [Time]
77
+ # Yields local time of the beginning of each day
78
+ # between from & till
49
79
  def each_day_from(from, till = ::Time.now)
50
80
  cursor = from.beginning_of_day
51
81
  end_time = till.beginning_of_day
@@ -57,9 +87,19 @@ module UsefulUtilities
57
87
  end
58
88
  end
59
89
 
90
+ # @param time_string [String] string to parse from
91
+ # @param time_zone [String] timezone
92
+ # @return [DateTime] parsed time in UTC
93
+ #
60
94
  # Parse and convert Time to the given time zone. If time zone is
61
95
  # not given, Time is assumed to be given in UTC. Returns result in
62
96
  # UTC
97
+ #
98
+ # @example
99
+ # time_string #=> "2018-03-07 15:51:58 +0200"
100
+ # time_zone #=> "EET"
101
+ #
102
+ # UsefulUtilities::Time.to_utc(time_string, time_zone) #=> Wed, 07 Mar 2018 13:51:58 +0000
63
103
  def to_utc(time_string, time_zone)
64
104
  time = ::DateTime.parse(time_string) # Wed, 22 Aug 2012 13:34:18 +0000
65
105
  rescue
@@ -74,15 +114,29 @@ module UsefulUtilities
74
114
  end
75
115
  end
76
116
 
117
+ # @param time [Time]
118
+ # @return [Integer] time in milliseconds
119
+ # @example
120
+ # time #=> 2018-03-07 15:51:58 +0200
121
+ # UsefulUtilities::Time.to_milliseconds(time) #=> 1520430718000
77
122
  def to_milliseconds(time)
78
123
  time.to_time.to_i * MILLISECONDS_IN_SECOND
79
124
  end
80
125
 
81
- # takes two objects: Time
126
+ # @param end_time [Time] end time
127
+ # @param start_time [Time] start time
128
+ # @return [Integer] difference in hours
129
+ # @example
130
+ # end_time #=> 2018-03-07 14:51:58 +0200
131
+ # start_time #=> 2018-03-07 15:51:58 +0200
132
+ #
133
+ # UsefulUtilities::Time.diff_in_hours(end_time, start_time) #=> 1
82
134
  def diff_in_hours(end_time, start_time)
83
135
  ((end_time.to_i - start_time.to_i) / SECONDS_IN_HOUR).abs
84
136
  end
85
137
 
138
+ # @param date [Date]
139
+ # @return [Boolean] checks if date is valid
86
140
  def valid_date?(date)
87
141
  return true if date.acts_like?(:date)
88
142
  return false if date.blank?
@@ -1,3 +1,3 @@
1
1
  module UsefulUtilities
2
- VERSION = '5.4.5'.freeze
2
+ VERSION = '5.4.6'.freeze
3
3
  end
@@ -1,5 +1,5 @@
1
- # It was used in outdated rake task, but we decided to leave for the future
2
1
  module UsefulUtilities
2
+ # YAML utilities
3
3
  module YAML
4
4
  extend self
5
5
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: useful_utilities
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.4.5
4
+ version: 5.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - OnApp Ltd.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-09 00:00:00.000000000 Z
11
+ date: 2018-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -101,7 +101,6 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - README.md
105
104
  - Rakefile
106
105
  - lib/useful_utilities.rb
107
106
  - lib/useful_utilities/api.rb
data/README.md DELETED
@@ -1,14 +0,0 @@
1
- ## UsefulUtilities
2
-
3
- A collection of useful utilities
4
-
5
- This package include next tools with new namespaces:
6
-
7
- Api Utilities => UsefulUtilities::Api
8
- AR Utilities => UsefulUtilities::AR
9
- Hash Utilities => UsefulUtilities::Hash
10
- Size Utilities => UsefulUtilities::Size
11
- Numeric Utilities => UsefulUtilities::Numeric
12
- Time Utilities => UsefulUtilities::Time
13
- YAML Utilities => UsefulUtilities::YAML
14
- I18n Utilities => UsefulUtilities::I18n