class Blufin::Routes
Public Class Methods
at_least_one_flag(opts)
click to toggle source
Will throw an error if no flags are set. @param Array (@opts) @return void
# File lib/core/routes.rb, line 45 def self.at_least_one_flag(opts) all_flags = [] opts.each_key do |key| unless key.to_s =~ /_given\z/i || key == :version || key == :help all_flags << key.to_s end end if flags_set(opts) <= 0 error_text = ['Available flags are:', nil] all_flags.each do |flag| error_text << " \x1B[38;5;244m--#{convert_underscore_to_hypen(flag.to_s)}\x1B[0m" end Blufin::Terminal::error("You must set #{Blufin::Terminal::format_highlight('atleast one')} flag", error_text) end end
code_already_ran(tmp_key, timeout = 1000)
click to toggle source
Prevents code from running twice. @return boolean
# File lib/core/routes.rb, line 77 def self.code_already_ran(tmp_key, timeout = 1000) t = Time.new epoch = t.strftime('%s%3N') epoch_previous = nil tmp_file = "/tmp/#{tmp_key}-last-run-epoch.txt" if Blufin::Files::file_exists(tmp_file) Blufin::Files::read_file(tmp_file).each do |line| epoch_previous = line break end end Blufin::Files::write_file(tmp_file, [epoch]) return if epoch_previous.nil? diff = (epoch.to_i - epoch_previous.to_i).abs diff < timeout end
flags_set(opts)
click to toggle source
Returns number of flags set for route (from @opts variable) @param Array (@opts) @return int
# File lib/core/routes.rb, line 8 def self.flags_set(opts) flags_set = 0 opts.each do |key, value| # Only here to prevent code-inspector from complaining. key.strip! if key.nil? if value flags_set = flags_set + 1 end end if flags_set > 0 flags_set = flags_set / 2 end flags_set end
max_one_flag(opts)
click to toggle source
Will throw an error if more than one flag is set. @param Array (@opts) @return void
# File lib/core/routes.rb, line 26 def self.max_one_flag(opts) all_flags = [] opts.each_key do |key| unless key.to_s =~ /_given\z/i || key == :version || key == :help all_flags << key.to_s end end if flags_set(opts) >= 2 error_text = ['Please set 1 one of the following flags:', nil] all_flags.each do |flag| error_text << " \x1B[38;5;244m--#{convert_underscore_to_hypen(flag)}\x1B[0m" end Blufin::Terminal::error("You can set #{Blufin::Terminal::format_highlight('only one')} flag at a time", error_text) end end
only_one_of(opts, possible_flags = [])
click to toggle source
Will throw an error if more than 1 of the possible flags is set. Having NONE of the flags set is OK. @return void
# File lib/core/routes.rb, line 64 def self.only_one_of(opts, possible_flags = []) raise RuntimeError, "Expected Array, instead got: #{possible_flags.class}" unless possible_flags.is_a?(Array) raise RuntimeError, 'possible_flags cannot be an empty Array.' unless possible_flags.any? flags_set = 0 possible_flags.each { |possible_flag| raise RuntimeError, "@opts does not contain flag: #{possible_flag}" unless opts.has_key?(possible_flag) flags_set += 1 if opts[possible_flag] && !opts[possible_flag].nil? } Blufin::Terminal::error("Cannot set #{Blufin::Terminal::format_highlight('more than one')} of the following flags: \x1B[38;5;172m#{possible_flags.join(', ')}\x1B[0m") if flags_set > 1 end
Private Class Methods
convert_underscore_to_hypen(flag)
click to toggle source
Does exactly what it says on the Tin. @return string
# File lib/core/routes.rb, line 98 def self.convert_underscore_to_hypen(flag) flag.gsub('_', '-') end