Class: Codestrap::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/codestrap/config.rb

Overview

Loaded variables from Codestrapfile presented as getters

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/codestrap/config.rb', line 107

def initialize
  # Setup defaults
  Codestrapfile.config do |conf|
    conf.local.base  = %W[codestrap .codestrap].map { |d| File.join(ENV['HOME'], d) }
    conf.local.ignore = []
    conf.server.bind = '127.0.0.1'
    conf.server.port = '4567'
    conf.server.base = conf.local.base
    conf.server.ignore = conf.local.ignore
  end

  @@codestrapfile_mtime = Time.new(0)
  load_codestrapfile
end

Class Method Details

.localObject



179
180
181
182
# File 'lib/codestrap/config.rb', line 179

def self.local
  @@config ||= Codestrapfile.new
  @@config.local
end

.serverObject



189
190
191
192
# File 'lib/codestrap/config.rb', line 189

def self.server
  @@config ||= Codestrapfile.new
  @@config.server
end

Instance Method Details

#codestrapfile_mtime(codestrapfile) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/codestrap/config.rb', line 122

def codestrapfile_mtime(codestrapfile)
  if not codestrapfile and ENV['CODESTRAPFILE'] and File.exist? ENV['CODESTRAPFILE']
    codestrapfile = ENV['CODESTRAPFILE']
  end
  if codestrapfile
    return File::Stat.new(codestrapfile).mtime
  else
    return Time.new(0)
  end
end

#load_codestrapfile(codestrapfile = nil) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/codestrap/config.rb', line 160

def load_codestrapfile(codestrapfile=nil)
  codestrapfile_mtime = nil
  unless codestrapfile
    # Load possible codestrapfiles
    [ENV['CODESTRAPFILE'], File.join(ENV['HOME'], 'codestrap', 'Codestrapfile'), File.join(ENV['HOME'], '.codestrap', 'Codestrapfile')].each do |sf|
      next unless sf and File.exist?(sf)
      codestrapfile       = sf
      codestrapfile_mtime = codestrapfile_mtime(sf)
      break
    end
  end

  if codestrapfile
    load codestrapfile
    @@codestrapfile       = codestrapfile
    @@codestrapfile_mtime = codestrapfile_mtime
  end
end

#localObject



184
185
186
187
# File 'lib/codestrap/config.rb', line 184

def local
  @@config ||= Codestrapfile.new
  @@config.local
end

#reload_on_changeObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/codestrap/config.rb', line 133

def reload_on_change
  reload   = false
  mtime    = nil
  codestrapfile = nil
  if ENV['CODESTRAPFILE'] and !@@codestrapfile
    reload   = true
    codestrapfile = ENV['CODESTRAPFILE']
    mtime    = codestrapfile_mtime(codestrapfile)
  elsif ENV['CODESTRAPFILE'] and !ENV['CODESTRAPFILE'].eql?(@@codestrapfile)
    reload   = true
    codestrapfile = ENV['CODESTRAPFILE']
    mtime    = codestrapfile_mtime(codestrapfile)
  elsif @@codestrapfile
    mtime = codestrapfile_mtime(@@codestrapfile)
    if mtime > @@codestrapfile_mtime
      reload = true
    end
  end
  if reload
    load codestrapfile
    @@codestrapfile       = codestrapfile
    @@codestrapfile_mtime = mtime
    true
  end
  false
end

#serverObject



194
195
196
197
# File 'lib/codestrap/config.rb', line 194

def server
  @@config ||= Codestrapfile.new
  @@config.server
end