Class: GH::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/gh/stack.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Stack

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.



28
29
30
31
# File 'lib/gh/stack.rb', line 28

def initialize(options = {}, &block)
  @options, @stack = {}, []
  instance_eval(&block) if block
end

Class Method Details

.build(options, &block) ⇒ Object

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.



19
20
21
# File 'lib/gh/stack.rb', line 19

def self.build(options, &block)
  new(&block).build(options)
end

Instance Method Details

#build(options = {}) ⇒ Object Also known as: new

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.



46
47
48
49
50
# File 'lib/gh/stack.rb', line 46

def build(options = {})
  @stack.reverse.inject(nil) do |backend, (klass, opts)|
    klass.new backend, @options.merge(opts).merge(options)
  end
end

#replace(old_class, new_class) ⇒ Object

Public: …



53
54
55
# File 'lib/gh/stack.rb', line 53

def replace(old_class, new_class)
  @stack.map! { |klass, options| [old_class == klass ? new_class : klass, options] }
end

#use(klass, options = {}) ⇒ Object

Public: Adds a new layer to the stack.

Layer will be wrapped by layers already on the stack.



36
37
38
39
# File 'lib/gh/stack.rb', line 36

def use(klass, options = {})
  @stack << [klass, options]
  self
end