Class: FilesToJs
- Inherits:
-
Object
- Object
- FilesToJs
- Defined in:
- lib/FilesToJS.rb
Instance Attribute Summary collapse
-
#js ⇒ Object
readonly
Returns the value of attribute js.
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ FilesToJs
constructor
A new instance of FilesToJs.
- #list_files ⇒ Object
- #render_js ⇒ Object
- #write_js ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ FilesToJs
Returns a new instance of FilesToJs.
3 4 5 6 7 8 |
# File 'lib/FilesToJS.rb', line 3 def initialize(params = {}) @file_dir = params[:file_dir] || "templates" @file_format = params[:file_format] || false @js_object_name = params[:js_object_name] || "files" @output = params[:output] || false end |
Instance Attribute Details
#js ⇒ Object (readonly)
Returns the value of attribute js.
2 3 4 |
# File 'lib/FilesToJS.rb', line 2 def js @js end |
Instance Method Details
#list_files ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/FilesToJS.rb', line 9 def list_files if @file_format then @files = Dir.glob("#{@file_dir}/*.#{@file_format}") else @files = Dir.glob("#{@file_dir}/*.*") end return @files end |
#render_js ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/FilesToJS.rb', line 17 def render_js @js = "#{@js_object_name} = {};" list_files.each { |file_path| file_filename = file_path.split('/').last.split('.').first file_string = "" File.open("#{file_path}",'r') do |f1| while line = f1.gets file_string = file_string + line.gsub('\'',''').strip end end @js = @js + "#{@js_object_name}['#{file_filename}']='#{file_string}';" } return @js end |
#write_js ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/FilesToJS.rb', line 31 def write_js render_js unless @js if @output then File.open(@output,'w') { |f| f.write(@js) } end end |