Module: ActsAsCleo::InstanceMethods

Defined in:
lib/acts_as_cleo/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#cleo_idObject

end callback hooks



26
27
28
29
30
31
32
# File 'lib/acts_as_cleo/instance_methods.rb', line 26

def cleo_id
  return nil if self.id.nil?
  cr = cleo_reference

  return nil if cr.nil?
  return cr.id
end

#cleo_referenceObject



82
83
84
# File 'lib/acts_as_cleo/instance_methods.rb', line 82

def cleo_reference
  Cleo::Element.find(:first, :conditions => ["record_type = ? and record_id = ?", record_type, self.id])
end

#cleo_scoreObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/acts_as_cleo/instance_methods.rb', line 61

def cleo_score
  s = self.send(self.cleo_config[:score])

  begin
    return Float(s)
  rescue ArgumentError => e
    if s.nil?
      return 0
    elsif s.respond_to?("count")
      return s.count
    end
  end

  return s
end

#create_cleoObject

callback hooks to keep cleo insync with data



4
5
6
# File 'lib/acts_as_cleo/instance_methods.rb', line 4

def create_cleo
  Cleo::ElementServer.create(self)
end

#record_typeObject



78
79
80
# File 'lib/acts_as_cleo/instance_methods.rb', line 78

def record_type
  self.cleo_config[:type]
end

#remove_from_cleoObject



12
13
14
15
16
17
18
# File 'lib/acts_as_cleo/instance_methods.rb', line 12

def remove_from_cleo
  cr = cleo_reference
  current_cleo_id = self.cleo_id
  cr.delete

  Cleo::ElementServer.delete(current_cleo_id)
end

#set_cleo_idObject



20
21
22
23
# File 'lib/acts_as_cleo/instance_methods.rb', line 20

def set_cleo_id
  cr = Cleo::Element.find(:first, :conditions => ["record_type = ? and record_id = ?", record_type, self.id])
  cr = Cleo::Element.create(:record_type => record_type, :record_id => self.id) if cr.nil?
end

#to_cleo_resultObject Also known as: as_cleo



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/acts_as_cleo/instance_methods.rb', line 34

def to_cleo_result
  #take self and change it into a Cleo::Xml::Result and return
  cr = Cleo::Xml::Result.new
  cr.term = []

  to_process = []

  self.cleo_config[:terms].each do |term|
    to_process << self.send(term).to_s.downcase
  end

  cr.term = clean_terms_for_storage(to_process).flatten

  set_cleo_id if self.cleo_id.nil? && !self.id.nil?
  cr.id = self.cleo_id

  cr.name = self.send(self.cleo_config[:name]).to_s.downcase
  cr.name = cr.term.first if cr.name.blank?
  cr.name = cr.name.gsub('&', 'and')

  cr.score = self.cleo_score

  cr
end

#update_cleoObject



8
9
10
# File 'lib/acts_as_cleo/instance_methods.rb', line 8

def update_cleo
  Cleo::ElementServer.update(self)
end