Class: Stylish::Declaration

Inherits:
Object
  • Object
show all
Includes:
Formattable
Defined in:
lib/stylish/core.rb

Overview

Each Rule may have one or more Declaration objects, and usually has more than one. In a sense, declarations are the business end of stylesheets: they are where the set of elements specified by a rule’s selectors are given their various attributes.

Direct Known Subclasses

Background

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Formattable

included

Constructor Details

#initialize(name, value) ⇒ Declaration

Each Declaration has a property name and a value.



260
261
262
263
# File 'lib/stylish/core.rb', line 260

def initialize(name, value)
  self.value = value
  self.name  = name
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



256
257
258
# File 'lib/stylish/core.rb', line 256

def value
  @value
end

Instance Method Details

#nameObject

The property name of the Declaration.



266
267
268
# File 'lib/stylish/core.rb', line 266

def name
  @property_name
end

#name=(name) ⇒ Object

Property names are CSS identifiers.



271
272
273
# File 'lib/stylish/core.rb', line 271

def name=(name)
  @property_name = name.to_s
end

#to_s(symbols = {}) ⇒ Object

Serialising a declaration produces a name-value pair separated by a colon and terminating with a semicolon, for instance

declaration = Declaration.new("font-style", "italic")
declaration.to_s # => "font-style:italic;"

Since the formatting can be adjusted via the #format= accessor, the exact spacing of the declaration can be controlled if desired.



288
289
290
291
292
293
294
295
296
# File 'lib/stylish/core.rb', line 288

def to_s(symbols = {})
  if @value.is_a?(Generate::Variable) || @value.is_a?(Color)
    value = @value.to_s(symbols)
  else
    value = @value.to_s
  end
  
  sprintf(self.class.format, @property_name.to_s, value)
end