Class: Selector::Condition

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/selector/condition.rb

Overview

Describe an immutable condition for selecting values

Direct Known Subclasses

And, Anything, Collection, Function, Not, Nothing, Or, Regexp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*attributes) ⇒ Condition

Returns a new instance of Condition.



26
27
28
29
# File 'lib/selector/condition.rb', line 26

def initialize(*attributes)
  @attributes = attributes
  IceNine.deep_freeze(self)
end

Instance Attribute Details

#attributesArray (readonly)

Returns The array of initialized attributes.

Returns:

  • (Array)

    The array of initialized attributes



15
16
17
# File 'lib/selector/condition.rb', line 15

def attributes
  @attributes
end

Instance Method Details

#!Selector::Condition

Inverts the condition

Returns:



59
60
61
# File 'lib/selector/condition.rb', line 59

def !
  Not.new(self)
end

#&(other) ⇒ Selector::Condition

Composes (by AND) the condition to the other condition

Parameters:

Returns:



69
70
71
# File 'lib/selector/condition.rb', line 69

def &(other)
  And.new(self, other)
end

#-(other) ⇒ Selector::Condition

Composes (by AND) the condition to inversion of the other condition

This is the same as ‘&(!other)`

Parameters:

Returns:



81
82
83
# File 'lib/selector/condition.rb', line 81

def -(other)
  And.new(self, !other)
end

#==(other) ⇒ Boolean

Compares the condition to the other object by type and attributes

Parameters:

  • other (Object)

Returns:

  • (Boolean)


37
38
39
# File 'lib/selector/condition.rb', line 37

def ==(other)
  other.instance_of?(self.class) && attributes.eql?(other.attributes)
end

#[](value) ⇒ Boolean

This method is abstract.

Checks if the value satisfies the condtion

Parameters:

  • value (Object)

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

    by default



51
52
53
# File 'lib/selector/condition.rb', line 51

def [](_value)
  fail NotImplementedError.new "#{self.class}#[] not implemented"
end

#attributeObject

The first attribute

Returns:

  • (Object)


21
22
23
# File 'lib/selector/condition.rb', line 21

def attribute
  attributes.first
end

#|(other) ⇒ Selector::Condition

Composes (by OR) the condition to the other condition

This is the same as ‘!((!self)&(!other))`

Parameters:

Returns:



93
94
95
# File 'lib/selector/condition.rb', line 93

def |(other)
  Or.new(self, other)
end