Class: Codestrap::Config

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

Overview

Loaded variables from Codestrapfile presented as getters

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



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

Instance Attribute Details

#envHash

Environment variables. Defaults to system environment variables

Returns:

  • (Hash)


128
129
130
# File 'lib/codestrap/config.rb', line 128

def env
  @env ||= ENV.to_hash
end

Class Method Details

.localObject



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

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

.serverObject



199
200
201
202
# File 'lib/codestrap/config.rb', line 199

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

Instance Method Details

#codestrapfile_mtime(codestrapfile) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/codestrap/config.rb', line 132

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/codestrap/config.rb', line 170

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



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

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

#reload_on_changeObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/codestrap/config.rb', line 143

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



204
205
206
207
# File 'lib/codestrap/config.rb', line 204

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