Class: Teapot::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/teapot/environment/base.rb,
lib/teapot/environment/system.rb,
lib/teapot/environment/flatten.rb,
lib/teapot/environment/evaluator.rb,
lib/teapot/environment/constructor.rb

Overview

This is the basic environment data structure which is essentially a linked list of hashes. It is primarily used for organising build configurations across a wide range of different sub-systems, e.g. platform configuration, target configuration, local project configuration, etc. The majority of the actual functionality is exposed in the ‘environment/*.rb` files.

Defined Under Namespace

Modules: System Classes: Constructor, Default, Evaluator, Replace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil, values = {}, &block) ⇒ Environment

Returns a new instance of Environment.



24
25
26
27
28
29
30
31
# File 'lib/teapot/environment/base.rb', line 24

def initialize(parent = nil, values = {}, &block)
  @values = (values || {}).to_hash
  @parent = parent
  
  if block_given?
    Constructor.new(self).instance_exec(&block)
  end
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



34
35
36
# File 'lib/teapot/environment/base.rb', line 34

def parent
  @parent
end

#valuesObject (readonly)

Returns the value of attribute values.



33
34
35
# File 'lib/teapot/environment/base.rb', line 33

def values
  @values
end

Class Method Details

.combine(*environments) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/teapot/environment/constructor.rb', line 58

def self.combine(*environments)
  # Flatten the list of environments:
  environments = environments.collect do |environment|
    if Environment === environment
      environment.to_a
    else
      environment
    end
  end.flatten
  
  # Resequence based on order:
  first = Environment.new(nil, environments.shift)
  top = first
  
  environments.each do |tail|
    top = Environment.new(top, tail)
  end
  
  return top
end

.system_environment(env = ENV) ⇒ Object

Construct an environment from a given system environment:



52
53
54
# File 'lib/teapot/environment/system.rb', line 52

def self.system_environment(env = ENV)
  self.new(Hash[env.map{|key, value| [key.downcase.to_sym, value]}])
end

Instance Method Details

#[](key) ⇒ Object



44
45
46
47
48
# File 'lib/teapot/environment/base.rb', line 44

def [] (key)
  environment = lookup(key)
  
  environment ? environment.values[key] : nil
end

#[]=(key, value) ⇒ Object



50
51
52
# File 'lib/teapot/environment/base.rb', line 50

def []= (key, value)
  @values[key] = value
end

#flattenObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/teapot/environment/flatten.rb', line 23

def flatten
  hash = {}
  
  # Flatten this chain of environments:
  flatten_to_hash(hash)
  
  # Evaluate all items to their respective object value:
  evaluator = Evaluator.new(hash)
  
  # Evaluate all the individual environment values so that they are flat:
  Hash[hash.map{|key, value| [key, evaluator.object_value(value)]}]
end

#lookup(name) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/teapot/environment/base.rb', line 36

def lookup(name)
  if @values.include? name
    self
  elsif @parent
    @parent.lookup(name)
  end
end

#merge(&block) ⇒ Object



79
80
81
82
83
84
# File 'lib/teapot/environment/constructor.rb', line 79

def merge(&block)
  self.class.combine(
    self,
    self.class.new(&block)
  )
end

#to_aObject

Convert the hierarchy of environments to an array where the parent comes before the child.



87
88
89
90
91
92
93
# File 'lib/teapot/environment/constructor.rb', line 87

def to_a
  flat = []
  
  flatten_to_array(flat)
  
  return flat
end

#to_hashObject



54
55
56
# File 'lib/teapot/environment/base.rb', line 54

def to_hash
  @values
end

#to_sObject



58
59
60
# File 'lib/teapot/environment/base.rb', line 58

def to_s
  "<#{self.class} #{self.values}>"
end