Class: Stylish::Comment
- Inherits:
-
Object
- Object
- Stylish::Comment
- Includes:
- Tree::Leaf
- Defined in:
- lib/stylish/core.rb
Overview
Comment objects form the other concrete leaves of selector trees, and allow stylesheets to be annotated at any point (outside Rules). Their serialisation format follows the well-known JavaDoc and PHPDoc style, with a header, several lines of notes, and key-value information.
This format is not amendable; those desirous of their own formatting would be better served by creating their own Comment class, such as the following more basic one:
module Stylish
module Extensions
class Comment
include Tree::Leaf
attr_accessor :content
def initialize(content)
@content = content
end
def to_s(scope = "")
"/* " + content.to_s + " */"
end
end
end
end
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Comment
constructor
Each Comment can have a header, additional lines of text content (each provided as its own argument), and key-value metadata passed in as a Ruby Hash object.
-
#to_s(symbols = {}, scope = "") ⇒ Object
As Comment objects are the leaves of selector trees, they must implement the serialisation API of those trees, and thus the #to_s method has a scope argument, which is in practice discarded when the serialisation of the comment occurs.
Methods included from Tree::Leaf
Methods included from Tree::Node
Constructor Details
#initialize(*args) ⇒ Comment
Each Comment can have a header, additional lines of text content (each provided as its own argument), and key-value metadata passed in as a Ruby Hash object.
comment = Comment.new("My wonderful comment",
"It has several lines of insightful notes,",
"filled with wisdom and the knowledge of ages.",
{:author => "Some Egotist"})
comment.to_s # => /**
# * My wonderful comment
# *
# * It has several lines of insightful notes,
# * filled with wisdom and the knowledge of ages.
# *
# * @author Some Egotist
# */
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/stylish/core.rb', line 134 def initialize(*args) @lines, @metadata = [], {} args.each do |arg| if arg.is_a? String unless @header.nil? @lines << arg else @header = arg end elsif arg.is_a? Hash @metadata.merge! arg end end end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
114 115 116 |
# File 'lib/stylish/core.rb', line 114 def header @header end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
114 115 116 |
# File 'lib/stylish/core.rb', line 114 def lines @lines end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
114 115 116 |
# File 'lib/stylish/core.rb', line 114 def @metadata end |
Instance Method Details
#to_s(symbols = {}, scope = "") ⇒ Object
As Comment objects are the leaves of selector trees, they must implement the serialisation API of those trees, and thus the #to_s method has a scope argument, which is in practice discarded when the serialisation of the comment occurs.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/stylish/core.rb', line 154 def to_s(symbols = {}, scope = "") if @lines.empty? && @metadata.empty? sprintf("/**\n * %s\n */", @header) else header = sprintf(" * %s", @header) unless @header.nil? lines = @lines.map {|l| ' * ' + l }.join("\n") unless @lines.empty? = @metadata.to_a.map {|name, value| sprintf(" * @%s %s", name.to_s, value.to_s) }.join("\n") unless @metadata.empty? sprintf("/**\n%s\n */", [ header || nil, lines || nil, || nil ].compact.join("\n *\n")) end end |