Module: Squared::Common::Utils

Defined in:
lib/squared/common/utils.rb

Class Method Summary collapse

Class Method Details

.as_a(obj, *meth, flat: nil, compact: false, &blk) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/squared/common/utils.rb', line 39

def as_a(obj, *meth, flat: nil, compact: false, &blk)
  return [] if obj.nil?

  unless obj.is_a?(::Array)
    obj = if obj.respond_to?(:to_a) && !obj.is_a?(::Hash) && (val = obj.to_a).is_a?(::Array)
            val
          elsif obj.respond_to?(:to_ary)
            obj.to_ary
          else
            [obj]
          end
  end
  obj = flat.is_a?(::Numeric) ? obj.flatten(flat) : obj.flatten if flat
  obj = obj.compact if compact
  obj = obj.map(&meth.shift) until meth.empty?
  return obj unless block_given?

  obj.select(&blk)
end

.env(key, default = nil, equals: nil, notequals: nil, ignore: nil, **kwargs, &blk) ⇒ Object



142
143
144
# File 'lib/squared/common/utils.rb', line 142

def env(key, default = nil, equals: nil, notequals: nil, ignore: nil, **kwargs, &blk)
  env_yield(env_value(key, **kwargs), default, equals: equals, notequals: notequals, ignore: ignore, &blk)
end

.env_bool(key, default = false, index: false, **kwargs) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/squared/common/utils.rb', line 150

def env_bool(key, default = false, index: false, **kwargs)
  case key
  when nil
    default
  when ::String
    case (val = env_value(key, **kwargs))
    when ''
      default
    when '0', 'false'
      false
    else
      index && val.to_i > 0 ? val.to_i : true
    end
  else
    key
  end
end

.env_key(*val) ⇒ Object



146
147
148
# File 'lib/squared/common/utils.rb', line 146

def env_key(*val)
  val.join('_').gsub(/\W+/, '_').upcase
end

.env_match(key, default = nil, options: 0, timeout: nil, **kwargs) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/squared/common/utils.rb', line 188

def env_match(key, default = nil, options: 0, timeout: nil, **kwargs)
  case (val = env_value(key, **kwargs))
  when ''
    default
  when '0'
    false
  when '1'
    true
  else
    Regexp.new(val, options, timeout: timeout)
  end
end

.env_pipe(key, default = 1, root: nil, **kwargs) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/squared/common/utils.rb', line 168

def env_pipe(key, default = 1, root: nil, **kwargs)
  case key
  when ::String
    case (ret = env_value(key, **kwargs))
    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 => e
    warn e
    1
  end
end

.split_escape(val, char: ',', limit: 0, &blk) ⇒ Object



59
60
61
62
63
64
# File 'lib/squared/common/utils.rb', line 59

def split_escape(val, char: ',', limit: 0, &blk)
  ret = val.split(/(?<!\\)#{char}/, limit).map(&:strip)
  return ret unless block_given?

  ret.each_with_index(&blk)
end

.task_invoke(*cmd, args: [], sync: true, exception: true, warning: true) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/squared/common/utils.rb', line 66

def task_invoke(*cmd, args: [], sync: true, exception: true, warning: true)
  cmd.each do |name|
    if sync
      Rake::Task[name].invoke(*args)
    else
      Rake.application.run_with_threads { Rake::Task[name].invoke(*args) }
    end
  rescue => e
    raise if exception

    warn e if warning
  end
end

.task_invoked?(*args) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/squared/common/utils.rb', line 91

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



80
81
82
83
84
85
86
87
88
89
# File 'lib/squared/common/utils.rb', line 80

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_epoch(val = Time.now, ms: true) ⇒ Object



138
139
140
# File 'lib/squared/common/utils.rb', line 138

def time_epoch(val = Time.now, ms: true)
  val.utc.strftime(ms ? '%s%L' : '%s').to_i
end

.time_format(epoch, clock: false, pass: []) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/squared/common/utils.rb', line 97

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_since(val, ms: true) ⇒ Object



134
135
136
# File 'lib/squared/common/utils.rb', line 134

def time_since(val, ms: true)
  time_epoch(ms: ms) - time_epoch(Time.parse(val), ms: ms)
end