Class: Roll::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/roll/environment.rb

Overview

An Environment represents a set of libraries.

Defined Under Namespace

Classes: Index, Lookup

Constant Summary collapse

DEFAULT =

Default environment name.

'production'
DIRS =

Location of environment files. – Perhaps combine all enrtries instead? ++

::Config.find_config('roll', 'environments')
HOME_ENV_DIR =
File.join(::Config::CONFIG_HOME, 'roll', 'environments')
CURRENT_FILE =

File that stores the name of the current environment.

File.join(::Config::CONFIG_HOME, 'roll', 'current')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Environment

Instantiate environment.



66
67
68
# File 'lib/roll/environment.rb', line 66

def initialize(name=nil)
  @name = name || Environment.current
end

Instance Attribute Details

#nameObject (readonly)

Environment name.



63
64
65
# File 'lib/roll/environment.rb', line 63

def name
  @name
end

Class Method Details

.currentObject

Current environment name.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/roll/environment.rb', line 29

def self.current
  @current ||= (
    if File.exist?(CURRENT_FILE)
      env = File.read(CURRENT_FILE).strip
    else
      env = ENV['RUBYENV'] || DEFAULT
    end
    #warn "#{env} is not a valid environment" unless list.include?(env)
    env
  )
end

.listObject

List of available environments.



42
43
44
45
46
# File 'lib/roll/environment.rb', line 42

def self.list
  Dir[File.join('{'+DIRS.join(',')+'}', '*')].map do |file|
    File.basename(file)
  end
end

.save(name) ⇒ Object

Change environment to given name.

TODO: should only last a long as the shell session, not change it perminently.



53
54
55
56
57
58
59
60
# File 'lib/roll/environment.rb', line 53

def self.save(name)
  if name == 'system'
    FileUtils.rm(CURRENT_FILE)
  else
    File.open(CURRENT_FILE,'w'){|f| f << name.to_s}
  end
  CURRENT_FILE
end

Instance Method Details

#each(&block) ⇒ Object



91
92
93
# File 'lib/roll/environment.rb', line 91

def each(&block)
  index.each(&block)
end

#indexObject



71
72
73
# File 'lib/roll/environment.rb', line 71

def index
  @index ||= Index.new(name)
end

#lookupObject



76
77
78
# File 'lib/roll/environment.rb', line 76

def lookup
  @lookup ||= Lookup.new(name)
end

#saveObject

Save index.



86
87
88
# File 'lib/roll/environment.rb', line 86

def save
  index.save
end

#sizeObject



96
# File 'lib/roll/environment.rb', line 96

def size ; index.size ; end

#syncObject

Synchronize index to lookup table.



81
82
83
# File 'lib/roll/environment.rb', line 81

def sync
  index.reset(lookup.index)
end

#to_sObject



99
100
101
102
103
104
105
# File 'lib/roll/environment.rb', line 99

def to_s
  str = ""
  lookup.each do |(path, depth)|
    str << "#{path}  #{depth}\n"
  end
  str
end