Class: Less::Command
- Inherits:
-
Object
- Object
- Less::Command
- Defined in:
- lib/less/command.rb
Instance Method Summary collapse
- #compile ⇒ Object
- #compress? ⇒ Boolean
-
#initialize(options) ⇒ Command
constructor
A new instance of Command.
-
#log(s = '') ⇒ Object
Just a logging function to avoid typing ‘}’.
- #run! ⇒ Object
- #watch? ⇒ Boolean
Constructor Details
#initialize(options) ⇒ Command
Returns a new instance of Command.
3 4 5 6 |
# File 'lib/less/command.rb', line 3 def initialize @source, @destination = [:source], [:destination] @options = end |
Instance Method Details
#compile ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/less/command.rb', line 57 def compile begin # Create a new Less object with the contents of a file css = Less::Engine.new( File.read( @source ) ).to_css css = css.delete " \n" if compress? File.open( @destination, "w" ) do |file| file.write css end puts "#{@destination.split('/').last} was updated!" if watch? rescue Errno::ENOENT => e abort "#{e}" end end |
#compress? ⇒ Boolean
8 |
# File 'lib/less/command.rb', line 8 def compress?() @options[:compress] end |
#log(s = '') ⇒ Object
Just a logging function to avoid typing ‘}’
72 73 74 |
# File 'lib/less/command.rb', line 72 def log s = '' print '} ' + s.to_s end |
#run! ⇒ Object
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 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/less/command.rb', line 10 def run! # little function which allows us to Ctrl-C exit inside the passed block watch = lambda do |&block| begin block.call rescue Interrupt puts exit 0 end end if watch? log "Watching for changes in #@source ...Ctrl-C to abort.\n" # # Main watch loop # loop do watch.call { sleep 1 } # File has changed if File.stat( @source ).mtime > File.stat( @destination ).mtime log "Change detected... " # # Error loop # loop do begin compile rescue SyntaxError error = $!..split("\n")[1..-1].collect {|e| e.gsub(/\(eval\)\:\d+\:\s/, '') } * "\n" log " errors were found in the .less file! \n#{error}\n" log "Press [enter] to continue..." watch.call do $stdin.gets print "} " end next # continue within the error loop, until the error is fixed end break # break to main loop, as no errors were encountered end end # if end # loop else compile end end |
#watch? ⇒ Boolean
7 |
# File 'lib/less/command.rb', line 7 def watch?() @options[:watch] end |