Class: Selector::Regexp

Inherits:
Condition show all
Defined in:
lib/selector/regexp.rb

Overview

The condition checks if a value matches the regexp

Examples:

condition = Selector::Regexp.new /1/
condition[11] # => true
condition[22] # => false

Instance Attribute Summary

Attributes inherited from Condition

#attributes

Instance Method Summary collapse

Methods inherited from Condition

#!, #&, #-, #==, #attribute

Constructor Details

#initialize(regexp) ⇒ Regexp

Initializes the condition with the regexp

Parameters:

  • regexp (::Regexp)


16
17
18
# File 'lib/selector/regexp.rb', line 16

def initialize(_)
  super
end

Instance Method Details

#[](value) ⇒ Boolean

Checks if the stringified value matches the regexp

Examples:

condition = Selector::Regexp.new /1/
condition[11] # => true
condition[22] # => false

Parameters:

  • value (Object)

Returns:

  • (Boolean)


31
32
33
# File 'lib/selector/regexp.rb', line 31

def [](value)
  value.to_s[attribute] ? true : false
end

#|(other) ⇒ Object

Creates an OR composition

If other value is a regexp, then creates modified regexp to avoid nesting



43
44
45
46
# File 'lib/selector/regexp.rb', line 43

def |(other)
  return super unless other.instance_of? Regexp
  Regexp.new(/(#{attribute})|(#{other.attribute})/)
end