Module: Squared::Common::Utils
- Defined in:
- lib/squared/common/utils.rb
Class Method Summary collapse
- .env(key, default = nil, suffix: nil, strict: false, equals: nil, ignore: nil) ⇒ Object
- .env_bool(key, default = false, suffix: nil, strict: false, index: false) ⇒ Object
- .env_match(key, default = nil, suffix: nil, strict: false, options: 0, timeout: nil) ⇒ Object
- .env_pipe(key, default = 1, suffix: nil, strict: false, root: nil) ⇒ Object
- .env_value(key, default = '', suffix: nil, strict: false) ⇒ Object
- .split_escape(val, char: ',') ⇒ Object
- .split_option(val) ⇒ Object
- .task_invoke(*cmd, args: [], exception: true, warning: true) ⇒ Object
- .task_invoked?(*args) ⇒ Boolean
- .task_join(*val) ⇒ Object
- .time_format(epoch, clock: false, pass: []) ⇒ Object
- .time_offset(val = nil) ⇒ Object
- .time_since(val, ms: true) ⇒ Object
Class Method Details
.env(key, default = nil, suffix: nil, strict: false, equals: nil, ignore: nil) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/squared/common/utils.rb', line 110 def env(key, default = nil, suffix: nil, strict: false, equals: nil, ignore: nil) ret = env_value(key, suffix: suffix, strict: strict) return ret == equals.to_s unless equals.nil? ret.empty? || (ignore && as_a(ignore).any? { |val| val.to_s == ret }) ? default : ret end |
.env_bool(key, default = false, suffix: nil, strict: false, index: false) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/squared/common/utils.rb', line 132 def env_bool(key, default = false, suffix: nil, strict: false, index: false) case key when ::NilClass default when ::String case (val = env_value(key, suffix: suffix, strict: strict)) when '' default when '0', 'false' false else index && val.to_i > 0 ? val.to_i : true end else key end end |
.env_match(key, default = nil, suffix: nil, strict: false, options: 0, timeout: nil) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/squared/common/utils.rb', line 172 def env_match(key, default = nil, suffix: nil, strict: false, options: 0, timeout: nil) case (val = env_value(key, suffix: suffix, strict: strict)) when '' default when '0' false when '1' true else Regexp.new(val, , timeout: timeout) end end |
.env_pipe(key, default = 1, suffix: nil, strict: false, root: nil) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/squared/common/utils.rb', line 150 def env_pipe(key, default = 1, suffix: nil, strict: false, root: nil) case key when ::String case (ret = env_value(key, suffix: suffix, strict: strict)) when '0', '1', '2' return ret.to_i end when ::Numeric return key if key.between?(0, 2) end begin if default.is_a?(::String) default = (root ? Pathname.new(root) + default : Pathname.new(default)).realdirpath end rescue StandardError => e warn e 1 else default end end |
.env_value(key, default = '', suffix: nil, strict: false) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/squared/common/utils.rb', line 117 def env_value(key, default = '', suffix: nil, strict: false) if suffix if (ret = ENV["#{key + (@envname ? "_#{@envname}" : '')}_#{suffix}"]) return ret elsif strict return default end end if @envname return ret if (ret = ENV["#{key}_#{@envname}"]) return default if strict end ENV.fetch(key, default) end |
.split_escape(val, char: ',') ⇒ Object
12 13 14 |
# File 'lib/squared/common/utils.rb', line 12 def split_escape(val, char: ',') val.split(/\s*(?<!\\)#{char}\s*/) end |
.split_option(val) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/squared/common/utils.rb', line 16 def split_option(val) val = val.strip return [val, '', ''] unless (i = val.index('=')) last = val[i + 1..-1].strip quote = '' if last =~ /\A(["'])(.+)\1\z/ last = $2 quote = $1 end [val[0..i - 1], last, quote] end |
.task_invoke(*cmd, args: [], exception: true, warning: true) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/squared/common/utils.rb', line 29 def task_invoke(*cmd, args: [], exception: true, warning: true) cmd.each { |name| Rake::Task[name].invoke(*args) } rescue StandardError => e raise if exception warn e if warning end |
.task_invoked?(*args) ⇒ Boolean
48 49 50 51 52 53 54 |
# File 'lib/squared/common/utils.rb', line 48 def task_invoked?(*args) Rake::Task.tasks.any? do |obj| next unless obj.already_invoked args.any? { |val| val.is_a?(::Regexp) ? obj.name.match?(val) : val == obj.name } end end |
.task_join(*val) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/squared/common/utils.rb', line 37 def task_join(*val) case val.size when 1 val[0].to_s when 2 "#{val[0]}:#{val[1]}" else val.join(':') end end |
.time_format(epoch, clock: false, pass: []) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/squared/common/utils.rb', line 56 def time_format(epoch, clock: false, pass: []) ss = 1000 mm = 60 * ss hh = 60 * mm dd = 24 * hh hm = pass.include?('s') time = [] if !clock && (d = epoch / dd) > 0 time << "#{d}d" epoch -= d * dd end if (h = epoch / hh) > 0 time << (clock ? h.to_s : "#{h}h") epoch -= h * hh end if (m = epoch / mm) > 0 time << (clock ? m.to_s.rjust(2, '0') : "#{m}m") epoch -= m * mm elsif clock time << '00' end unless hm if (s = epoch / ss) > 0 time << (clock ? s.to_s.rjust(2, '0') : "#{s}s") epoch -= s * ss elsif clock time << '00' end end if clock time.join(':') else time << "#{epoch}ms" unless hm || pass.include?('ms') time.join(' ') end end |
.time_offset(val = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/squared/common/utils.rb', line 93 def time_offset(val = nil) val = DateTime.parse(val) if val.is_a?(::String) cur = DateTime.now ret = 0 if (r = /^([+-])(\d+):(\d+):(\d+)$/.match((val || cur).strftime('%::z'))) ret += (r[1] == '+' ? -1 : 1) * ((r[2].to_i * 60 * 60) + (r[3].to_i * 60) + r[4].to_i) * 1000 end return ret unless val (cur.strftime('%Q').to_i + time_offset) - (val.strftime('%Q').to_i + ret) end |
.time_since(val, ms: true) ⇒ Object
105 106 107 108 |
# File 'lib/squared/common/utils.rb', line 105 def time_since(val, ms: true) s = ms ? '%s%L' : '%s' Time.now.utc.strftime(s).to_i - Time.parse(val).utc.strftime(s).to_i end |