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

Attributes included from Formattable

#format

Instance Method Summary collapse

Constructor Details

#initialize(name, value) ⇒ Declaration

Each Declaration has a property name and a value.



238
239
240
241
242
# File 'lib/stylish/core.rb', line 238

def initialize(name, value)
  accept_format(/^\s*%s\s*:\s*%s;\s*$/m, "%s:%s;")
  self.value = value
  self.name  = name
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



235
236
237
# File 'lib/stylish/core.rb', line 235

def value
  @value
end

Instance Method Details

#nameObject

The property name of the Declaration.



245
246
247
# File 'lib/stylish/core.rb', line 245

def name
  @property_name
end

#name=(name) ⇒ Object

Property names are CSS identifiers.



250
251
252
# File 'lib/stylish/core.rb', line 250

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.



267
268
269
270
271
272
273
274
275
# File 'lib/stylish/core.rb', line 267

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(@format, @property_name.to_s, value)
end