Class: TemplateBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/csspress/template_builder.rb

Overview

TemplateBuilder is used to populate a template with a StyleSheet.

TemplateBuilder excepts a StyleSheet object and a template file. The template file contains erb tags that specify the layout of the output StyleSheet. TemplateBuilder will output the porulated template using the .publish() method.

If the template file supplied cannot be read an IOError is raised

Example:

ss = StyleSheet.new ( "foo.css" )
tb = TemplateBuilder.new ( ss )
puts tb.publish # output compressed version of ss

Constant Summary collapse

TEMPLATE_DIR =
"/../templates/"
DEFAULT =
"/../templates/default.csst"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(style_sheet, template = DEFAULT) ⇒ TemplateBuilder

Create a new TemplateBuilder for style_sheet using template.

Raises:

  • (ArgumentError)


38
39
40
41
42
# File 'lib/csspress/template_builder.rb', line 38

def initialize( style_sheet, template=DEFAULT )
  raise ArgumentError unless style_sheet.instance_of?(StyleSheet)
  @style_sheet = style_sheet
  @template =  template
end

Instance Attribute Details

#style_sheetObject (readonly)

Style sheet to publish



32
33
34
# File 'lib/csspress/template_builder.rb', line 32

def style_sheet
  @style_sheet
end

#templateObject (readonly)

Style sheet to publish



32
33
34
# File 'lib/csspress/template_builder.rb', line 32

def template
  @template
end

Instance Method Details

#publishObject

Publish StyleSheet using template. IOError raised if specified cannot be read.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/csspress/template_builder.rb', line 49

def publish
  begin
    template = File.read(File.dirname(__FILE__) + @template)
    engin = ERB.new(template)
    t = engin.result(binding())
    # Chomp the trailing newline
    t.gsub(/\n$/,'')
  rescue
    raise IOError, "Could not use requested template file: #{@template}"
  end
end