Skip to content

Commit

Permalink
Document adapter= and adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
julik committed Mar 20, 2024
1 parent 73f6be5 commit 9662a33
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/pecorino.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,29 @@ def self.create_tables(active_record_schema)
adapter.create_tables(active_record_schema)
end

# Allows assignment of an adapter for storing throttles. Normally this would be a subclass of `Pecorino::Adapters::BaseAdapter`, but
# you can assign anything you like. Set this in an initializer. By default Pecorino will use the adapter configured from your main
# database, but you can also create a separate database for it - or use Redis or memory storage.
#
# @param adapter[Pecorino::Adapters::BaseAdapter]
# @return [Pecorino::Adapters::BaseAdapter]
def self.adapter=(adapter)
@adapter = adapter
end

# Returns the currently configured adapter, or the default adapter from the main database
#
# @return [Pecorino::Adapters::BaseAdapter]
def self.adapter
@adapter || default_adapter_from_main_database
end

# Returns the database implementation for setting the values atomically. Since the implementation
# differs per database, this method will return a different adapter depending on which database is
# being used
def self.default_adapter_from_main_database # default_adapter_from_main_database
#
# @param adapter[Pecorino::Adapters::BaseAdapter]
def self.default_adapter_from_main_database
model_class = ActiveRecord::Base
adapter_name = model_class.connection.adapter_name
case adapter_name
Expand All @@ -61,7 +72,7 @@ def self.default_adapter_from_main_database # default_adapter_from_main_database
when /sqlite/i
Pecorino::Adapters::SqliteAdapter.new(model_class)
else
raise "Pecorino does not support #{adapter_name} just yet"
raise "Pecorino does not support the #{adapter_name} database just yet"
end
end
end

0 comments on commit 9662a33

Please sign in to comment.