Class: Rhoconnect::Client

Inherits:
Model
  • Object
show all
Includes:
Document, LockOps
Defined in:
lib/rhoconnect/client.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LockOps

#lock

Methods included from Document

#delete_data, #docname, #flash_data, #flash_source_data, #flush_zdata, #get_data, #get_list, #get_object, #get_value, #get_zdata, #put_data, #put_list, #put_object, #put_value, #put_zdata, #remove_objects, #rename, #update_count, #update_objects

Methods inherited from Model

_field_key, _prefix, class_prefix, #decrement!, field, #field_key, fields, #increment!, #initialize, is_exist?, list, marshal_class_name, #next_id, populate_attributes, #redis, redis, set, #to_array, validates_presence_of

Constructor Details

This class inherits a constructor from Rhoconnect::Model

Instance Attribute Details

#source_nameObject

Returns the value of attribute source_name.



13
14
15
# File 'lib/rhoconnect/client.rb', line 13

def source_name
  @source_name
end

Class Method Details

.create(fields, params = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rhoconnect/client.rb', line 19

def self.create(fields,params={})
  Rhoconnect.license.check_and_use_seat
  fields[:id] ||= get_random_uuid
  res = super(fields,params)
  user = User.load(fields[:user_id])
  user.clients << res.id
  if Rhoconnect.stats
    Rhoconnect::Stats::Record.set('clients') { Store.incr('client:count') }
  else
    Store.incr('client:count')
  end
  res
end

.load(id, params) ⇒ Object



33
34
35
36
# File 'lib/rhoconnect/client.rb', line 33

def self.load(id,params)
  validate_attributes(params)
  super(id,params)
end

Instance Method Details

#appObject



38
39
40
# File 'lib/rhoconnect/client.rb', line 38

def app
  @app ||= App.load(app_id)
end

#deleteObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/rhoconnect/client.rb', line 53

def delete
  flash_data('*')
  Rhoconnect.license.free_seat
  if Rhoconnect.stats
    Rhoconnect::Stats::Record.set('clients') { Store.decr('client:count') }
  else
    Store.decr('client:count')
  end
  super
end

#doc_suffix(doctype) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/rhoconnect/client.rb', line 42

def doc_suffix(doctype)
  doctype = doctype.to_s
  if doctype == '*'
    "#{self.user_id}:#{self.id}:*"
  elsif self.source_name 
    "#{self.user_id}:#{self.id}:#{self.source_name}:#{doctype}"
  else
    raise InvalidSourceNameError.new('Invalid Source Name For Client')   
  end          
end

#switch_user(new_user_id) ⇒ Object



64
65
66
67
68
69
# File 'lib/rhoconnect/client.rb', line 64

def switch_user(new_user_id)
  flash_data('*')
  User.load(self.user_id).clients.delete(self.id)
  User.load(new_user_id).clients << self.id
  self.user_id = new_user_id
end

#update_clientdoc(sources) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rhoconnect/client.rb', line 71

def update_clientdoc(sources)
  # TODO: We need to store schema info and data info in bulk data
  # source masterdoc and source schema might have changed!
  sources.each do |source|
    s = Source.load(source,{:app_id => app_id,:user_id => user_id})
    unless s.sync_type.to_sym == :bulk_sync_only
      self.source_name = source
      Store.clone(s.docname(:md_copy),self.docname(:cd))
    end
    self.put_value(:schema_sha1,s.get_value(:schema_sha1))
  end
end

#update_fields(params) ⇒ Object



84
85
86
87
88
# File 'lib/rhoconnect/client.rb', line 84

def update_fields(params)
  [:device_type, :device_push_type,:device_pin,:device_port,:phone_id].each do |setting|
    self.send "#{setting}=".to_sym, params[setting].to_s if params[setting]
  end 
end