Class: IcingaApi::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/icinga_api/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field = nil, operator = nil, value = nil) ⇒ Filter

Returns a new instance of Filter.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/icinga_api/filter.rb', line 7

def initialize(field=nil, operator=nil, value=nil)
  @multi = false
  if field.class == Hash
    # only for internal use
    @data = field
    @multi = true
  else
    # normal use
    @data = { type: "atom", field: [ field ], method: [ operator ], value:  [ value ] }
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/icinga_api/filter.rb', line 5

def data
  @data
end

Instance Method Details

#&(other) ⇒ Object



20
21
22
23
# File 'lib/icinga_api/filter.rb', line 20

def &(other)
  other = Marshal.load(Marshal.dump(other)) 
  self.class.new ({ type: 'AND', field: [@data, other.data] })
end

#to_jsonObject



32
33
34
35
36
# File 'lib/icinga_api/filter.rb', line 32

def to_json
  # icinga always wants a AND or OR construct, the hell knows why
  return (self & self).to_json unless @multi
  @data.to_json
end

#|(other) ⇒ Object



26
27
28
29
# File 'lib/icinga_api/filter.rb', line 26

def |(other)
  other = Marshal.load(Marshal.dump(other)) 
  self.class.new ({ type: 'OR', field: [@data, other.data] })
end