Class: StyleSheet

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

Overview

StyleSheet is a container class for Rule objects which can be added individually using the .add() method. The Rule objects that StyleSheet contains can be accessed using the .each_rule(){} method which yields each Rule to the block.

StyleSheet can also accept a CSS file as a paramiter to .new or using the .load() method. This file is then converted into Rule objects and they are added to StyleSheet. A compressed version of StyleSheet can be output using a TemplateBuilder object and passing it StyleSheet as a parameter.

Example:

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

Defined Under Namespace

Classes: StyleSheetParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ StyleSheet

Creates a new StyleSheet object. Can also accept a file name and parse the contents to initalize the StyleSheet with the file’s contents.



37
38
39
40
# File 'lib/csspress/style_sheet.rb', line 37

def initialize(file=nil)
  @rules = []
  load file unless file.nil?
end

Instance Attribute Details

#rulesObject (readonly)

rule objects


TODO: remove this and only allow access through each_rule. +++



31
32
33
# File 'lib/csspress/style_sheet.rb', line 31

def rules
  @rules
end

Instance Method Details

#add(rule) ⇒ Object

Add a rule to self



69
70
71
72
# File 'lib/csspress/style_sheet.rb', line 69

def add(rule)
  @rules << Rule.new(rule)
  self
end

#each_ruleObject

Yeild each style rule in self



44
45
46
47
48
# File 'lib/csspress/style_sheet.rb', line 44

def each_rule
  @rules.each do |rule|
    yield rule
  end
end

#lengthObject

Alias of size



92
93
94
# File 'lib/csspress/style_sheet.rb', line 92

def length
  size
end

#load(file) ⇒ Object

Load a CSS file into self



62
63
64
65
# File 'lib/csspress/style_sheet.rb', line 62

def load(file)
  ssp = StyleSheetParser.new(file)
  ssp.each { |rule| add(rule) }
end

#sizeObject

Number of Rules in Self



86
87
88
# File 'lib/csspress/style_sheet.rb', line 86

def size
  @rules.size
end

#to_sObject

Output self as String



76
77
78
79
80
81
82
# File 'lib/csspress/style_sheet.rb', line 76

def to_s
  out = ""
  self.each_rule do |rule|
    out << "#{rule}\n"
  end
  out
end