Class: Stylish::Generate::Description
- Inherits:
-
Object
- Object
- Stylish::Generate::Description
- Includes:
- ElementMethods
- Defined in:
- lib/stylish/generate.rb
Overview
Description objects are the core of the stylesheet generation DSL. Blocks passed into the +Stylish#generate_ method are executed in the context of a Description object. All the DSL methods, rule, comment, and all the HTML element methods are all methods on instances of the Description class.
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Instance Method Summary collapse
-
#comment(*args) ⇒ Object
Adds a
Commentobject to the current node. -
#initialize(context = nil) ⇒ Description
constructor
Descriptioninstances are associated with a particular node in a selector tree; if no node is assigned to them on creation, they associate with a new root, i.e. -
#rule(selectors, declarations = nil, &block) ⇒ Object
The
rulemethod is the most general and powerful part of the DSL.
Constructor Details
#initialize(context = nil) ⇒ Description
Description instances are associated with a particular node in a selector tree; if no node is assigned to them on creation, they associate with a new root, i.e. a Stylesheet object.
168 169 170 |
# File 'lib/stylish/generate.rb', line 168 def initialize(context = nil) @node = context || Stylesheet.new end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
163 164 165 |
# File 'lib/stylish/generate.rb', line 163 def node @node end |
Instance Method Details
#comment(*args) ⇒ Object
Adds a Comment object to the current node. This method simply hands its arguments off to the Comment initialiser, and hence implements its API precisely.
246 247 248 |
# File 'lib/stylish/generate.rb', line 246 def comment(*args) @node << Comment.new(*args) end |
#rule(selectors, declarations = nil, &block) ⇒ Object
The rule method is the most general and powerful part of the DSL. It can be used to add a single Rule, with attendant declarations, or to create a selector namespace within which further rules or namespaces can be added.
The nested structure created is precisely that of a selector tree; the rule method adds Rule leaves and SelectorScope nodes to the tree, whose root is a Stylesheet object.
Either a set of declarations or a block must be passed to the method; failing to do so will result in an early return which creates no additional objects. The following example demonstrates the various ways in which the method can be used:
Stylish.generate do
rule ".section", :margin_bottom => "10px"
rule "form" do
rule ".notice", :color => "#00f"
rule "input[type=submit]", :font_weight => "normal"
end
rule "body", :padding => "0.5em" do
rule "div", :margin => "2px"
end
end
This would produce a stylesheet with the following rules:
.section {margin-bottom:10px;}
form .notice {color:#00f;}
form input[type=submit] {font-weight:normal;}
body {padding:0.5em;}
body div {margin:2px;}
Usefully, a call to #rule which passes in both declarations and a block will produce a single Rule with the declarations attached, then create a new SelectorScope node with the same selectors and execute the block in that context.
If several selectors and a block are passed to rule, new SelectorScope nodes will be created for each selector and the block will be executed in all the contexts created, not just one.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/stylish/generate.rb', line 217 def rule(selectors, declarations = nil, &block) return unless declarations || block selectors = [selectors] unless selectors.is_a?(Array) selectors.map! do |s| Selector.new(s.is_a?(Symbol) ? Variable.new(s) : s) end declarations = Generate.parse_declarations(declarations) unless block @node << Rule.new(selectors, declarations) else selectors.each do |selector| unless declarations.empty? @node << Rule.new([selector], declarations) end new_node = Tree::SelectorScope.new(selector) @node << new_node self.class.new(new_node).instance_eval(&block) end end end |