Class: ThemeBandit::RackGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_bandit/rack_generator.rb

Constant Summary collapse

THEME_DIR =
"#{Dir.pwd}/theme/"
TEMPLATE_ERROR =
'Templating engine not supported'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRackGenerator

Returns a new instance of RackGenerator.



14
15
16
17
# File 'lib/theme_bandit/rack_generator.rb', line 14

def initialize
  copy_template_to_dir(THEME_DIR)
  generate_view
end

Class Method Details

.buildObject



7
8
9
# File 'lib/theme_bandit/rack_generator.rb', line 7

def self.build
  new
end

Instance Method Details

#absolute_to_relative(contents) ⇒ Object



37
38
39
# File 'lib/theme_bandit/rack_generator.rb', line 37

def absolute_to_relative(contents)
  contents.gsub("#{Dir.pwd}/theme/public", '')
end

#copy_template_to_dir(destination) ⇒ Object

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



26
27
28
29
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/theme_bandit/rack_generator.rb', line 41

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) ⇒ Object



19
20
21
22
# 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_contentsObject



31
32
33
34
# 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