Module: Riteway

Defined in:
lib/riteway.rb,
lib/riteway/match.rb,
lib/riteway/rspec.rb,
lib/riteway/version.rb,
lib/riteway/minitest.rb

Defined Under Namespace

Modules: MinitestLifecycle, RSpecBridge

Constant Summary collapse

ADAPTER =
:minitest
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.assert(given:, should:, actual:, expected:) ⇒ Object

Raises:

  • (RSpec::Expectations::ExpectationNotMetError)


7
8
9
10
# File 'lib/riteway.rb', line 7

def self.assert(**)
  raise "Riteway.assert requires an adapter. " \
        "Add `require \"riteway/rspec\"` or `require \"riteway/minitest\"` to your test helper."
end

.attempt(callable = nil, *args, **kwargs, &block) ⇒ Object

Calls callable (or block) with given args. Returns the error if raised, otherwise returns the result. Catches StandardError and subclasses only — SystemExit, Interrupt, SignalException, etc. propagate normally.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/riteway.rb', line 15

def self.attempt(callable = nil, *args, **kwargs, &block)
  raise ArgumentError, "attempt accepts a callable or a block, not both" if callable && block

  fn = callable || block
  raise ArgumentError, "attempt requires a callable or a block" unless fn
  raise ArgumentError, "attempt expects a callable (responds to #call), got #{fn.class}" unless fn.respond_to?(:call)

  begin
    kwargs.empty? ? fn.call(*args) : fn.call(*args, **kwargs)
  rescue => error
    error
  end
end

.count_keys(hash = {}) ⇒ Object

Raises:

  • (TypeError)


29
30
31
32
33
# File 'lib/riteway.rb', line 29

def self.count_keys(hash = {})
  raise TypeError, "count_keys expects a Hash, got #{hash.class}" unless hash.is_a?(Hash)

  hash.keys.length
end

.match(text) ⇒ Object

Returns a lambda that searches text for a pattern (String or Regexp). Returns the matched text on success, or nil if no match — consistent with Ruby's String#match which also returns nil on no match.

Raises:

  • (TypeError)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/riteway/match.rb', line 7

def self.match(text)
  raise TypeError, "match expects a String, got #{text.class}" unless text.is_a?(String)

  ->(pattern) {
    unless pattern.is_a?(String) || pattern.is_a?(Regexp)
      raise TypeError,
            "pattern must be a String or Regexp, got #{pattern.class}"
    end
    raise ArgumentError, "pattern must not be empty" if pattern.is_a?(String) && pattern.empty?

    re = pattern.is_a?(String) ? Regexp.new(Regexp.escape(pattern)) : pattern
    matched = text.match(re)
    matched ? matched[0] : nil
  }
end