Module: Pecorino

Defined in:
lib/pecorino.rb,
lib/pecorino/railtie.rb,
lib/pecorino/version.rb,
lib/pecorino/install_generator.rb

Defined Under Namespace

Modules: Adapters Classes: Block, CachedThrottle, InstallGenerator, LeakyBucket, Railtie, Throttle

Constant Summary collapse

VERSION =
"0.7.0"

Class Method Summary collapse

Class Method Details

.adapterPecorino::Adapters::BaseAdapter

Returns the currently configured adapter, or the default adapter from the main database



57
58
59
# File 'lib/pecorino.rb', line 57

def self.adapter
  @adapter || default_adapter_from_main_database
end

.adapter=(adapter) ⇒ Pecorino::Adapters::BaseAdapter

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.



50
51
52
# File 'lib/pecorino.rb', line 50

def self.adapter=(adapter)
  @adapter = adapter
end

.create_tables(active_record_schema) ⇒ Object

Creates the tables and indexes needed for Pecorino. Call this from your migrations like so:

class CreatePecorinoTables < ActiveRecord::Migration[7.0]
  def change
    Pecorino.create_tables(self)
  end
end

Parameters:

  • active_record_schema (ActiveRecord::SchemaMigration)

    the migration through which we will create the tables

Returns:

  • void



40
41
42
# File 'lib/pecorino.rb', line 40

def self.create_tables(active_record_schema)
  adapter.create_tables(active_record_schema)
end

.default_adapter_from_main_databaseObject

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

Parameters:



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pecorino.rb', line 66

def self.default_adapter_from_main_database
  model_class = ActiveRecord::Base
  adapter_name = model_class.connection.adapter_name
  case adapter_name
  when /postgres/i
    Pecorino::Adapters::PostgresAdapter.new(model_class)
  when /sqlite/i
    Pecorino::Adapters::SqliteAdapter.new(model_class)
  else
    raise "Pecorino does not support the #{adapter_name} database just yet"
  end
end

.prune!Object

Deletes stale leaky buckets and blocks which have expired. Run this method regularly to avoid accumulating too many unused rows in your tables.

Returns:

  • void



26
27
28
# File 'lib/pecorino.rb', line 26

def self.prune!
  adapter.prune
end