Module: Lite::Command::Utils

Defined in:
lib/lite/command/utils.rb

Class Method Summary collapse

Class Method Details

.call(object, argument) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/lite/command/utils.rb', line 32

def call(object, argument)
  if argument.is_a?(Symbol) || argument.is_a?(String)
    object.send(argument)
  elsif argument.is_a?(Proc)
    object.instance_eval(&argument)
  else
    argument
  end
end

.descendant_of?(object, other) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/lite/command/utils.rb', line 19

def descendant_of?(object, other)
  object_class = object.respond_to?(:new) ? object : object.class
  other_class = other.respond_to?(:new) ? other : other.class

  !!(object_class <= other_class)
end

.evaluate(object, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/lite/command/utils.rb', line 42

def evaluate(object, options = {})
  if options[:if]
    call(object, options[:if])
  elsif options[:unless]
    !call(object, options[:unless])
  else
    options.fetch(:default, true)
  end
end

.monotonic_timeObject



9
10
11
# File 'lib/lite/command/utils.rb', line 9

def monotonic_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

.pretty_exception(exception) ⇒ Object



13
14
15
16
17
# File 'lib/lite/command/utils.rb', line 13

def pretty_exception(exception)
  return if exception.nil?

  "[#{exception.class.name}] #{exception.message}".chomp(".")
end

.try(object, method_name, *args, include_private: true) ⇒ Object



26
27
28
29
30
# File 'lib/lite/command/utils.rb', line 26

def try(object, method_name, *args, include_private: true)
  return unless object.respond_to?(method_name, include_private)

  object.send(method_name, *args)
end