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

Instance Attribute Summary collapse

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.



30
31
32
33
# File 'lib/gh/stack.rb', line 30

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/gh/stack.rb', line 14

def options
  @options
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.



21
22
23
# File 'lib/gh/stack.rb', line 21

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.



48
49
50
51
52
# 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

#replace(old_class, new_class) ⇒ Object

Public: …



55
56
57
# 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 = {}) ⇒ Object

Public: Adds a new layer to the stack.

Layer will be wrapped by layers already on the stack.



38
39
40
41
# File 'lib/gh/stack.rb', line 38

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