module Rhoconnect module Model class JsBase < Rhoconnect::Model::Base class << self attr_accessor :js_method_list,:actual_name end attr_accessor :result def method_missing(method_name,*args) obj = Object.const_get(self.class.to_s) if obj.js_method_list.include? method_name.to_s package_and_publish(method_name,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 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 def login rho_methods('login') end def query(params=nil) @result = rho_methods('query',params) end def search(params=nil) rho_methods('search',params) end def create(create_hash) rho_methods('create',create_hash) end def update(update_hash) rho_methods('update',update_hash) end def delete(delete_hash) rho_methods('delete',delete_hash) end def logoff rho_methods('logoff') end def store_blob(obj,field_name,blob) raise "store_blob not implemented in JavaScript yet." # TODO: add method handling here end private def rho_methods(name,args=nil) obj = Object.const_get(self.class.to_s) if obj.js_method_list.include? name package_and_publish(name,args) else sup.send(name,args) end end def package_and_publish(method_name,args=nil) json = { :klss => self.class.actual_name, :function => method_name, :args => args, :route => 'request' } NodeChannel.publish_channel_and_wait(json,self.class) end end end end #allows me to call super method by passing string class SuperProxy def initialize(obj) @obj = obj end def method_missing(meth, *args) @obj.class.superclass.superclass.instance_method(meth).bind(@obj).call(*args) end end class Object private def sup SuperProxy.new(self) end end class String def classify result = self.split("_").inject("") do |res,index| res += index.capitalize res end result end end