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
108 109 110 111 112 113 |
# File 'lib/squared/common/utils.rb', line 108 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/squared/common/utils.rb', line 130 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
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/squared/common/utils.rb', line 168 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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/squared/common/utils.rb', line 148 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 return default unless default.is_a?(::String) begin (root ? Pathname.new(root) + default : Pathname.new(default)).realdirpath rescue StandardError => e warn e 1 end end |
.env_value(key, default = '', suffix: nil, strict: false) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/squared/common/utils.rb', line 115 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 |
# File 'lib/squared/common/utils.rb', line 48 def task_invoked?(*args) Rake::Task.tasks.any? do |obj| 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
54 55 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 |
# File 'lib/squared/common/utils.rb', line 54 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
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/squared/common/utils.rb', line 91 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
103 104 105 106 |
# File 'lib/squared/common/utils.rb', line 103 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 |