Class: Tap::Env::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/tap/env/context.rb

Overview

Context instances track information shared by a set of Env instances, for instance cached manifest data. Caching cross-env data in a shared space simplifies managment of this data, especially when dumping and loading it from a static file.

Contexts also ensure that only one env in initialized to a given directory (at least among envs that share the same context). This prevents errors that arise when one env eventually nests itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Context

Initializes a new Context. Options can specify a cache or a basename.



24
25
26
27
28
29
30
31
32
33
# File 'lib/tap/env/context.rb', line 24

def initialize(options={})
  options = {
    :cache => {},
    :basename => nil
  }.merge(options)
  
  @cache = options[:cache]
  @basename = options[:basename]
  @instances = []
end

Instance Attribute Details

#basenameObject (readonly)

The config file basename



18
19
20
# File 'lib/tap/env/context.rb', line 18

def basename
  @basename
end

#cacheObject (readonly)

A hash of cached manifest data



15
16
17
# File 'lib/tap/env/context.rb', line 15

def cache
  @cache
end

#instancesObject (readonly)

An array of Env instances registered with self



21
22
23
# File 'lib/tap/env/context.rb', line 21

def instances
  @instances
end

Instance Method Details

#config_file(dir) ⇒ Object

Returns the config filepath for the directory (ie basename under dir). If basename is nil, then config_file always return nil.



56
57
58
# File 'lib/tap/env/context.rb', line 56

def config_file(dir)
  basename ? File.join(dir, basename) : nil
end

#instance(dir) ⇒ Object

Gets the instance for the directory currently in instances, or nil if such an instance does not exist.



50
51
52
# File 'lib/tap/env/context.rb', line 50

def instance(dir)
  instances.find {|env| env.root.root == dir }
end

#register(env) ⇒ Object

Registers env with self by adding env to instances. Raises an error if instances contains an env with the same root directory.



37
38
39
40
41
42
43
44
45
46
# File 'lib/tap/env/context.rb', line 37

def register(env)
  path = env.root.root
  
  if instance(path)
    raise "context already has an env for: #{path}"
  end
  
  instances << env
  self
end