Class: EightBall::Conditions::List

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

Overview

The List Condition describes a list of acceptable values. These can be strings, integers, etc.

Instance Attribute Summary collapse

Attributes inherited from Base

#parameter

Instance Method Summary collapse

Methods inherited from Base

#==, #hash, #to_wire

Constructor Details

#initialize(options = {}) ⇒ List

Creates a new instance of a List Condition.

Parameters:

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

Options Hash (options):

  • :values (Array<String>, String)

    The list of acceptable values

  • :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?.

  • :coerce (Boolean)

    When true, compare values and the tested input as strings so that, eg., an integer list matches a string caller. Defaults to exact-type matching.



22
23
24
25
26
27
28
29
# File 'lib/eight_ball/conditions/list.rb', line 22

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

  @values = Array(options[:values])
  # Store a canonical true/nil so the wire never carries a stray non-boolean.
  @coerce = options[:coerce] ? true : nil
  self.parameter = options[:parameter]
end

Instance Attribute Details

#coerceObject (readonly)

Returns the value of attribute coerce.



7
8
9
# File 'lib/eight_ball/conditions/list.rb', line 7

def coerce
  @coerce
end

#valuesObject (readonly)

Returns the value of attribute values.



7
8
9
# File 'lib/eight_ball/conditions/list.rb', line 7

def values
  @values
end

Instance Method Details

#satisfied?(value) ⇒ Boolean

Examples:

condition = EightBall::Conditions::List.new(values: [1, 'a'])
condition.satisfied? 1 => true
condition.satisfied? 2 => false
condition.satisfied? 'a' => true

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
# File 'lib/eight_ball/conditions/list.rb', line 36

def satisfied?(value)
  if @coerce
    string_value = value.to_s
    return values.any? { |v| v.to_s == string_value }
  end

  values.include? value
end

#wire_fieldsObject



45
46
47
48
49
# File 'lib/eight_ball/conditions/list.rb', line 45

def wire_fields
  # coerce before parameter so the wire key order matches the eight-ball-ts
  # port (whose parameter is always emitted last), keeping the two byte-identical.
  %i[values coerce parameter]
end