class GH::Stack

Public: Exposes DSL for stacking wrappers.

Examples

api = GH::Stack.build do
  use GH::Cache, cache: Rails.cache
  use GH::Normalizer
  use GH::Remote, username: "admin", password: "admin"
end

Attributes

options[R]

Public Class Methods

build(options = {}, &block) click to toggle source

Public: Generates a new wrapper stack from the given block.

options - Hash of options that will be passed to all layers upon initialization.

Returns top most Wrapper instance.

# File lib/gh/stack.rb, line 21
def self.build(options = {}, &block)
  new(&block).build(options)
end
new(options = {}, &block) click to toggle source

Public: Generates a new Stack instance.

options - Hash of options that will be passed to all layers upon initialization.

Can be used for easly stacking layers.

# File lib/gh/stack.rb, line 30
def initialize(options = {}, &block)
  @options, @stack = {}, []
  instance_eval(&block) if block
end

Public Instance Methods

build(options = {}) click to toggle source

Public: Generates wrapper instances for stack configuration.

options - Hash of options that will be passed to all layers upon initialization.

Returns top most Wrapper instance.

# File lib/gh/stack.rb, line 48
def build(options = {})
  @stack.reverse.inject(nil) do |backend, (klass, opts)|
    klass.new backend, @options.merge(opts).merge(options)
  end
end
Also aliased as: new
new(options = {})
Alias for: build
replace(old_class, new_class) click to toggle source

Public: …

# File lib/gh/stack.rb, line 55
def replace(old_class, new_class)
  @stack.map! { |klass, options| [old_class == klass ? new_class : klass, options] }
end
use(klass, options = {}) click to toggle source

Public: Adds a new layer to the stack.

Layer will be wrapped by layers already on the stack.

# File lib/gh/stack.rb, line 38
def use(klass, options = {})
  @stack << [klass, options]
  self
end