Class: Rho::RhoContact

Inherits:
Object
  • Object
show all
Defined in:
lib/rho/rhocontact.rb

Class Method Summary collapse

Class Method Details

.create!(properties) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rho/rhocontact.rb', line 22

def create!(properties)
  pb = Phonebook::openPhonebook
  unless pb.nil?
    record = Phonebook::createRecord(pb)
    if record.nil?
      puts "Can't find record " + properties['id']
    else
      properties.each do |key,value|
        Phonebook::setRecordValue(record,key,value)
      end
      Phonebook::addRecord(pb,record)
    end
    Phonebook::closePhonebook(pb)
  end
end

.destroy(recordId) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rho/rhocontact.rb', line 38

def destroy(recordId)
  pb = Phonebook::openPhonebook
  unless pb.nil?
    record = Phonebook::openPhonebookRecord(pb,recordId)
    if record.nil?
      puts "Can't find record " + recordId
    else
      Phonebook::deleteRecord(pb,record)
    end
    Phonebook::closePhonebook(pb)
  end
end

.find(param) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rho/rhocontact.rb', line 6

def find(param)
  pb = Phonebook::openPhonebook
  if pb.nil?
    puts "Can't open phonebook"
    return nil
  elsif param == :all or param == 'all'
    records = Phonebook::getallPhonebookRecords(pb)
    Phonebook::closePhonebook(pb)
    return records
  else
    record = Phonebook::getPhonebookRecord(pb,param)
    Phonebook::closePhonebook(pb)
    return record
  end
end

.select(index, &block) ⇒ Object

> returns all records of the Test User from the company rhomobile



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rho/rhocontact.rb', line 80

def select(index, &block)
  key, value = index.keys[0], index.values[0]
  if @contacts.nil? or @key != key
    @key, @contacts = key, find(:all).to_a.sort! {|x,y| x[1][key] <=> y[1][key] }
  end
  found = @contacts[@contacts.bsearch_range {|x| x[1][key] <=> value}]
  unless found.nil? or block.nil?
    return found.select(&block)
  end
  return found
end

.select_by_name(first_last_name, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rho/rhocontact.rb', line 92

def select_by_name(first_last_name, &block)
  if @contacts.nil?
    @contacts = find(:all).to_a.sort! do |x,y|
      x[1]['first_name'] + " " + x[1]['last_name'] <=> y[1]['first_name'] + " " + y[1]['last_name']
    end
  end
  range = @contacts.bsearch_range do |x|
    x[1]['first_name'] + " " + x[1]['last_name'] <=> first_last_name
  end
  found = @contacts[range]
  unless found.nil? or block.nil?
    return found.select(&block)
  end
  return found
end

.update_attributes(properties) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rho/rhocontact.rb', line 51

def update_attributes(properties)
  pb = Phonebook::openPhonebook
  unless pb.nil?
    record = Phonebook::openPhonebookRecord(pb,properties['id'])
    if record.nil?
      puts "Can't find record " + properties['id']
    else
      properties.each do |key,value|
        Phonebook::setRecordValue(record,key,value)
      end
      Phonebook::saveRecord(pb,record)
    end
    Phonebook::closePhonebook(pb)
  end
end