Class: RubyRest::SequelApplication

Inherits:
SecureApplication show all
Defined in:
lib/rubyrest/application.rb

Overview

Specialization of a secure application, that provides support for Sequel (sequel.rubyforge.org) configuration at startup by implementing the ‘init_database’ method

Instance Attribute Summary

Attributes inherited from AbstractApplication

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SecureApplication

#auto_bind, #create, #delete, #initialize, #register_security, #resolve_principal, #retrieve, #security, #update

Methods inherited from SimpleApplication

#create, #delete, #retrieve, #update

Methods inherited from AbstractApplication

#formatter, #initialize, #register_domain, #register_formatters, #register_resource, #render_model, #resource_by_domain, #resource_by_name, #resource_by_path, #setup, #to_domain_class, #to_resource_class, #to_s

Constructor Details

This class inherits a constructor from RubyRest::SecureApplication

Class Method Details

.persistent_resourcesObject

Returns the list of persistent resources



266
267
268
269
# File 'lib/rubyrest/application.rb', line 266

def self.persistent_resources
  @persistent = [] if !@persistent
  return @persistent
end

.with_persistent_resources(*resources) ⇒ Object

Specifies the list of resources to be persisted



261
262
263
# File 'lib/rubyrest/application.rb', line 261

def self.with_persistent_resources *resources
  @persistent = resources
end

Instance Method Details

#import_dataObject

PLease override this in subclasses



294
295
296
297
298
299
300
301
302
# File 'lib/rubyrest/application.rb', line 294

def import_data
  begin 
    import_file = @config[:resources_dir]||"" + @config[:service] + ".sql"
    IO.readlines( import_file, ";" ).each{ |insert| @db.execute insert }
  rescue => e
    puts "skipping import of #{import_file}. Reason: #{e.message}"
  end
  puts "data import complete from #{import_file} ..."
end

#init_databaseObject

Connects all persistent resources to the database and optionnally recreates the database schema. For the moment this only works with PostgreSQL databases



279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/rubyrest/application.rb', line 279

def init_database
  @db = Sequel::Postgres::Database.new( @config )
  
  #@db = Sequel.open "sqlite:///#{to_database_path}"
  self.class.persistent_resources.each{ |r| 
    resource = resource_by_name( r )
    resource.domain.db=@db 
    if @config[:destroy]
      resource.domain.recreate_table 
    end
  }
  import_data if @config[:destroy]
end

#is_a_collection(model) ⇒ Object

Tells whether the specified model will be rendered as a feed or entry. To be subclassed



306
307
308
# File 'lib/rubyrest/application.rb', line 306

def is_a_collection( model )
  super( model ) || model.is_a?( Sequel::Dataset )
end

#is_a_service_doc(model) ⇒ Object



310
311
312
# File 'lib/rubyrest/application.rb', line 310

def is_a_service_doc( model )
  super( model ) || model.is_a?( RubyRest::Atom::ServiceDocument )
end

#to_database_pathObject

Builds the path to the sqlite database file



272
273
274
# File 'lib/rubyrest/application.rb', line 272

def to_database_path
  "#{@config[:workdir]}/#{@config[:service]}-#{@config[:database]}.database"
end