Class: Kiss::ModelCache

Inherits:
Object show all
Defined in:
lib/kiss/model.rb

Instance Method Summary collapse

Constructor Details

#initialize(database, model_dir) ⇒ ModelCache

Returns a new instance of ModelCache.



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/kiss/model.rb', line 251

def initialize(database, model_dir)
  @_db = database
  @_model_dir = model_dir
  @_cache = {}
  
  # TODO: Fix this to use file cache and subclass hierarchy, so models can be
  # reloaded if _model.rb changes.
  @_root_class = Class.new(Kiss::Model)
  if File.exist?(filename = "#{@_model_dir}/_model.rb")
    @_root_class.class_eval(File.read(filename), filename)
  end
end

Instance Method Details

#[](source) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/kiss/model.rb', line 275

def [](source)
  raise 'argument to model cache must be symbol of database table name' unless source.is_a?(Symbol)
  
  database = @_db
  @_model_dir ? begin
    # TODO: use request's file_cache
    model_path = "#{@_model_dir}/#{source}.rb"
    db.kiss_controller.file_cache(model_path) do |src|
      klass = new_model_class(database, source)
      klass.class_eval(src, model_path) if src
      klass
    end
  end : @_cache[source] ||= new_model_class(database, source)
end

#databaseObject Also known as: db



290
291
292
# File 'lib/kiss/model.rb', line 290

def database
  @_db
end

#literal(*args) ⇒ Object Also known as: quote



295
296
297
# File 'lib/kiss/model.rb', line 295

def literal(*args)
  Sequel::Model.dataset.literal(*args)
end

#new_model_class(database, source) ⇒ Object



264
265
266
267
268
269
270
271
272
273
# File 'lib/kiss/model.rb', line 264

def new_model_class(database, source)
  klass = Class.new(@_root_class)
  klass.set_dataset(database[source])
  klass.table = source
  klass.class_eval do
    @_value_column = :id
    @_display_column = :name
  end
  klass
end