Module: EnsureIt

Defined in:
lib/ensure_it/patch.rb,
lib/ensure_it/config.rb,
lib/ensure_it/errors.rb,
lib/ensure_it/version.rb,
lib/ensure_it/ensure_hash.rb,
lib/ensure_it/ensure_array.rb,
lib/ensure_it/ensure_class.rb,
lib/ensure_it/ensure_float.rb,
lib/ensure_it/ensure_string.rb,
lib/ensure_it/ensure_symbol.rb,
lib/ensure_it/ensure_integer.rb,
lib/ensure_it/ensure_instance_of.rb

Defined Under Namespace

Modules: Config Classes: Error, ErrorMessage

Constant Summary collapse

VERSION =
'0.1.3'
FLOAT_REGEXP =
/\A[+\-]?(?:\d+(?:\.\d*)?|\d*\.\d+)(?:[eE][+\-]?\d+)?\z/
OCT_REGEXP =
/\A0\d+\z/
INT_REGEXP =
/\A[+\-]?\d[\d_]*\z/
HEX_REGEXP =
/\A0x[0-9a-zA-Z]+\z/
BIN_REGEXP =
/\A0b[01]+\z/

Class Method Summary collapse

Class Method Details

.configObject



15
16
17
# File 'lib/ensure_it/config.rb', line 15

def self.config
  Config
end

.configure {|Config| ... } ⇒ Object

Yields:



19
20
21
22
# File 'lib/ensure_it/config.rb', line 19

def self.configure
  yield(Config) if block_given?
  Config
end

.ensure_float_error_options(**opts) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/ensure_it/ensure_float.rb', line 98

def self.ensure_float_error_options(**opts)
  unless opts.key?(:message)
    opts[:message] = '#{subject} should be a float or be able' \
                     ' to convert to it'
  end
  opts
end

.ensure_integer_error_options(**opts) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/ensure_it/ensure_integer.rb', line 153

def self.ensure_integer_error_options(**opts)
  unless opts.key?(:message)
    opts[:message] = '#{subject} should be an integer or be able' \
                     ' to convert to it'
  end
  opts
end

.ensure_string_error_options(**opts) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ensure_it/ensure_string.rb', line 80

def self.ensure_string_error_options(**opts)
  unless opts.key?(opts[:message])
    opts[:message] =
      if opts[:numbers] == true
        '#{subject} should be a String, Symbol, Numeric or Rational'
      else
        '#{subject} should be a String or a Symbol'
      end
  end
  opts
end

.ensure_symbol_error_options(**opts) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ensure_it/ensure_symbol.rb', line 59

def self.ensure_symbol_error_options(**opts)
  unless opts.key?(opts[:message])
    opts[:message] = '#{subject} should be' +
      if opts[:values].is_a?(Array)
        " one of #{opts[:values]}"
      else
        ' a Symbol or a String'
      end
  end
  opts
end

.raise_error(method_name, message: nil, error: Error, **opts) ⇒ Object

Raises:

  • (error)


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ensure_it/errors.rb', line 62

def self.raise_error(method_name, message: nil, error: Error, **opts)
  error = Error unless error <= Exception
  error_msg = ErrorMessage.new(method_name, message, caller[1..-1])
  # save message in backtrace in variables to not call getter
  # methods of error_msg instance in raise call
  error_message = error_msg.message
  error_backtrace = error_msg.backtrace
  if opts[:smart] == true || EnsureIt.config.errors == :smart
    inspect_source(error_msg, **opts)
    activate_smart_errors(error_msg, **opts)
  end
  raise error, error_message, error_backtrace
end