Class: JsCat
- Inherits:
-
Object
- Object
- JsCat
- Defined in:
- lib/jscat.rb
Instance Attribute Summary collapse
-
#js ⇒ Object
readonly
Returns the value of attribute js.
Instance Method Summary collapse
- #compress_js ⇒ Object
-
#initialize(params = {}) ⇒ JsCat
constructor
A new instance of JsCat.
- #list_scripts ⇒ Object
- #render_js ⇒ Object
- #set_scripts(scripts_array) ⇒ Object
- #write_js ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ JsCat
Returns a new instance of JsCat.
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/jscat.rb', line 3 def initialize(params = {}) @js_dir = params[:js_dir] || "public" @compress = params[:compress] || false @output = params[:output] || false @priority_files = params[:prioritize] || false @ignore_files = params[:ignore] || false require "yui/compressor" if @compress render_js write_js if @output end |
Instance Attribute Details
#js ⇒ Object (readonly)
Returns the value of attribute js.
2 3 4 |
# File 'lib/jscat.rb', line 2 def js @js end |
Instance Method Details
#compress_js ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/jscat.rb', line 52 def compress_js begin compressor = YUI::JavaScriptCompressor.new compressed_js = compressor.compress(@js) compress_success = true puts "compressed js" rescue Exception=>e puts 'Warning: Could not write javascript file, syntax error in javascript' end @js = compressed_js if compress_success return @js end |
#list_scripts ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/jscat.rb', line 13 def list_scripts @scripts = Dir.glob("#{@js_dir}/*.js") if @ignore_files then @ignore_files.uniq.each {|file| @scripts.delete(@js_dir+'/'+file) } end if @priority_files then @priority_files_full = [] @priority_files.uniq.each {|file| @priority_files_full << @js_dir+'/'+file } @priority_files_full.each {|file| if @scripts.include? file then @scripts.delete(file) else @priority_files_full.delete file end } @scripts = @priority_files_full + @scripts end return @scripts end |
#render_js ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jscat.rb', line 39 def render_js @js = "" list_scripts.each { |script| File.open(script, 'r') do |f1| while line = f1.gets @js = @js + line end @js = @js + "\n" end } compress_js if @compress return @js end |
#set_scripts(scripts_array) ⇒ Object
36 37 38 |
# File 'lib/jscat.rb', line 36 def set_scripts(scripts_array) @scripts = scripts_array end |
#write_js ⇒ Object
64 65 66 67 68 |
# File 'lib/jscat.rb', line 64 def write_js File.open(@output,'w') { |f| f.write(@js) } end |