Class: Rhoconnect::Client

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

Instance Attribute Summary collapse

Attributes inherited from StoreOrm

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LockOps

#lock

Methods included from Document

#clone, #compute_store_index, #compute_token, #delete_data, #docname, #exists?, #flush_data, #get_data, #get_db_doc, #get_diff_data, #get_diff_data_bruteforce, #get_list, #get_object, #get_objects, #get_value, included, #put_data, #put_list, #put_object, #put_tmp_data, #put_value, #remove_objects, #rename, #rename_tmp_data, #set_db_doc, #update_count, #update_elements, #update_objects, #verify_doctype

Methods inherited from StoreOrm

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

Constructor Details

This class inherits a constructor from Rhoconnect::StoreOrm

Instance Attribute Details

#source_nameObject

Returns the value of attribute source_name.



15
16
17
# File 'lib/rhoconnect/client.rb', line 15

def source_name
  @source_name
end

Class Method Details

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



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rhoconnect/client.rb', line 47

def self.create(fields,params={})
  fields[:id] ||= get_random_identifier
  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



60
61
62
63
# File 'lib/rhoconnect/client.rb', line 60

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

Instance Method Details

#appObject



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

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

#deleteObject



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

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

#doc_suffix(doctype) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/rhoconnect/client.rb', line 69

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

#flush_all_documentsObject



93
94
95
96
97
# File 'lib/rhoconnect/client.rb', line 93

def flush_all_documents
  app.sources.each do |sourcename|
    flush_source_documents(sourcename)
  end  
end

#flush_source_data(doctype, from_source) ⇒ Object



105
106
107
108
109
110
# File 'lib/rhoconnect/client.rb', line 105

def flush_source_data(doctype, from_source)
  verify_doctype(doctype)
  self.source_name=from_source
  docnamestr = docname('') + doctype.to_s
  Store.flush_data(docnamestr)
end

#flush_source_documents(source_name) ⇒ Object



99
100
101
102
103
# File 'lib/rhoconnect/client.rb', line 99

def flush_source_documents(source_name)
  self.class.valid_doctypes.each do |docname, doctype|
    flush_source_data(docname, source_name)
  end
end

#store_index(doctype) ⇒ Object



78
79
80
81
# File 'lib/rhoconnect/client.rb', line 78

def store_index(doctype)
  source = Source.load(self.source_name,{:app_id => app_id,:user_id => self.user_id})
  index = compute_store_index(doctype, source, user_id)
end

#switch_user(new_user_id) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/rhoconnect/client.rb', line 112

def switch_user(new_user_id)
  loaded_source = self.source_name
  flush_all_documents
  User.load(self.user_id).clients.delete(self.id)
  User.load(new_user_id).clients << self.id
  self.user_id = new_user_id
  self.source_name = loaded_source
end

#update_clientdoc(sources) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rhoconnect/client.rb', line 121

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})
    self.source_name = source
    unless s.sync_type.to_sym == :bulk_sync_only
      s.clone(:md_copy,self.docname(:cd))
    end
    self.put_value(:schema_sha1,s.get_value(:schema_sha1))
  end
end

#update_fields(params) ⇒ Object



134
135
136
137
138
# File 'lib/rhoconnect/client.rb', line 134

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