Class: Bricolage::Context

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bricolage/context.rb

Constant Summary collapse

DEFAULT_ENV =
'development'
GLOBAL_VARIABLE_FILE =
'variable.yml'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fs, env, global_variables: nil, data_sources: nil, logger: nil) ⇒ Context

Returns a new instance of Context.



29
30
31
32
33
34
35
# File 'lib/bricolage/context.rb', line 29

def initialize(fs, env, global_variables: nil, data_sources: nil, logger: nil)
  @logger = logger || Logger.default
  @filesystem = fs
  @environment = env
  @opt_global_variables = global_variables || Variables.new
  @data_sources = data_sources
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



44
45
46
# File 'lib/bricolage/context.rb', line 44

def environment
  @environment
end

#loggerObject (readonly)

Returns the value of attribute logger.



45
46
47
# File 'lib/bricolage/context.rb', line 45

def logger
  @logger
end

Class Method Details

.environment(opt_env) ⇒ Object



12
13
14
# File 'lib/bricolage/context.rb', line 12

def Context.environment(opt_env)
  opt_env || ENV['BRICOLAGE_ENV'] || DEFAULT_ENV
end

.for_application(home_path, job_path = nil, environment: nil, global_variables: nil, logger: nil) ⇒ Object



16
17
18
19
20
# File 'lib/bricolage/context.rb', line 16

def Context.for_application(home_path, job_path = nil, environment: nil, global_variables: nil, logger: nil)
  env = environment(environment)
  fs = FileSystem.for_option_pathes(home_path, job_path, env)
  load(fs, env, global_variables: global_variables, logger: logger)
end

Instance Method Details

#builtin_variablesObject



82
83
84
85
86
87
# File 'lib/bricolage/context.rb', line 82

def builtin_variables
  Variables.define {|vars|
    vars['bricolage_env'] = @environment
    vars['bricolage_home'] = home_path.to_s
  }
end

#get_data_source(type, name) ⇒ Object



47
48
49
# File 'lib/bricolage/context.rb', line 47

def get_data_source(type, name)
  @data_sources.get(type, name)
end

#global_variablesObject

Variables



74
75
76
77
78
79
80
# File 'lib/bricolage/context.rb', line 74

def global_variables
  Variables.union(
    builtin_variables,
    load_global_variables,
    @opt_global_variables
  )
end

#load_configurationsObject



37
38
39
40
41
42
# File 'lib/bricolage/context.rb', line 37

def load_configurations
  @filesystem.config_pathes('prelude.rb').each do |path|
    EmbeddedCodeAPI.module_eval(File.read(path)) if path.exist?
  end
  @data_sources = DataSourceFactory.load(self, @logger)
end

#load_global_variablesObject



89
90
91
92
93
94
95
# File 'lib/bricolage/context.rb', line 89

def load_global_variables
  subsys_path = scoped? ? [@filesystem.relative(GLOBAL_VARIABLE_FILE)] : []
  vars_list = (config_pathes(GLOBAL_VARIABLE_FILE) + subsys_path).map {|path|
    path.exist? ? load_variables(path) : nil
  }
  Variables.union(*vars_list.compact)
end

#subsystem(id) ⇒ Object



51
52
53
54
55
56
# File 'lib/bricolage/context.rb', line 51

def subsystem(id)
  self.class.new(@filesystem.subsystem(id), @environment,
    global_variables: @opt_global_variables,
    data_sources: @data_sources,
    logger: @logger)
end