Class: Bronto::Filter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilter

Returns a new instance of Filter.



8
9
10
# File 'lib/bronto/filter.rb', line 8

def initialize
  self.fields = {}
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



6
7
8
# File 'lib/bronto/filter.rb', line 6

def fields
  @fields
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/bronto/filter.rb', line 6

def type
  @type
end

Instance Method Details

#add_filter(*args) ⇒ Object

Accepts two or three arguments:

1. Field name
2. (optional) The operator to use (only available for certain fields; see the filter documentation).
3. Value

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bronto/filter.rb', line 21

def add_filter(*args)
  raise ArgumentError, "wrong number of arguments (#{args.length} for 2..3)" if args.length != 2 and args.length != 3

  field = args.shift.to_sym
  self.fields[field] = [] unless self.fields.has_key?(field)

  if args.length == 1
    self.fields[field] << args.first
  else
    self.fields[field] << { operator: args.first, value: args.last }
  end

  self
end

#to_hashObject



12
13
14
15
# File 'lib/bronto/filter.rb', line 12

def to_hash
  hash = { type: type || "AND" }
  hash.merge(fields)
end