Class: Stylish::Rule
- Inherits:
-
Object
- Object
- Stylish::Rule
- Includes:
- Formattable, Tree::Leaf
- Defined in:
- lib/stylish/core.rb
Overview
Rule objects represent CSS rules, and serialise to them. They possess one or more selectors, and zero or more declarations. In addition to their importance as the major building-blocks of stylesheets, they act as the leaves of Stylish’s stylesheet trees.
Their serialisation is controllable to some extent by altering their format attribute; this should never make them lose information when they are serialised.
E.g., by changing its format to “%s %sn” a rule that would normally be serialised thus:
body {font-size:81%;}
Would instead be serialised like this:
body {
font-size:81%;
}
Instance Attribute Summary collapse
-
#declarations ⇒ Object
readonly
Returns the value of attribute declarations.
-
#selectors ⇒ Object
readonly
Returns the value of attribute selectors.
Instance Method Summary collapse
-
#initialize(selectors, declarations) ⇒ Rule
constructor
Every Rule must have at least one selector, but may have any number of declarations.
-
#to_s(symbols = {}, scope = "") ⇒ Object
Serialise the rule to valid CSS code.
Methods included from Formattable
Methods included from Tree::Leaf
Methods included from Tree::Node
Constructor Details
#initialize(selectors, declarations) ⇒ Rule
Every Rule must have at least one selector, but may have any number of declarations. Empty rules are often used in stylesheets to indicate particular combinations of selectors which may be used to produce particular effects.
In Stylish, of course, a Rule’s declarations may be amended after its creation:
rule = Stylish::Rule.new([Stylish::Selector.new("body")])
rule.declarations << Stylish::Declaration.new("font-weight", "bold")
rule.to_s # => "body {font-weight:bold;}"
This makes Rule objects a very flexible foundation for the higher-level data structures and APIs in Stylish.
82 83 84 85 86 87 88 89 90 |
# File 'lib/stylish/core.rb', line 82 def initialize(selectors, declarations) @selectors = selectors.inject(Selectors.new) do |ss, s| ss << s end @declarations = declarations.inject(Declarations.new) do |ds, d| ds << d end end |
Instance Attribute Details
#declarations ⇒ Object (readonly)
Returns the value of attribute declarations.
65 66 67 |
# File 'lib/stylish/core.rb', line 65 def declarations @declarations end |
#selectors ⇒ Object (readonly)
Returns the value of attribute selectors.
65 66 67 |
# File 'lib/stylish/core.rb', line 65 def selectors @selectors end |
Instance Method Details
#to_s(symbols = {}, scope = "") ⇒ Object
Serialise the rule to valid CSS code.
93 94 95 96 |
# File 'lib/stylish/core.rb', line 93 def to_s(symbols = {}, scope = "") sprintf(self.class.format, selectors.join(symbols, scope), @declarations.to_s(symbols)) end |