Class: Rhoconnect::Model::JsBase

Inherits:
Base
  • Object
show all
Defined in:
lib/rhoconnect/model/js_base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Base

#session, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#after_query, #ask, #before_query, create, #current_user, #do_query, enable, #expire_bulk_data, #get_data, #initialize, load, load_source_model, #push_deletes, #push_objects, #run_validators, #stash_result, #sync, #validate

Methods included from Helpers

extended, #validators

Methods included from Handler::Helpers::Binding

#bind_handler

Constructor Details

This class inherits a constructor from Rhoconnect::Model::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/rhoconnect/model/js_base.rb', line 9

def method_missing(method_name,*args)
  obj = Object.const_get(self.class.to_s)
  if obj.js_method_list.include? method_name.to_s
    self.class.package_and_publish(self,method_name,current_user,args)
  else
    log "METHOD #{method_name} NOT DEFINED IN JS MODEL #{self.class.to_s}"
    return "#{method_name} method not defined for #{self.class.to_s}"
  end
end

Class Attribute Details

.actual_nameObject

Returns the value of attribute actual_name.



5
6
7
# File 'lib/rhoconnect/model/js_base.rb', line 5

def actual_name
  @actual_name
end

.js_method_listObject

Returns the value of attribute js_method_list.



5
6
7
# File 'lib/rhoconnect/model/js_base.rb', line 5

def js_method_list
  @js_method_list
end

Instance Attribute Details

#resultObject

Returns the value of attribute result.



7
8
9
# File 'lib/rhoconnect/model/js_base.rb', line 7

def result
  @result
end

Class Method Details

.class_rho_methods(name, args = nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/rhoconnect/model/js_base.rb', line 75

def self.class_rho_methods(name,args=nil)
  if has_method? name
    package_and_publish(self,name,nil,args)
  else
    send(name,args)
  end
end

.has_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/rhoconnect/model/js_base.rb', line 83

def self.has_method?(name)
  self.js_method_list.include? name
end

.package_and_publish(caller, method_name, user, args = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/rhoconnect/model/js_base.rb', line 95

def self.package_and_publish(caller,method_name,user,args=nil)
  json = {
     :klss   => self.actual_name,
     :function => method_name,
     :args  => args,
     :route => 'request'
   }
   json[:user] = user. if user
   NodeChannel.publish_channel_and_wait(json,caller)
end

.partition_name(user_id) ⇒ Object



71
72
73
# File 'lib/rhoconnect/model/js_base.rb', line 71

def self.partition_name(user_id)
  class_rho_methods('partitionName',{:user_id => user_id})
end

.register_models(models) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rhoconnect/model/js_base.rb', line 19

def self.register_models(models)
  app = App.load(APP_NAME)
  models.each do |key,val|
    a_name = key
    klassified = key.classify
    unless Object.const_defined?(klassified)
      klass = Object.const_set(klassified, Class.new(Rhoconnect::Model::JsBase))
      klass.js_method_list = val
      klass.actual_name = a_name
    end
    source = Source.load(klassified,{:app_id=>APP_NAME,:user_id=>'*'})
    unless source
      sconfig = Rhoconnect.source_config(klassified)
      Source.create(sconfig.merge!(:name => klassified),{:app_id => APP_NAME})
      app.sources << klassified
    end
  end
end

Instance Method Details

#create(create_hash) ⇒ Object



50
51
52
# File 'lib/rhoconnect/model/js_base.rb', line 50

def create(create_hash)
 rho_methods('create',create_hash)
end

#delete(delete_hash) ⇒ Object



58
59
60
# File 'lib/rhoconnect/model/js_base.rb', line 58

def delete(delete_hash)
  rho_methods('delete',delete_hash)
end

#loginObject



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

def 
  rho_methods('login')
end

#logoffObject



62
63
64
# File 'lib/rhoconnect/model/js_base.rb', line 62

def logoff
  rho_methods('logoff')
end

#query(params = nil) ⇒ Object



42
43
44
# File 'lib/rhoconnect/model/js_base.rb', line 42

def query(params=nil)
  @result = rho_methods('query',params)
end

#rho_methods(name, args = nil) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/rhoconnect/model/js_base.rb', line 87

def rho_methods(name,args=nil)
  if self.class.has_method? name
    self.class.package_and_publish(self,name,self.current_user,args)
  else
    send(name,args)
  end
end

#search(params = nil) ⇒ Object



46
47
48
# File 'lib/rhoconnect/model/js_base.rb', line 46

def search(params=nil)
  rho_methods('search',params)
end

#store_blob(obj, field_name, blob) ⇒ Object



66
67
68
69
# File 'lib/rhoconnect/model/js_base.rb', line 66

def store_blob(obj,field_name,blob)
  blob[:path] = blob[:tempfile].path
  rho_methods('storeBlob',blob)
end

#update(update_hash) ⇒ Object



54
55
56
# File 'lib/rhoconnect/model/js_base.rb', line 54

def update(update_hash)
  rho_methods('update',update_hash)
end