Class: ConvertTheme
- Inherits:
-
Object
- Object
- ConvertTheme
- Defined in:
- lib/convert_theme.rb,
lib/convert_theme/cli.rb
Defined Under Namespace
Classes: CLI, ConvertThemeGenerator
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Attribute Summary collapse
-
#content_id ⇒ Object
readonly
Returns the value of attribute content_id.
-
#image_dir ⇒ Object
readonly
Returns the value of attribute image_dir.
-
#index_path ⇒ Object
readonly
Returns the value of attribute index_path.
-
#javascript_dir ⇒ Object
readonly
Returns the value of attribute javascript_dir.
-
#rails_path ⇒ Object
readonly
Returns the value of attribute rails_path.
-
#stylesheet_dir ⇒ Object
readonly
Returns the value of attribute stylesheet_dir.
-
#template_root ⇒ Object
readonly
Returns the value of attribute template_root.
-
#template_type ⇒ Object
readonly
Returns the value of attribute template_type.
Instance Method Summary collapse
- #apply_to(rails_path, options = {}) ⇒ Object
- #erb? ⇒ Boolean
- #haml? ⇒ Boolean
-
#initialize(options = {}) ⇒ ConvertTheme
constructor
A new instance of ConvertTheme.
- #setup_template_temp_path ⇒ Object
-
#template_temp_path ⇒ Object
This generator’s templates folder is temporary and is accessed via source_root within the generator.
Constructor Details
#initialize(options = {}) ⇒ ConvertTheme
Returns a new instance of ConvertTheme.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/convert_theme.rb', line 15 def initialize( = {}) @template_root = File.([:template_root] || File.dirname('.')) @index_path = [:index_path] || "index.html" @content_id = [:content_id] || "content" @stylesheet_dir = [:stylesheet_dir] || detect_stylesheet_dir @javascript_dir = [:javascript_dir] || detect_javascript_dir @image_dir = [:image_dir] || detect_image_dir @stdout = [:stdout] || $stdout setup_template_temp_path end |
Instance Attribute Details
#content_id ⇒ Object (readonly)
Returns the value of attribute content_id.
13 14 15 |
# File 'lib/convert_theme.rb', line 13 def content_id @content_id end |
#image_dir ⇒ Object (readonly)
Returns the value of attribute image_dir.
12 13 14 |
# File 'lib/convert_theme.rb', line 12 def image_dir @image_dir end |
#index_path ⇒ Object (readonly)
Returns the value of attribute index_path.
11 12 13 |
# File 'lib/convert_theme.rb', line 11 def index_path @index_path end |
#javascript_dir ⇒ Object (readonly)
Returns the value of attribute javascript_dir.
12 13 14 |
# File 'lib/convert_theme.rb', line 12 def javascript_dir @javascript_dir end |
#rails_path ⇒ Object (readonly)
Returns the value of attribute rails_path.
11 12 13 |
# File 'lib/convert_theme.rb', line 11 def rails_path @rails_path end |
#stylesheet_dir ⇒ Object (readonly)
Returns the value of attribute stylesheet_dir.
12 13 14 |
# File 'lib/convert_theme.rb', line 12 def stylesheet_dir @stylesheet_dir end |
#template_root ⇒ Object (readonly)
Returns the value of attribute template_root.
11 12 13 |
# File 'lib/convert_theme.rb', line 11 def template_root @template_root end |
#template_type ⇒ Object (readonly)
Returns the value of attribute template_type.
11 12 13 |
# File 'lib/convert_theme.rb', line 11 def template_type @template_type end |
Instance Method Details
#apply_to(rails_path, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/convert_theme.rb', line 27 def apply_to(rails_path, = {}) @rails_path = rails_path @template_type = ([:template_type] || detect_template).to_s File.open(File.join(template_temp_path, 'app/views/layouts/application.html.erb'), "w") do |file| index_file = File.read(File.join(template_root, index_path)).gsub(/\r/, '') doc = Hpricot(index_file) doc.search("##{content_id}").each do |div| div.inner_html = "<%= yield %>" end contents = doc.to_html contents.gsub!(%r{(["'])/?#{image_dir}}, '\1/images') contents.gsub!(%r{(["'])/?#{stylesheet_dir}}, '\1/stylesheets') contents.gsub!(%r{(["'])/?#{javascript_dir}}, '\1/javascripts') file << contents end template_stylesheets.each do |file| File.open(File.join(template_temp_path, 'public/stylesheets', File.basename(file)), "w") do |f| contents = File.read(file) contents.gsub!(%r{url\((["']?)[./]*#{image_dir}}, 'url(\1/images') f << contents end end template_javascripts.each do |file| FileUtils.cp_r(file, File.join(template_temp_path, 'public/javascripts')) end template_images.each do |file| FileUtils.cp_r(file, File.join(template_temp_path, 'public/images')) end # now use rubigen to install the files into the rails app # so users can get conflict resolution options from command line RubiGen::Base.reset_sources RubiGen::Base.prepend_sources(RubiGen::PathSource.new(:internal, File.dirname(__FILE__))) = [:generator] || {} .merge!(:stdout => @stdout, :no_exit => true, :source => template_temp_path, :destination => rails_path) RubiGen::Scripts::Generate.new.run(["convert_theme"], ) end |
#erb? ⇒ Boolean
89 90 91 |
# File 'lib/convert_theme.rb', line 89 def erb? template_type == 'erb' end |
#haml? ⇒ Boolean
85 86 87 |
# File 'lib/convert_theme.rb', line 85 def haml? template_type == 'haml' end |
#setup_template_temp_path ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/convert_theme.rb', line 77 def setup_template_temp_path FileUtils.rm_rf(template_temp_path) FileUtils.mkdir_p(template_temp_path) %w[app/views/layouts public/images public/javascripts public/stylesheets].each do |app_path| FileUtils.mkdir_p(File.join(template_temp_path, app_path)) end end |
#template_temp_path ⇒ Object
This generator’s templates folder is temporary and is accessed via source_root within the generator.
70 71 72 73 74 75 |
# File 'lib/convert_theme.rb', line 70 def template_temp_path @template_temp_path ||= begin tmp_dir = ENV['TMPDIR'] || '/tmp' template_path = File.join(tmp_dir, "convert_theme", "templates") end end |