Module: Linesman

Defined in:
lib/linesman.rb,
lib/linesman/version.rb

Constant Summary collapse

VERSION =
'0.0.2'

Class Method Summary collapse

Class Method Details

.within_bounds?(options = {}, &block) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/linesman.rb', line 2

def self.within_bounds?(options = {}, &block)
  at_least_one = [:exactly, :min, :max]
  unless at_least_one.map {|key| options.has_key?(key)}.any? {|present| present}
    raise ArgumentError.new('At least one of the following is required: :exactly, :min, :max')
  end

  raise ArgumentError.new('a block is required') if block.nil?

  exactly = options[:exactly]
  min = options[:min]
  max = options[:max]

  under_scrutiny = block.call

  if exactly
    return under_scrutiny == exactly
  elsif min && max
    return under_scrutiny >= min && under_scrutiny <= max
  elsif min
    return under_scrutiny >= min
  else
    return under_scrutiny <= max
  end
end