Class: Rhoconnect::Model::Base

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Includes:
Handler::Helpers::Binding
Defined in:
lib/rhoconnect/model/base.rb,
lib/rhoconnect/test_methods.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

extended, validators

Methods included from Handler::Helpers::Binding

#bind_handler

Constructor Details

#initialize(source) ⇒ Base

Returns a new instance of Base.



31
32
33
# File 'lib/rhoconnect/model/base.rb', line 31

def initialize(source)
  @source = source
end

Instance Attribute Details

#resultObject

Returns the value of attribute result.



4
5
6
# File 'lib/rhoconnect/test_methods.rb', line 4

def result
  @result
end

#sessionObject

Returns the value of attribute session.



16
17
18
# File 'lib/rhoconnect/model/base.rb', line 16

def session
  @session
end

#sourceObject

Returns the value of attribute source.



16
17
18
# File 'lib/rhoconnect/model/base.rb', line 16

def source
  @source
end

Class Method Details

.create(source) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rhoconnect/model/base.rb', line 82

def self.create(source)
  model=nil
  model_klass = load_source_model(source)
  if model_klass
    begin
      model = model_klass.new(source)
    rescue ArgumentError => e
      # Backward compatibility with code generated by gems < 2.2.0
      log "ERROR: credential parameter in `initialize` method is deprecated and removed in version >= 2.2.0."
      raise e
    rescue Exception => e
      log "Failure to create model from class #{source.name}: #{e.inspect.to_s}"
      raise e
    end
  end
  model
end

.enable(procname, options = {}, &block) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rhoconnect/model/base.rb', line 22

def enable(procname, options = {}, &block)
  # installer suppose to do what's needed
  if block_given?
    options[:handler] = block
  end
  options = send "install_#{procname}", options
end

.load(username) ⇒ Object

public API Method for loading an instance of the Model



36
37
38
39
40
# File 'lib/rhoconnect/model/base.rb', line 36

def self.load(username)
  source = Source.load(self.name,
                {:user_id => username,:app_id => APP_NAME})
  self.create(source)
end

.load_source_model(source) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rhoconnect/model/base.rb', line 51

def self.load_source_model(source)
  model_klass=nil
  if source
    begin
      if source.name
        source.name = source.name.dup if source.name.frozen?
        source.name.strip!
      end
      # certain models are pre-defined and reserved
      # load them and instantiate
      if Rhoconnect.predefined_sources.has_key?(source.name)
        model_klass = Object.const_get(source.name)
      elsif Object.const_defined?(source.name) && Object.const_get(source.name).to_s.split("::").first != 'Rhoconnect'
        # fix until source adpaters are phased out, checking for Rhoconnect namespace
        # so that backend models with same name as Rhoconnect models are instantiated correctly
        model_klass = Object.const_get(source.name)
        unless model_klass
          require under_score(source.name)
          model_klass = Object.const_get(source.name)
        end
      else
        model_klass=Rhoconnect::Model::DynamicAdapterModel
      end
    rescue Exception => e
      log "Failure to find class for model #{source.name}: #{e.inspect.to_s}"
      raise e
    end
  end
  model_klass
end

.partition_name(user_id) ⇒ Object



47
48
49
# File 'lib/rhoconnect/model/base.rb', line 47

def self.partition_name(user_id)
  user_id
end

Instance Method Details

#after_queryObject



130
131
132
133
134
135
136
137
138
139
# File 'lib/rhoconnect/model/base.rb', line 130

def after_query
  res = nil
  if @source.is_pass_through?
    res = @result
  else
    self.sync
    res = true
  end
  res
end

#ask(params = nil) ⇒ Object



182
# File 'lib/rhoconnect/model/base.rb', line 182

def ask(params=nil); end

#before_queryObject



125
126
127
128
# File 'lib/rhoconnect/model/base.rb', line 125

def before_query
  @tmp_docname = :md.to_s + get_random_identifier
  @stash_size = 0
end

#create(create_hash) ⇒ Object



176
# File 'lib/rhoconnect/model/base.rb', line 176

def create(create_hash); end

#current_userObject



252
253
254
# File 'lib/rhoconnect/model/base.rb', line 252

def current_user
  @source.user
end

#delete(delete_hash) ⇒ Object



180
# File 'lib/rhoconnect/model/base.rb', line 180

def delete(delete_hash); end

#do_query(params = nil) ⇒ Object



141
142
143
144
145
# File 'lib/rhoconnect/model/base.rb', line 141

def do_query(params=nil)
  before_query
  params ? self.query(params) : self.query
  after_query
end

#expire_bulk_data(partition = :user) ⇒ Object



154
155
156
# File 'lib/rhoconnect/model/base.rb', line 154

