class ThemeBandit::RackGenerator

Constants

TEMPLATE_ERROR
THEME_DIR

Public Class Methods

build() click to toggle source
# File lib/theme_bandit/rack_generator.rb, line 7
def self.build
  new
end
new() click to toggle source
# File lib/theme_bandit/rack_generator.rb, line 14
def initialize
  copy_template_to_dir(THEME_DIR)
  generate_view
end

Public Instance Methods

absolute_to_relative(contents) click to toggle source
# File lib/theme_bandit/rack_generator.rb, line 36
def absolute_to_relative(contents)
  contents.gsub("#{Dir.pwd}/theme/public", '')
end
copy_template_to_dir(destination) click to toggle source

NOTE: to copy the innards of a dir, use a /., with a dot at the end example - recipes/sinatra/.

# File lib/theme_bandit/rack_generator.rb, line 26
def copy_template_to_dir(destination)
  t = get_recipe
  FileUtils.cp_r t, destination
end
generate_view(root = Dir.pwd) click to toggle source
# File lib/theme_bandit/rack_generator.rb, line 40
def generate_view(root = Dir.pwd)
  case ThemeBandit.configuration.template_engine
  when 'slim'
    slim_contents = HTML2Slim.convert!(index_file_contents, :html)
    File.open("#{root}/theme/app/views/templates/index.slim", 'w') { |file| file.write(slim_contents) }
  when ('erb' || 'html')
    File.open("#{root}/theme/app/views/templates/index.erb", 'w') { |file| file.write(index_file_contents) }
  when 'haml'
    haml_contents = Haml::HTML.new(index_file_contents, erb: nil).render
    File.open("#{root}/theme/app/views/templates/index.haml", 'w') { |file| file.write(haml_contents) }
  else
    fail TEMPLATE_ERROR
  end
end
get_recipe(root = ThemeBandit.configuration.gem_root) click to toggle source
# File lib/theme_bandit/rack_generator.rb, line 19
def get_recipe(root = ThemeBandit.configuration.gem_root)
  src = "#{root}/lib/theme_bandit/recipes/sinatra/#{ThemeBandit.configuration.template_engine}/"
  Dir.glob("#{src}/**/*")
end
index_file_contents() click to toggle source
# File lib/theme_bandit/rack_generator.rb, line 31
def index_file_contents
  index_html = File.open("#{Dir.pwd}/theme/public/index.html", 'r')
  absolute_to_relative(File.read(index_html))
end