Class: Ginsu::Knife
- Inherits:
-
Object
- Object
- Ginsu::Knife
- Defined in:
- lib/ginsu/knife.rb,
lib/ginsu/config.rb
Constant Summary collapse
- @@defaults =
{ :source => 'static', :partials => [], :pages => [], :templates => [], :links => [], :folders => [] }
- @@config =
Ginsu::Config.new(@@defaults)
Class Method Summary collapse
-
.configure {|@@config| ... } ⇒ Object
Yields to given
block
to configure the Ginsu. - .folders ⇒ Object
- .links ⇒ Object
- .pages(parameters = nil) ⇒ Object
- .partials ⇒ Object
- .slice ⇒ Object
- .templates(parameters = nil) ⇒ Object
Class Method Details
.configure {|@@config| ... } ⇒ Object
Yields to given block
to configure the Ginsu.
41 42 43 44 |
# File 'lib/ginsu/config.rb', line 41 def configure(&block) raise ArgumentError, "Block must be provided to configure" unless block_given? yield @@config end |
.folders ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ginsu/knife.rb', line 123 def self.folders @@config.folders.each do |folder| source = File.join(FileUtils.pwd, @@config.source, folder[:static]) destination = File.join('public', folder[:static]) puts "Purging '#{destination}'..." FileUtils.rm_rf destination puts "Recursively copying '#{source}' to '#{destination}'..." puts "Each folder requires a :static parameter with the name of the static resource" + " to copy to the public/ directory in your Rails application." if folder[:static].blank? FileUtils.cp_r source, destination end end |
.links ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ginsu/knife.rb', line 106 def self.links @@config.links.each do |link| source = File.join(FileUtils.pwd, @@config.source, link[:static]) destination = File.join('public', link[:static]) puts "Linking '#{source}' to '#{destination}'..." puts "Each link requires a :static parameter with the name of the static resource" + " to link to the public/ directory in your Rails application." if link[:static].blank? FileUtils.ln_s source, destination, :force => true unless File.exists? destination end end |
.pages(parameters = nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 |
# File 'lib/ginsu/knife.rb', line 12 def self.pages parameters=nil files = (parameters and parameters[:partials]) ? @@config.partials : @@config.pages files.each do |file| # These should probably raise errors. puts "Error: Each file requires a :static parameter to specify the source file." if file[:static].blank? puts "Error: Each file requires a :search parameter with a search string." if file[:search].blank? puts "Error: Each file requires a :partial parameter with the name of the destination template." if file[:partial].blank? # Open the static source HTML file. static_source_string = '' static_source_path = File.join(@@config.source, file[:static]) File.open(static_source_path, "r") { |f| static_source_string = '' # Run a proc on each line? if file[:do] f.each {|line| static_source_string << file[:do].call(line)} else static_source_string = f.read end } static_source = Hpricot(static_source_string) # Use Hpricot to slice out the desired element. found = static_source.search(file[:search]).first # Make sure that the target folder exists. unless File.exists?(target_folder = File.join('app/views/', File.dirname(file[:partial]))) Dir.mkdir(target_folder) end # Drop that found string into the appropriate partial. target_filename = file[:partial] puts "Slicing '#{target_filename}' from '#{static_source_path}'..." target_filename.gsub!(/\/([^\_][^\/]+$)/) {|string| '/_' + $1 } if parameters and parameters[:partials] target_filename += '.html.erb' unless target_filename =~ /\./ target_filename = File.join('app/views/', target_filename) File.open(target_filename, 'w') do |f| f.write(found) end end end |
.partials ⇒ Object
60 61 62 |
# File 'lib/ginsu/knife.rb', line 60 def self.partials self.pages :partials => true end |
.slice ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/ginsu/knife.rb', line 4 def self.slice self.partials self.pages self.templates self.links self.folders end |
.templates(parameters = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ginsu/knife.rb', line 64 def self.templates parameters=nil files = @@config.templates files.each do |file| # These should probably raise errors. puts "Error: Each file requires a :static parameter to specify the source file." if file[:static].blank? puts "Error: Each file requires a :search parameter with a search string." if file[:search].blank? puts "Error: Each file requires a :template parameter with the name of the destination template." if file[:template].blank? # Open the static source HTML file. static_source_string = '' static_source_path = File.join(@@config.source, file[:static]) File.open(static_source_path, "r") { |f| static_source_string = '' # Run a proc on each line? if file[:do] f.each {|line| static_source_string << file[:do].call(line)} else static_source_string = f.read end } static_source = Hpricot(static_source_string) # Use Hpricot to replace the desired element. static_source.at(file[:search]).swap(file[:replace]) found = static_source.to_original_html # Drop that found string into the appropriate partial. target_filename = file[:template] puts "Slicing template '#{target_filename}' from '#{static_source_path}'..." target_filename += '.html.erb' unless target_filename =~ /\./ target_filename = File.join('app/views/layouts', target_filename) File.open(target_filename, 'w') do |f| f.write(found) end end end |