lib/ronin/database/database.rb in ronin-1.0.0 vs lib/ronin/database/database.rb in ronin-1.1.0.rc1

- old
+ new

@@ -60,10 +60,12 @@ # @raise [InvalidConfig] # The config file did not contain a YAML Hash. # # @since 1.0.0 # + # @api private + # def Database.repositories if @repositories.empty? @repositories[:default] = DEFAULT_REPOSITORY if File.file?(CONFIG_FILE) @@ -91,12 +93,14 @@ # @return [Boolean] # Specifies if the Database provides the repository. # # @since 1.0.0 # + # @api semipublic + # def Database.repository?(name) - Database.repositories.has_key?(name.to_sym) + repositories.has_key?(name.to_sym) end # # Saves the Database configuration to `CONFIG_FILE`. # @@ -106,17 +110,19 @@ # # @return [true] # # @since 1.0.0 # + # @api private + # def Database.save yield if block_given? File.open(CONFIG_FILE,'w') do |file| hash = {} - Database.repositories.each do |name,value| + repositories.each do |name,value| hash[name.to_s] = value.to_s end YAML.dump(hash,file) end @@ -141,14 +147,16 @@ # May be either `:fatal`, `:error`, `:warn`, `:info` or `:debug`. # # @return [true] # Specifies that the log has been setup. # + # @api private + # def Database.setup_log(options={}) - path = (options[:path] || DEFAULT_LOG_PATH) - stream = (options[:stream] || File.new(path,'w+')) - level = (options[:level] || DEFAULT_LOG_LEVEL) + 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 end @@ -159,10 +167,12 @@ # The database repository name. # # @return [Boolean] # Specifies wether or not the Database is setup. # + # @api semipublic + # def Database.setup?(name=:default) repository = DataMapper.repository(name) return repository.class.adapters.has_key?(repository.name) end @@ -173,12 +183,14 @@ # # @return [Boolean] # Specifies whether the Database was migrated or is currently # not setup. # + # @api semipublic + # def Database.upgrade! - if Database.setup? + if setup? Migrations.migrate_up! else false end end @@ -186,21 +198,29 @@ # # Sets up the Database. # # @see Database.upgrade! # + # @api semipublic + # def Database.setup # setup the database log - Database.setup_log unless @log + unless @log + if ENV['DEBUG'] + setup_log(:stream => STDERR, :level => :debug) + else + setup_log + end + end # setup the database repositories - Database.repositories.each do |name,uri| + repositories.each do |name,uri| DataMapper.setup(name,uri) end # apply any new migrations to the database - Database.upgrade! + upgrade! end # # Performs Database transactions within a given repository. # @@ -213,14 +233,16 @@ # @raise [UnknownRepository] # The specified Database repository is unknown. # # @since 1.0.0 # + # @api public + # def Database.repository(name,&block) name = name.to_sym - unless Database.repository?(name) + unless repository?(name) raise(UnknownRepository,"unknown database repository #{name}") end return DataMapper.repository(name,&block) end @@ -240,14 +262,16 @@ # @raise [UnknownRepository] # The specified Database repository is unknown. # # @since 1.0.0 # + # @api private + # def Database.clear(name) name = name.to_sym - unless Database.repository?(name) + unless repository?(name) raise(UnknownRepository,"unknown database repository #{name}") end DataMapper.auto_migrate!(name) @@ -266,13 +290,15 @@ # @return [Array] # The results from each database transaction. # # @since 1.0.0 # + # @api public + # def Database.map results = [] - Database.repositories.each_key do |name| + repositories.each_key do |name| DataMapper.repository(name) do result = yield results << result unless result.nil? end end