Class: Appfuel::Initialize::Initializer

Inherits:
Object
  • Object
show all
Defined in:
lib/appfuel/initialize/initializer.rb

Overview

The client application will declare a series of initializer blocks. Each of these blocks are represented as this class. This allows us to save the block to be later executed along with info about which environments this can run on

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, env = [], &block) ⇒ Initializer

Ensure each environment is stored as a lowercased string, convert the name to a string as save the block to be executed later

Parameters:

  • name (String, Symbol)

    name to identify this initializer

  • env (String, Symbol, Array) (defaults to: [])

    env or list of envs to execute on

  • blk (Proc)

    the code to be excuted



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/appfuel/initialize/initializer.rb', line 20

def initialize(name, env = [], &block)
  @name = name.to_s
  @envs = []

  env = [env] if env.is_a?(String) || env.is_a?(Symbol)
  env = []    if env.nil?

  unless env.is_a?(Array)
    fail ArgumentError, "environments must be a string, symbol or array"
  end
  env.each {|item| add_env(item) }

  fail ArgumentError, "initializer requires a block" unless block_given?
  @code = block
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



8
9
10
# File 'lib/appfuel/initialize/initializer.rb', line 8

def code
  @code
end

#envsObject (readonly)

Returns the value of attribute envs.



8
9
10
# File 'lib/appfuel/initialize/initializer.rb', line 8

def envs
  @envs
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/appfuel/initialize/initializer.rb', line 8

def name
  @name
end

Instance Method Details

#add_env(name) ⇒ Array

Parameters:

  • name (String, Symbol)

    name of the environment

Returns:

  • (Array)


51
52
53
54
55
# File 'lib/appfuel/initialize/initializer.rb', line 51

def add_env(name)
  name = name.to_s.downcase
  fail "env already exists" if envs.include?(name)
  envs << name.to_s.downcase
end

#call(config, container) ⇒ Object

Delegate to executing the stored code

Parameters:

  • config (Hash)
  • app_container (Dry::Container)

Returns:

  • nil



62
63
64
65
# File 'lib/appfuel/initialize/initializer.rb', line 62

def call(config, container)
  code.call(config, container)
  nil
end

#env_allowed?(env) ⇒ Bool

Determines which env this is allowed to execute on. No enironment means it it is allow to execute on all

Parameters:

  • env (String, Symbol)

Returns:

  • (Bool)


41
42
43
44
45
# File 'lib/appfuel/initialize/initializer.rb', line 41

def env_allowed?(env)
  return true if envs.empty?

  envs.include?(env.to_s.downcase)
end