Class: Blufin::Routes
- Inherits:
-
Object
- Object
- Blufin::Routes
- Defined in:
- lib/core/routes.rb
Class Method Summary collapse
-
.at_least_one_flag(opts) ⇒ Object
Will throw an error if no flags are set.
-
.code_already_ran(tmp_key, timeout = 1000) ⇒ Object
Prevents code from running twice.
-
.flags_set(opts) ⇒ Object
Returns number of flags set for route (from @opts variable).
-
.max_one_flag(opts) ⇒ Object
Will throw an error if more than one flag is set.
-
.only_one_of(opts, possible_flags = []) ⇒ Object
Will throw an error if more than 1 of the possible flags is set.
Class Method Details
.at_least_one_flag(opts) ⇒ Object
Will throw an error if no flags are set.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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) ⇒ Object
Prevents code from running twice.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
Returns number of flags set for route (from @opts variable)
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 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) ⇒ Object
Will throw an error if more than one flag is set.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 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 = []) ⇒ Object
Will throw an error if more than 1 of the possible flags is set. Having NONE of the flags set is OK.
64 65 66 67 68 69 70 71 72 73 |
# 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 |