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
- .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
91 92 93 94 95 96 |
# File 'lib/squared/common/utils.rb', line 91 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| ret == val.to_s }) ? default : ret end |
.env_bool(key, default = false, suffix: nil, strict: false, index: false) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/squared/common/utils.rb', line 113 def env_bool(key, default = false, suffix: nil, strict: false, index: false) if key.is_a?(::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.nil? ? default : key end end |
.env_match(key, default = nil, suffix: nil, strict: false, options: 0, timeout: nil) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/squared/common/utils.rb', line 149 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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/squared/common/utils.rb', line 128 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 >= 0 && key <= 2 end default end |
.env_value(key, default = '', suffix: nil, strict: false) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/squared/common/utils.rb', line 98 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*/o) end |
.task_invoke(*cmd, args: [], exception: true, warning: true) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/squared/common/utils.rb', line 15 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
34 35 36 37 38 39 40 |
# File 'lib/squared/common/utils.rb', line 34 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
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/squared/common/utils.rb', line 23 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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/squared/common/utils.rb', line 42 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
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/squared/common/utils.rb', line 79 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 |