Class: ASIR::Environment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



11
12
13
14
15
# File 'lib/asir/environment.rb', line 11

def initialize
  @verbose = 0
  @exit_code = 0
  @options = { }
end

Instance Attribute Details

#adjectiveObject

Returns the value of attribute adjective.



5
6
7
# File 'lib/asir/environment.rb', line 5

def adjective
  @adjective
end

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/asir/environment.rb', line 6

def config
  @config
end

#config_rbObject

Returns the value of attribute config_rb.



6
7
8
# File 'lib/asir/environment.rb', line 6

def config_rb
  @config_rb
end

#identifierObject

Returns the value of attribute identifier.



5
6
7
# File 'lib/asir/environment.rb', line 5

def identifier
  @identifier
end

#log_dirObject

Returns the value of attribute log_dir.



7
8
9
# File 'lib/asir/environment.rb', line 7

def log_dir
  @log_dir
end

#log_fileObject

Returns the value of attribute log_file.



7
8
9
# File 'lib/asir/environment.rb', line 7

def log_file
  @log_file
end

#objectObject

Returns the value of attribute object.



5
6
7
# File 'lib/asir/environment.rb', line 5

def object
  @object
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/asir/environment.rb', line 8

def options
  @options
end

#phaseObject

Returns the value of attribute phase.



4
5
6
# File 'lib/asir/environment.rb', line 4

def phase
  @phase
end

#pid_dirObject

Returns the value of attribute pid_dir.



7
8
9
# File 'lib/asir/environment.rb', line 7

def pid_dir
  @pid_dir
end

#verbObject

Returns the value of attribute verb.



5
6
7
# File 'lib/asir/environment.rb', line 5

def verb
  @verb
end

#verboseObject

Returns the value of attribute verbose.



9
10
11
# File 'lib/asir/environment.rb', line 9

def verbose
  @verbose
end

Instance Method Details

#asir_basenameObject



51
52
53
# File 'lib/asir/environment.rb', line 51

def asir_basename
  "asir-#{adjective}-#{object}-#{identifier}"
end

#config!(phase, opts = { }) ⇒ Object



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

def config! phase, opts = { }
  ((@config ||= { })[phase] ||= [
    begin
      opts[:phase] = phase
      save = { }
      opts.each do | k, v |
        save[k] = send(k)
        send(:"#{k}=", v)
      end
      $stderr.puts "#{self.class} calling #{config_rb} asir.phase=#{phase.inspect} ..." if @verbose >= 1
      value = config_lambda.call(self)
      $stderr.puts "#{self.class} calling #{config_rb} asir.phase=#{phase.inspect} DONE" if @verbose >= 1
      value
    ensure
      save.each do | k , v |
        send(:"#{k}=", v)
      end
    end
    ]).first
end

#config_lambdaObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/asir/environment.rb', line 81

def config_lambda
  @config_lambda ||=
    (
      file = config_rb
      $stderr.puts "#{self.class} loading #{file} ..." if @verbose >= 1
      expr = File.read(file)
      expr = "begin; lambda do | asir |; #{expr}\n end; end"
      cfg = Object.new.send(:eval, expr, binding, file, 1)
      # cfg = load file
      # $stderr.puts "#{self.class} loading #{file} DONE" if @verbose >= 1
      raise "#{file} did not return a Proc, returned a #{cfg.class}" unless Proc === cfg
      cfg
    )
end

#find_writable_directory(kind, *list) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/asir/environment.rb', line 33

def find_writable_directory kind, *list
  list.
    reject { | p | ! p }.
    map { | p |  File.expand_path(p) }.
    find { | p | File.writable?(p) } or
    raise "Cannot find writable directory for #{kind}"
end

#pid_fileObject



41
42
43
44
# File 'lib/asir/environment.rb', line 41

def pid_file
  @pid_file ||=
    "#{pid_dir}/#{asir_basename}.pid"
end