Sha256: 47e3b11624d6d0e856c3eadee66a5662e70538d49af77f8025d6022ee948810c

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Lite
  module Command
    module Utils

      module_function

      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

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

        object.send(method_name, *args)
      end

      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

      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

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lite-command-3.1.2 lib/lite/command/utils.rb
lite-command-3.1.1 lib/lite/command/utils.rb
lite-command-3.1.0 lib/lite/command/utils.rb