Class: Arspy::Operators::Selector::AttributeSelector

Inherits:
Base
  • Object
show all
Defined in:
lib/arspy/operators/selector/attribute_selector.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_value) ⇒ AttributeSelector

Returns a new instance of AttributeSelector.



9
10
11
12
13
# File 'lib/arspy/operators/selector/attribute_selector.rb', line 9

def initialize(key_value)
  super
  @attribute = key_value.first.to_sym
  @test_values = key_value.last.is_a?(Array) ? key_value.last : [key_value.last]
end

Class Method Details

.applies?(key_value) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/arspy/operators/selector/attribute_selector.rb', line 6

def self.applies?(key_value)
  key_value.first.is_a? Symbol
end

Instance Method Details

#attribute_testsObject



24
25
26
27
28
# File 'lib/arspy/operators/selector/attribute_selector.rb', line 24

def attribute_tests
  @attribute_tests ||= @test_values.map do |param|
    AttributeTest.for(param)
  end
end

#select?(obj) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/arspy/operators/selector/attribute_selector.rb', line 15

def select?(obj)
  raise "Attribute '#{@attribute}' not found for #{obj.class.name}" unless obj.attribute_names.include?(@attribute.to_s)
  response = obj.__send__ @attribute
  return nil unless response
  raise "Attributes resulting in arrays not supported" if response.is_a?(Array)
  response = [response] unless response.is_a?(Array)
  response.any?{|value_or_object| attribute_tests.any?{|test| test.match?(value_or_object)}}
end