Module: Stylish::Formattable

Included in:
Declaration, Declarations, Image, Rule, Selectors, Tree::SelectorScope
Defined in:
lib/stylish/formattable.rb

Overview

The Formattable mixin gives an API for changing the output format of any of Stylish’s core objects, as long as the new output format matches the regex provided in the object’s initialize method. This is because in order to serialise a valid CSS document, strings must be joined in the correct way.

For example, a rule’s selectors must be comma-separated and its declarations must be wrapped in curly braces, while each declaration consists of a property name and a value separated by a colon and terminating with a semicolon.

In order to employ the Formattable mixin, it must be included into a class and the #accept_format method must be called in the class’s #initialize method, setting the allowed format and the default format.

class Stylesheet
  include Formattable

  def initialize
    accept_format(/\s*/m, "\n")
  end
end

If one then creates a new Stylesheet object, one can modify the way it’s formatted when serialised to CSS code.

stylesheet = Stylesheet.new
stylesheet.format # => "\n"
stylesheet.format = "\n\n"
stylesheet.format # => "\n\n"

Instance Attribute Summary collapse

Instance Attribute Details

#formatObject

Returns the value of attribute format.



34
35
36
# File 'lib/stylish/formattable.rb', line 34

def format
  @format
end