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
Class Method Details
.env(key, default = nil, suffix: nil, strict: false, equals: nil, ignore: nil) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/squared/common/utils.rb', line 104 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/squared/common/utils.rb', line 126 def env_bool(key, default = false, suffix: nil, strict: false, index: false) case key when nil 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
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/squared/common/utils.rb', line 165 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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/squared/common/utils.rb', line 144 def env_pipe(key, default = 1, suffix: nil, strict: false, root: nil) if default.is_a?(::String) begin default = (root ? root.join(default) : Pathname.new(default)).realdirpath rescue StandardError => e default = 1 warn e end end 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 default end |
.env_value(key, default = '', suffix: nil, strict: false) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/squared/common/utils.rb', line 111 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
11 12 13 |
# File 'lib/squared/common/utils.rb', line 11 def split_escape(val, char: ',') val.split(/\s*(?<!\\)#{char}\s*/) end |
.split_option(val) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/squared/common/utils.rb', line 15 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
28 29 30 31 32 33 34 |
# File 'lib/squared/common/utils.rb', line 28 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
47 48 49 50 51 52 53 |
# File 'lib/squared/common/utils.rb', line 47 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
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/squared/common/utils.rb', line 36 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
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 90 |
# File 'lib/squared/common/utils.rb', line 55 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
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/squared/common/utils.rb', line 92 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 |