Class: SleepingKingStudios::Tools::CoreTools

Inherits:
Base
  • Object
show all
Defined in:
lib/sleeping_king_studios/tools/core_tools.rb

Overview

Tools for working with an application or working environment.

Defined Under Namespace

Classes: DeprecationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

instance

Constructor Details

#initialize(deprecation_caller_depth: nil, deprecation_strategy: nil) ⇒ CoreTools

Returns a new instance of CoreTools.

Parameters:

  • deprecation_caller_depth (Integer) (defaults to: nil)

    The number of backtrace lines to display when outputting a deprecation warning.

  • deprecation_strategy (String) (defaults to: nil)

    The name of the strategy used when deprecated code is called. Must be ‘ignore’, ‘raise’, or ‘warn’.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sleeping_king_studios/tools/core_tools.rb', line 23

def initialize(
  deprecation_caller_depth: nil,
  deprecation_strategy:     nil
)
  super()

  @deprecation_caller_depth =
    deprecation_caller_depth ||
    ENV.fetch('DEPRECATION_CALLER_DEPTH', '3').to_i
  @deprecation_strategy =
    deprecation_strategy || ENV.fetch('DEPRECATION_STRATEGY', 'warn')
end

Instance Attribute Details

#deprecation_caller_depthInteger (readonly)

Returns the number of backtrace lines to display when outputting a deprecation warning.

Returns:

  • (Integer)

    the number of backtrace lines to display when outputting a deprecation warning.



38
39
40
# File 'lib/sleeping_king_studios/tools/core_tools.rb', line 38

def deprecation_caller_depth
  @deprecation_caller_depth
end

#deprecation_strategyString (readonly)

Returns the current deprecation strategy.

Returns:

  • (String)

    the current deprecation strategy.



41
42
43
# File 'lib/sleeping_king_studios/tools/core_tools.rb', line 41

def deprecation_strategy
  @deprecation_strategy
end

Instance Method Details

#deprecate(name, message: nil) ⇒ Object #deprecate(*args, format: , message: nil) ⇒ Object

Overloads:

  • #deprecate(name, message: nil) ⇒ Object

    Prints a deprecation warning.

    Parameters:

    • name (String)

      The name of the object, method, or feature that has been deprecated.

    • message (String) (defaults to: nil)

      An optional message to print after the formatted string. Defaults to nil.

  • #deprecate(*args, format: , message: nil) ⇒ Object

    Prints a deprecation warning with the specified format.

    Parameters:

    • args (Array)

      The arguments to pass into the format string.

    • format (String) (defaults to: )

      The format string.

    • message (String) (defaults to: nil)

      An optional message to print after the formatted string. Defaults to nil.



58
59
60
61
62
63
64
65
# File 'lib/sleeping_king_studios/tools/core_tools.rb', line 58

def deprecate(*args, format: nil, message: nil)
  send(
    :"deprecate_as_#{deprecation_strategy}",
    *args,
    format:  format,
    message: message
  )
end

#empty_bindingBinding

Generates an empty Binding object with an Object as the receiver.

Returns:

  • (Binding)

    The empty binding object.



70
71
72
# File 'lib/sleeping_king_studios/tools/core_tools.rb', line 70

def empty_binding
  Object.new.instance_exec { binding }
end

#require_each(*file_patterns) ⇒ Object

Expands each file pattern and requires each file.

Parameters:

  • file_patterns (Array)

    The files to require.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sleeping_king_studios/tools/core_tools.rb', line 77

def require_each(*file_patterns)
  file_patterns.each do |file_pattern|
    if file_pattern.include?('*')
      Dir[file_pattern].each do |file_name|
        Kernel.require file_name
      end
    else
      Kernel.require file_pattern
    end
  end
end