Class: Zfben_libjs::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/zfben_libjs/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, sources, libs, options) ⇒ Collection

Returns a new instance of Collection.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/zfben_libjs/collection.rb', line 4

def initialize name, sources, libs, options
  sources = [sources] unless sources.class.to_s != 'Array'
  @name = name
  
  @sources = sources.flatten.uniq.compact
  
  @options = options
  
  @libs = []
  @css = []
  @js = []
  @images = []
  
  process_sources @sources, libs
end

Instance Attribute Details

#cssObject

Returns the value of attribute css.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def css
  @css
end

#css_pathObject

Returns the value of attribute css_path.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def css_path
  @css_path
end

#imagesObject

Returns the value of attribute images.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def images
  @images
end

#images_pathObject

Returns the value of attribute images_path.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def images_path
  @images_path
end

#jsObject

Returns the value of attribute js.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def js
  @js
end

#js_pathObject

Returns the value of attribute js_path.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def js_path
  @js_path
end

#libsObject

Returns the value of attribute libs.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def libs
  @libs
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def name
  @name
end

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def options
  @options
end

#sourcesObject

Returns the value of attribute sources.



2
3
4
# File 'lib/zfben_libjs/collection.rb', line 2

def sources
  @sources
end

Instance Method Details

#write_files!Object



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
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/zfben_libjs/collection.rb', line 20

def write_files!
  if @images.length > 0
    @images_path = @images.map{ |path|
      new_path = File.join(@options['src/images'], File.basename(path))
      FileUtils.cp path, new_path
      new_path
    }.uniq
  end
  
  merge_css = ''
  @css.each do |css|
    merge_css << css.to_css
  end
  if merge_css != ''
    @css_path = File.join(@options['src/stylesheets'], @name + '.css')
    css = Zfben_libjs::Css.new(:source => merge_css, :options => @options)
    if @options['changeImageUrl']
      css.change_images_url!
    end
    if @options['minify']
      #File.open(@css_path + '.debug', 'w'){ |f| f.write(merge_css) }
      file_content = css.minify
    else
      file_content = css.compile
    end
    File.open(@css_path, 'w'){ |f| f.write(file_content) }
  else
    @css_path = nil
  end
  
  merge_js = ''
  @js.each do |js|
    merge_js << js.to_js + ";\n"
  end
  if merge_js != ''
    @js_path = File.join(@options['src/javascripts'], @name + '.js')
    if @options['minify']
      #File.open(@js_path + '.debug', 'w'){ |f| f.write(merge_js) }
      merge_js = Zfben_libjs::Js.new(:source => merge_js).minify
    end
    File.open(@js_path, 'w'){ |f| f.write(merge_js) }
  else
    @js_path = nil
  end

  if @libs.length > 0
    @libs = @libs.flatten.uniq
  end
  
  return @libs + [@css_path, @js_path].compact
end