Class: SensuCli::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu-cli/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_data) ⇒ Filter

Returns a new instance of Filter.



5
6
7
# File 'lib/sensu-cli/filter.rb', line 5

def initialize(filter_data)
  filter_split(filter_data)
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



3
4
5
# File 'lib/sensu-cli/filter.rb', line 3

def filter
  @filter
end

Instance Method Details

#filter_split(filter) ⇒ Object



9
10
11
# File 'lib/sensu-cli/filter.rb', line 9

def filter_split(filter)
  @filter = filter.sub(' ', '').split(',')
end

#inspect_hash(data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sensu-cli/filter.rb', line 17

def inspect_hash(data)
  data.any? do |key, value|
    if value.is_a?(Array)
      match?(value) if key == filter[0]
    elsif value.is_a?(Hash)
      process(value)
    else
      match?(value) if key == filter[0]
    end
  end
end

#match?(data) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/sensu-cli/filter.rb', line 13

def match?(data)
  data.to_s.include? filter[1]
end

#process(data) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/sensu-cli/filter.rb', line 29

def process(data)
  if data.is_a?(Array)
    data.select do |value|
      process(value)
    end
  elsif data.is_a?(Hash)
    inspect_hash(data)
  end
end