class Selector::Or

The composition of several conditions. Requires any of them to be satisfied

@example (see [])

Public Class Methods

new(*attributes) click to toggle source

@private

Calls superclass method Selector::Condition::new
# File lib/selector/or.rb, line 12
def self.new(*attributes)
  attrs = attributes.uniq - [NOTHING]

  return NOTHING     if attrs.empty?
  return attrs.first if attrs.count.equal?(1)
  return ANYTHING    if attrs.include? ANYTHING
  return ANYTHING    if (attrs & attrs.map(&:!)).any?

  super(*attrs)
end

Public Instance Methods

[](value) click to toggle source

Checks if value satisfies any of composed conditions

@example
  left  = Selector.new only: /foo/
  right = Selector.new only: /bar/
  composition = Selector::Or.new(left, right)

  composition[:foo] # => true
  composition[:bar] # => true
  composition[:baz] # => false

@param (see Selector::Composition#[])

@return (see Selector::Composition#[])

# File lib/selector/or.rb, line 38
def [](value)
  attributes.detect { |part| part[value] } ? true : false
end
|(other) click to toggle source

Adds the other condition to the composition (avoids nesting)

@param (see Selector::Composition#|)

@return (see Selector::Composition#|)

# File lib/selector/or.rb, line 48
def |(other)
  Or.new(*attributes, other)
end