Class: MiddleSquid::Builder
- Inherits:
-
Object
- Object
- MiddleSquid::Builder
- Defined in:
- lib/middle_squid/builder.rb
Overview
Small DSL to configure MiddleSquid.
Instance Attribute Summary collapse
-
#adapter ⇒ Adapter
readonly
Returns the adapter selected by #use.
-
#blacklists ⇒ Array<BlackList>
readonly
Returns the blacklists registered by #blacklist.
-
#custom_actions ⇒ Hash<Symbol, Proc>
readonly
Returns the custom actions created by #define_action.
-
#handler ⇒ #call
readonly
Returns the object passed to #run.
Class Method Summary collapse
Instance Method Summary collapse
-
#blacklist(*args) ⇒ BlackList
Returns a new registered blacklist instance.
-
#database(path) ⇒ Object
Setup the blacklist database.
-
#define_action(name, &block) ⇒ Object
(also: #define_helper)
Register a custom action or helper.
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
-
#run(handler) ⇒ Object
Takes any object that responds to the
call
method with two arguments: the URI to process and an array of extra data. -
#use(adapter, **options) ⇒ Adapter
Select the active adapter.
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
43 44 45 46 |
# File 'lib/middle_squid/builder.rb', line 43 def initialize @blacklists = [] @custom_actions = {} end |
Instance Attribute Details
#adapter ⇒ Adapter (readonly)
Returns the adapter selected by #use.
39 40 41 |
# File 'lib/middle_squid/builder.rb', line 39 def adapter @adapter ||= Adapters::Squid.new end |
#blacklists ⇒ Array<BlackList> (readonly)
Returns the blacklists registered by #blacklist.
23 24 25 |
# File 'lib/middle_squid/builder.rb', line 23 def blacklists @blacklists end |
#custom_actions ⇒ Hash<Symbol, Proc> (readonly)
Returns the custom actions created by #define_action.
28 29 30 |
# File 'lib/middle_squid/builder.rb', line 28 def custom_actions @custom_actions end |
#handler ⇒ #call (readonly)
Returns the object passed to #run.
33 34 35 |
# File 'lib/middle_squid/builder.rb', line 33 def handler @handler end |
Class Method Details
.from_file(file) ⇒ Builder
49 50 51 52 53 54 55 |
# File 'lib/middle_squid/builder.rb', line 49 def self.from_file(file) obj = self.new content = File.read file obj.instance_eval content, file obj end |
Instance Method Details
#blacklist(*args) ⇒ BlackList
You need to call #database in order to use the blacklists.
Returns a new registered blacklist instance.
114 115 116 117 118 |
# File 'lib/middle_squid/builder.rb', line 114 def blacklist(*args) bl = BlackList.new *args @blacklists << bl bl end |
#database(path) ⇒ Object
Setup the blacklist database. It will be created if the file does not exists. Read/write access is required.
Run middle_squid index to add your blacklists to the database.
84 85 86 |
# File 'lib/middle_squid/builder.rb', line 84 def database(path) Database.setup path end |
#define_action(name, &block) ⇒ Object Also known as: define_helper
Register a custom action or helper.
136 137 138 139 140 |
# File 'lib/middle_squid/builder.rb', line 136 def define_action(name, &block) raise ArgumentError, 'no block given' unless block_given? @custom_actions[name] = block end |
#run(handler) ⇒ Object
Takes any object that responds to the call
method with two arguments: the URI to process and an array of extra data.
153 154 155 156 157 |
# File 'lib/middle_squid/builder.rb', line 153 def run(handler) raise ArgumentError, 'the handler must respond to #call' unless handler.respond_to? :call @handler = handler end |
#use(adapter, **options) ⇒ Adapter
Select the active adapter. By default Adapters::Squid with no options will be used.
67 68 69 70 71 |
# File 'lib/middle_squid/builder.rb', line 67 def use(adapter, **) raise ArgumentError, 'Not an adapter.' unless adapter < Adapter @adapter = adapter.new() end |