lib/ronin/database/database.rb in ronin-1.4.1 vs lib/ronin/database/database.rb in ronin-1.5.0.rc1

- old
+ new

@@ -62,11 +62,11 @@ # # @since 1.0.0 # # @api private # - def Database.repositories + def self.repositories if @repositories.empty? @repositories[:default] = DEFAULT_REPOSITORY if File.file?(CONFIG_FILE) config = YAML.load_file(CONFIG_FILE) @@ -95,11 +95,11 @@ # # @since 1.0.0 # # @api semipublic # - def Database.repository?(name) + def self.repository?(name) repositories.has_key?(name.to_sym) end # # Saves the Database configuration to `CONFIG_FILE`. @@ -112,11 +112,11 @@ # # @since 1.0.0 # # @api private # - def Database.save + def self.save yield if block_given? File.open(CONFIG_FILE,'w') do |file| hash = {} @@ -129,11 +129,11 @@ return true end # - # Setup the Database log. + # Returns or sets up the Database log. # # @param [Hash] options # Additional options. # # @option options [String] :path (DEFAULT_LOG_PATH) @@ -149,22 +149,25 @@ # * `:error` # * `:warn` # * `:info` # * `:debug` # - # @return [true] - # Specifies that the log has been setup. + # @return [DataMapper::Logger] + # The Database Logger. # - # @api private + # @api semipublic # - def Database.setup_log(options={}) - path = options.fetch(:path,DEFAULT_LOG_PATH) - stream = options.fetch(:stream,File.new(path,'w+')) - level = options.fetch(:level,DEFAULT_LOG_LEVEL) + def self.log(options={}) + unless (@log && options.empty?) + path = options.fetch(:path,DEFAULT_LOG_PATH) + stream = options.fetch(:stream,File.new(path,'w+')) + level = options.fetch(:level,DEFAULT_LOG_LEVEL) - @log = DataMapper::Logger.new(stream,level) - return true + @log = DataMapper::Logger.new(stream,level) + end + + return @log end # # Determines if a specific database repository is setup. # @@ -174,11 +177,11 @@ # @return [Boolean] # Specifies whether or not the Database is setup. # # @api semipublic # - def Database.setup?(name=:default) + def self.setup?(name=:default) repository = DataMapper.repository(name) return repository.class.adapters.has_key?(repository.name) end @@ -190,38 +193,46 @@ # Specifies whether the Database was migrated or is currently # not setup. # # @api semipublic # - def Database.upgrade! + def self.upgrade! if setup? Migrations.migrate_up! else false end end # # Sets up the Database. # + # @param [String, Hash] uri + # The optional default repository to setup instead of {repositories}. + # # @see Database.upgrade! # # @api semipublic # - def Database.setup + def self.setup(uri=nil) # setup the database log unless @log - if ($DEBUG || ENV['DEBUG']) - setup_log(:stream => $stderr, :level => :debug) + if $DEBUG + log(:stream => $stderr, :level => :debug) else - setup_log + log end end - # setup the database repositories - repositories.each do |name,uri| - DataMapper.setup(name,uri) + if uri + # only setup the default database repositories + DataMapper.setup(:default,uri) + else + # setup the database repositories + repositories.each do |name,uri| + DataMapper.setup(name,uri) + end end # finalize the Models DataMapper.finalize @@ -243,11 +254,11 @@ # # @since 1.0.0 # # @api public # - def Database.repository(name,&block) + def self.repository(name,&block) name = name.to_sym unless repository?(name) raise(UnknownRepository,"unknown database repository #{name}") end @@ -272,11 +283,11 @@ # # @since 1.0.0 # # @api private # - def Database.clear(name) + def self.clear(name) name = name.to_sym unless repository?(name) raise(UnknownRepository,"unknown database repository #{name}") end @@ -300,10 +311,10 @@ # # @since 1.0.0 # # @api public # - def Database.map + def self.map results = [] repositories.each_key do |name| DataMapper.repository(name) do result = yield