Sha256: e8b80622cb6ea24a5fa45f70b92bef3814768e433329bea76476d8e95a7e84b0

Contents?: true

Size: 871 Bytes

Versions: 6

Compression:

Stored size: 871 Bytes

Contents

require "style_guide/partial"

module StyleGuide
  class Section
    attr_reader :path, :id

    def self.id_from_path(path)
      File.basename(path).downcase.gsub(/[^a-zA-Z0-9]/, " ").strip.gsub(/\s+/, "_")
    end

    def self.from_paths(paths)
      [*paths].reduce({}) do |sections, path|
        id = id_from_path(path)
        section = sections[id] ||= []
        section << new("#{id}#{section.empty? ? '' : section.count}", path)
        sections
      end.values.flatten
    end

    def initialize(id, path)
      @id = id
      @path = path
    end

    def title
      @title ||= File.basename(path).titleize
    end

    def partials
      partial_paths.map { |path| StyleGuide::Partial.new(path, self) }.sort_by { |p| p.title }
    end

    private

    def partial_paths
      @partial_paths ||= Dir.glob(File.expand_path("_*", path))
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
style-guide-1.1.1 lib/style_guide/section.rb
style-guide-1.1.0 lib/style_guide/section.rb
style-guide-1.0.0 lib/style_guide/section.rb
style-guide-0.7.4 lib/style_guide/section.rb
style-guide-0.7.3 lib/style_guide/section.rb
style-guide-0.7.2 lib/style_guide/section.rb