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 { declaration block }

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

Example:

r = Rule.new ( "my rule" )
r.add(Declaration.new("margin", "1px"))
r.each_declaration do |dec|
  puts dec
end
puts r.name

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Rule

Create a new Rule with name



35
36
37
38
# File 'lib/csspress/rule.rb', line 35

def initialize(name)
  @name = name
  @declaration_block = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add(declaration) ⇒ Object

add a Declaration to the rule’s declaration block

Raises:

  • (ArgumentError)


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

def add(declaration)
  raise ArgumentError unless declaration.instance_of?(Declaration)
  @declaration_block << declaration
end

#declaration_blockObject

Return all declarations for the rule in an array



58
59
60
# File 'lib/csspress/rule.rb', line 58

def declaration_block
  @declaration_block.dup
end

#each_declarationObject

iterate through the declarations in the declaration block and pass each one to the block



50
51
52
53
54
# File 'lib/csspress/rule.rb', line 50

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

#to_sObject



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

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