Class: Kss::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/kss/section.rb

Overview

Public: Represents a styleguide section. Each section describes one UI element. A Section can be thought of as the collection of the description, modifiers, and styleguide reference.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment_text = nil, filename = nil) ⇒ Section

Public: Initialize a new Section

comment_text - The raw comment String, minus any comment syntax. filename - The filename as a String.



18
19
20
21
# File 'lib/kss/section.rb', line 18

def initialize(comment_text=nil, filename=nil)
  @raw = comment_text
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Public: Returns the filename where this section is found.



12
13
14
# File 'lib/kss/section.rb', line 12

def filename
  @filename
end

#rawObject (readonly)

Returns the raw comment text for the section, not including comment syntax (such as // or /* */).



9
10
11
# File 'lib/kss/section.rb', line 9

def raw
  @raw
end

Instance Method Details

#comment_sectionsObject

Splits up the raw comment text into comment sections that represent description, modifiers, etc.

Returns an Array of comment Strings.



27
28
29
# File 'lib/kss/section.rb', line 27

def comment_sections
  @comment_sections ||= raw ? raw.split("\n\n") : []
end

#descriptionObject

Public: The description section of a styleguide comment block.

Returns the description String.



50
51
52
# File 'lib/kss/section.rb', line 50

def description
  comment_sections.first
end

#modifiersObject

Public: The modifiers section of a styleguide comment block.

Returns an Array of Modifiers.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kss/section.rb', line 57

def modifiers
  last_indent = nil
  modifiers = []
  return modifiers unless comment_sections[1]

  comment_sections[1].split("\n").each do |line|
    next if line.strip.empty?
    indent = line.scan(/^\s*/)[0].to_s.size

    if last_indent && indent > last_indent
      modifiers.last.description += line.squeeze(" ")
    else
      modifier, desc = line.split(" - ")
      modifiers << Modifier.new(modifier.strip, desc.strip) if modifier && desc
    end

    last_indent = indent
  end

  modifiers
end

#sectionObject

Public: The styleguide section for which this comment block references.

Returns the section reference String (ex: “2.1.8”).



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kss/section.rb', line 34

def section
  return @section unless @section.nil?

  comment_sections.each do |text|
    if text =~ /Styleguide \d/i
      cleaned = text.strip.sub(/\.$/, '') # Kill trailing period
      @section = cleaned.match(/Styleguide (.+)/)[1]
    end
  end

  @section
end