Class: OTRS::Relation

Inherits:
Array
  • Object
show all
Defined in:
lib/otrs_connector/otrs/relation.rb

Instance Method Summary collapse

Instance Method Details

#limit(int) ⇒ Object



48
49
50
# File 'lib/otrs_connector/otrs/relation.rb', line 48

def limit(int)
  self[0...int]
end

#order(field = 'id', order = 'desc') ⇒ Object

This is hit and miss for some reason… sometimes the order works correctly, sometimes it’s backwards (desc/asc)



53
54
55
56
57
58
59
60
# File 'lib/otrs_connector/otrs/relation.rb', line 53

def order(field='id',order='desc')
  case order.downcase.to_s
  when 'asc'
    self.sort { |a,b| b.send(field.to_s) <=> a.send(field.to_s) }
  when 'desc'                                               
    self.sort { |a,b| a.send(field.to_s) <=> b.send(field.to_s) }
  end
end

#uniqifyObject

Removes duplicate records



63
64
65
66
67
68
69
70
71
# File 'lib/otrs_connector/otrs/relation.rb', line 63

def uniqify
  ids = []
  results = self.class.new
  self.each do |s|
    results << s unless ids.include? s.id
    ids << s.id
  end
  results
end

#where(attributes) ⇒ Object

Allows chaining where methods, this method only parses the already returned objects from OTRS, currently. In the future I hope to have this grouping the where chains together into one OTRS request for all in the chain



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/otrs_connector/otrs/relation.rb', line 34

def where(attributes)
  relation = self.class.new
  attributes.each do |lookup_key,lookup_value|
    self.each do |object|
      object.attributes.each do |object_key,object_value|
        if object_key == lookup_key and object_value == lookup_value
          relation << object
        end
      end
    end
  end
  return relation
end