Class: ODDB::CssTemplate

Inherits:
Object show all
Defined in:
src/util/csstemplate.rb,
test/test_util/csstemplate.rb

Constant Summary

RESOURCE_PATH =
"../../test/data/css/"
TEMPLATE =
File.expand_path('./../data/css/template.css', File.dirname(__FILE__))
FLAVORS =
{
  :foo =>  {
    :bar_bg         =>  'black',
    :bar_txt_color  => 'white',
},
}
DEFAULT =
{
  :bar_bg          => 'white',
  :bar_txt_color =>  'black',
  :bar_link_color  => 'gold',
}

Class Method Summary (collapse)

Class Method Details

+ (Object) output_path(dirname)



284
285
286
287
# File 'src/util/csstemplate.rb', line 284

def output_path(dirname)
  path = RESOURCE_PATH + "#{dirname}/oddb.css"
  File.expand_path(path, File.dirname(__FILE__))
end

+ (Object) resolve(var, flavor)



288
289
290
291
292
293
# File 'src/util/csstemplate.rb', line 288

def resolve(var, flavor)
  key = var.intern
  flavor.fetch(key) {
    DEFAULT.fetch(key) { raise "could not find default for #{key}" }
  }
end

+ (Object) substitute(src, flavor)



294
295
296
297
298
# File 'src/util/csstemplate.rb', line 294

def substitute(src, flavor)
  src.gsub(/\$([^\s;]+)/u) { |match|
    resolve($1, flavor)
  }
end

+ (Object) write_css



299
300
301
302
303
304
305
306
307
308
309
# File 'src/util/csstemplate.rb', line 299

def write_css()
  FLAVORS.each { |name, flavor|
    src =  File.read(TEMPLATE)
    path = output_path(name)
    FileUtils.mkdir_p(File.dirname(path))
    File.open(path, "w") { |fh|
      fh << substitute(src, flavor)
    }
    File.chmod(0664, path)
  }
end