Class: EightBall::Conditions::Range

Inherits:
Base
  • Object
show all
Defined in:
lib/eight_ball/conditions/range.rb

Overview

The Range Condition describes a range of acceptable values by specifying a minimum and a maximum. These can be strings, integers, etc. but mixing data types will probably give you unexpected results.

Instance Attribute Summary collapse

Attributes inherited from Base

#parameter

Instance Method Summary collapse

Methods inherited from Base

#==, #hash

Constructor Details

#initialize(options = {}) ⇒ Range

Creates a new instance of a Range Condition.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :min (Object)

    The minimum acceptable value (inclusive).

  • :max (Object)

    The maximum acceptable value (inclusive).

  • :parameter (String)

    The name of the parameter this Condition was created for (eg. “account_id”). This value is only used by calling classes as a way to know what to pass into #satisfied?.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/eight_ball/conditions/range.rb', line 20

def initialize(options = {})
  options ||= {}

  raise ArgumentError, 'Missing value for min' if options[:min].nil?
  raise ArgumentError, 'Missing value for max' if options[:max].nil?

  @min = options[:min]

  raise ArgumentError, 'Max must be greater or equal to min' if options[:max] < min

  @max = options[:max]

  self.parameter = options[:parameter]
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



8
9
10
# File 'lib/eight_ball/conditions/range.rb', line 8

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



8
9
10
# File 'lib/eight_ball/conditions/range.rb', line 8

def min
  @min
end

Instance Method Details

#satisfied?(value) ⇒ Boolean

Examples:

Using integers

condition = new EightBall::Conditions::List.new min: 1, max: 300
condition.satisfied? 1 => true
condition.satisfied? 301 => false

Using strings

condition = new EightBall::Conditions::List.new min: 'a', max: 'm'
condition.satisfied? 'a' => true
condition.satisfied? 'z' => false

Returns:

  • (Boolean)


44
45
46
# File 'lib/eight_ball/conditions/range.rb', line 44

def satisfied?(value)
  value >= min && value <= max
end