Class: Ginsu::Knife

Inherits:
Object
  • Object
show all
Defined in:
lib/ginsu/knife.rb,
lib/ginsu/config.rb

Constant Summary collapse

@@defaults =
{ 
  :source => 'static',
  :slices => [],
  :links => []
}
@@config =
Ginsu::Config.new(@@defaults)

Class Method Summary collapse

Class Method Details

.configure {|@@config| ... } ⇒ Object

Yields to given block to configure the Ginsu.

Yields:

  • (@@config)

Raises:

  • (ArgumentError)


35
36
37
38
# File 'lib/ginsu/config.rb', line 35

def configure(&block)
  raise ArgumentError, "Block must be provided to configure" unless block_given?
  yield @@config
end


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ginsu/knife.rb', line 45

def self.link
  @@config.links.each do |link|
    
    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?
    
    source = File.join(FileUtils.pwd, @@config.source, link[:static])
    destination = File.join('public', link[:static])
    
    puts "Linking '#{source}' to '#{destination}'."
    FileUtils.ln_s source, destination, :force => true
    
  end
end

.sliceObject



6
7
8
9
10
11
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
# File 'lib/ginsu/knife.rb', line 6

def self.slice
  @@config.slices.each do |slice|
    
    puts "Each slice requires a :static parameter to specify the source file." if
      slice[:static].blank?
    puts "Each slice requires either a :search, :css or :xpath parameter with a search string." if
      slice[:css].blank? and slice[:xpath].blank? and slice[:search].blank?
    puts "Each slice requires a :partial parameter with the name of the destination template." if
      slice[:partial].blank?

    # Open the static source HTML file.
    static_source_string = ''
    static_source_path = File.join(@@config.source, slice[:static])
    File.open(static_source_path, "r") { |f|
        static_source_string = f.read
    }
    static_source = Nokogiri::HTML(static_source_string)

    # Use Nokogiri to slice out the desired element's content.
    found = ''
    if slice[:css]
      found = static_source.css(slice[:css]).first
    elsif slice[:xpath]
      found = static_source.css(slice[:xpath]).first
    else
      found = static_source.search(slice[:search]).first
    end
    
    # Drop that found string into the appropriate partial.
    partial_filename = slice[:partial]
    partial_filename = '_' + partial_filename unless partial_filename =~ /^[^\_]/
    partial_filename += '.html.erb' unless partial_filename =~ /\./
    partial_filename = File.join('app/views/', partial_filename)
    puts "Sliced partial '#{partial_filename}' from static '#{static_source_path}'."
    File.open(partial_filename, 'w') {|f| f.write(found) }
    
  end
end