def expire_bulk_data(partition = :user)
  Rhoconnect.expire_bulk_data(current_user.,partition)
end

#get_data(docname = :md) ⇒ Object

public API method to load the Model’s data



43
44
45
# File 'lib/rhoconnect/model/base.rb', line 43

def get_data(docname = :md)
  source.get_data(docname)
end

#loginObject



100
# File 'lib/rhoconnect/model/base.rb', line 100

def ; end

#logoffObject



184
# File 'lib/rhoconnect/model/base.rb', line 184

def logoff; end

#push_deletes(params) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rhoconnect/model/base.rb', line 220

def push_deletes(params)
  timeout = params[:timeout] || 10
  raise_on_expire = params[:raise_on_expire] || false
  rebuild_md = params[:rebuild_md].nil? ? true : params[:rebuild_md]
  objects = params[:objects]

  @source.lock(:md,timeout,raise_on_expire) do |s|
    diff_count = 0
    if(rebuild_md)
      # in case of rebuild_md
      # we clean-up and rebuild the whole :md doc
      # on every request
      doc = @source.get_data(:md)
      orig_doc_size = doc.size
      objects.each do |id|
        doc.delete(id)
      end if objects
      diff_count = doc.size - orig_doc_size
      @source.put_data(:md,doc)
    else
      # if rebuild_md == false
      # we only operate on specific set values
      # which brings a big optimization
      # in case of small transactions
      diff_count = -@source.remove_objects(:md, objects)
    end

    @source.update_count(:md_size,diff_count)
  end
  @source.announce_changes
end

#push_objects(params) ⇒ Object

plugin callbacks implementation



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rhoconnect/model/base.rb', line 187

def push_objects(params)
  timeout = params[:timeout] || 10
  raise_on_expire = params[:raise_on_expire] || false
  rebuild_md = params[:rebuild_md].nil? ? true : params[:rebuild_md]
  objects = params[:objects]

  @source.lock(:md,timeout,raise_on_expire) do |s|
    diff_count = 0
    # in case of rebuild_md
    # we clean-up and rebuild the whole :md doc
    # on every request
    if(rebuild_md)
      doc = @source.get_data(:md)
      orig_doc_size = doc.size
      objects.each do |id,obj|
        doc[id] ||= {}
        doc[id].merge!(obj)
      end if objects
      diff_count = doc.size - orig_doc_size
      @source.put_data(:md,doc)
    else
      # if rebuild_md == false
      # we only operate on specific set values
      # which brings a big optimization
      # in case of small transactions
      diff_count = @source.update_objects(:md, objects)
    end

    @source.update_count(:md_size,diff_count)
  end
  @source.announce_changes
end

#query(params = nil) ⇒ Object



102
# File 'lib/rhoconnect/model/base.rb', line 102

def query(params=nil); end

#run_validators(operation, operations_data, client_ids) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/rhoconnect/model/base.rb', line 158

def run_validators(operation, operations_data, client_ids)
  # run custom code
  invalid_meta = validate(operation, operations_data, client_ids)
  invalid_meta ||= {}
  # run pre-installed validators
  self.class.validators.each do |validator, options|
    if options.has_key? operation.to_sym
      invalid_meta = send validator.to_sym, options, invalid_meta, operation, operations_data, client_ids
    end
  end
  invalid_meta
end

#search(params = nil) ⇒ Object



104
# File 'lib/rhoconnect/model/base.rb', line 104

def search(params=nil); end

#stash_resultObject



147
148
149
150
151
152
# File 'lib/rhoconnect/model/base.rb', line 147

def stash_result
  return if @result.nil?
  @source.put_tmp_data(@tmp_docname,@result,true)
  @stash_size += @result.size
  @result = nil
end

#syncObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rhoconnect/model/base.rb', line 106

def sync
  if @result and @result.is_a?(Hash) and @result.empty?
    @source.lock(:md) do |s|
      s.flush_data(:md)
      s.put_value(:md_size,0)
    end
  else
    if @result and @result.is_a?(Hash)
      @source.put_tmp_data(@tmp_docname,@result)
      @stash_size += @result.size
    end
    @source.lock(:md) do |s|
      s.flush_data(:md)
      s.rename_tmp_data(@tmp_docname,:md)
      s.put_value(:md_size,@stash_size)
    end
  end
end

#update(update_hash) ⇒ Object



178
# File 'lib/rhoconnect/model/base.rb', line 178

def update(update_hash); end

#validate(operation, operations_data, client_ids) ⇒ Object

do pre-processing before CUD operation



172
173
174
# File 'lib/rhoconnect/model/base.rb', line 172

def validate(operation,operations_data,client_ids)
  {}
end