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.

Example:

ss = StyleSheet.new
ss.add( Rule.new("A Rule") )
tb = TemplateBuilder.new ( ss )
puts tb.publish # output compressed version of ss

Instance Method Summary collapse

Constructor Details

#initializeStyleSheet

Returns a new instance of StyleSheet.



23
24
25
26
# File 'lib/csspress/style_sheet.rb', line 23

def initialize
  # Store style rules in order
  @rules = []
end

Instance Method Details

#add(rule) ⇒ Object

Add rule to StyleSheet Raises ArgumentError rule is not a Rule

Raises:

  • (ArgumentError)


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

def add(rule)
  raise ArgumentError unless rule.instance_of?(Rule) 
  @rules << rule
  @self
end

#each_ruleObject

Iterate through all rules in self and yield them to a block



43
44
45
# File 'lib/csspress/style_sheet.rb', line 43

def each_rule
  @rules.each {|r| yield r }
end

#rulesObject

Return an array of all rules in self



48
49
50
# File 'lib/csspress/style_sheet.rb', line 48

def rules
  @rules.dup
end

#sizeObject

The number of rules in self



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

def size
  @rules.size
end

#to_sObject



52
53
54
55
56
57
58
# File 'lib/csspress/style_sheet.rb', line 52

def to_s
  out = ""
  @rules.each do |rule|
    out << rule.to_s
  end
  out
end