class BoilerPlater::Parser::Section

Attributes

content[R]
file[R]
metadata[R]

Public Class Methods

new(content) click to toggle source
# File lib/parser.rb, line 69
def initialize(content)
  @content = content.split("\n")
  @metadata = parse_metadata(@content.shift.strip)
  @content = @content.join("\n")
  @file = @metadata[:filename]
end

Public Instance Methods

content?() click to toggle source
# File lib/parser.rb, line 96
def content?
  !content.empty?
end
download_content!() click to toggle source
# File lib/parser.rb, line 92
def download_content!
  @content = open(metadata[:url]).read
end
exists?() click to toggle source
# File lib/parser.rb, line 88
def exists?
  File.exists? path
end
path() click to toggle source
# File lib/parser.rb, line 83
def path
  FileUtils.mkdir_p(BoilerPlater.config.prefix) if !BoilerPlater.config.prefix.nil?
  File.join(BoilerPlater.config.prefix || '.', File.dirname(@file), File.basename(@file))
end
save!() click to toggle source
# File lib/parser.rb, line 76
def save!
  FileUtils.mkdir_p(File.dirname(path)) unless File.directory?(File.dirname(path))
  File.open(path, 'w') do |f|
    f.puts(content)
  end
end

Private Instance Methods

parse_metadata(line) click to toggle source
# File lib/parser.rb, line 102
def parse_metadata(line)
  raise 'Each section must start with ##' unless line =~ /^##/
  line.gsub!(/^##(\s+)/, '')
  line.split('->').inject({}) { |result, part|
    if part.strip =~ /^(\w+):\/\//
      result[:url] = part.strip
    elsif !part.nil?
      result[:filename] = part.strip
    end
    result
  }
end