Class: Rule

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

Overview

Rule represents a rule in a style sheet. An example of a rule would be:

body { margin : 0; padding : 0 }

The components of a rule are:

name { declarations }

All declarations of Rule can be accessed using the .each_declaration(){} method where each declaration is passed to the block as a Declaration object.

Example:

r = Rule.new ( "foo { margin: 0; padding: 0 }" )
r.each_declaration do |dec|
  puts dec
end
puts r.name

Defined Under Namespace

Classes: RuleParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Rule

Create a new Rule from a string (The string should be a valid CSS rule!)



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

def initialize( string )
  rp = RuleParser.new( string )
  @name = rp.name
  @declarations = rp.declarations
end

Instance Attribute Details

#declarationsObject (readonly)

declarations contained within Rule



33
34
35
# File 'lib/csspress/rule.rb', line 33

def declarations
  @declarations
end

#nameObject (readonly)

name of the Rule



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

def name
  @name
end

Instance Method Details

#each_declarationObject

Yeild all declarations for self



46
47
48
49
50
# File 'lib/csspress/rule.rb', line 46

def each_declaration
  @declarations.each do |declaration|
    yield declaration
  end
end

#to_sObject

Print out the rule neatly



54
55
56
# File 'lib/csspress/rule.rb', line 54

def to_s
  "#{@name}{#{@declarations.join(";")}}"
end