Module: TcAdapter::ClassMethods

Defined in:
lib/models/tc_adapter.rb

Instance Method Summary collapse

Instance Method Details

#allObject

TODO add pagination



12
13
14
15
16
17
18
19
# File 'lib/models/tc_adapter.rb', line 12

def all
  result = TcUser.query do |q|
    q.order_by 'created_at_i', :numdesc
  end

  #these will be the same for all adapters, they should be defined in the user class, and these methods should have a different name?
  result.collect {|instance| self.new instance }
end

#delete(pk) ⇒ Object



62
63
64
65
# File 'lib/models/tc_adapter.rb', line 62

def delete(pk)
  #true or false
  !!TcUser.delete(pk)
end

#get(hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/models/tc_adapter.rb', line 21

def get(hash)
  if hash[:id]
    pk = hash[:id]
    result = TcUser.get(pk)
  else
    result = TcUser.query do |q|
      hash.each do |key, value|
        q.add key.to_s, :streq, value.to_s
      end
    end[0]
  end
  #elsif hash[:email]
  #  result = TcUser.query do |q|
  #    q.add 'email', :streq, hash[:email]
  #  end[0]
    #the zero is because this returns an array but get should return the first result
  #end

  if result
    self.new result
  else
    nil
  end
end

#set(attributes) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/models/tc_adapter.rb', line 46

def set(attributes)
  user = TcUser.query do |q|
    q.add 'email', :streq, attributes['email']
  end

  if user == [] #no user
    self.new TcUser.set(attributes)
  else
    false
  end
end

#set!(attributes) ⇒ Object



58
59
60
# File 'lib/models/tc_adapter.rb', line 58

def set!(attributes)
  self.new TcUser.set!(attributes)
end