Module: Compressible::Writable::ClassMethods

Defined in:
lib/compressible/writable.rb

Instance Method Summary collapse

Instance Method Details

#compress(value = nil) ⇒ Object

called if you gave it a config



10
11
12
13
14
15
16
17
18
19
# File 'lib/compressible/writable.rb', line 10

def compress(value = nil)
  configure(value) if value
  raise "set config to yaml file or run 'Compressible.js' or 'Compressible.css' manually" unless @config

  [:js, :css].each do |k|
    config[k].each do |item|
      compress_from_hash(k, item)
    end
  end
end

#compress_from_hash(k, v) ⇒ Object



21
22
23
24
# File 'lib/compressible/writable.rb', line 21

def compress_from_hash(k, v)
  args = v.dup.delete(:paths) + [v]
  self.send(k, *args)
end

#compressor_for(type, options = {}) ⇒ Object



77
78
79
80
81
82
# File 'lib/compressible/writable.rb', line 77

def compressor_for(type, options = {})
  {
    :javascript => YUI::JavaScriptCompressor,
    :stylesheet => YUI::CssCompressor
  }[type].new(options.reject {|k,v| k.to_s !~ /(munge|charset|linebreak|optimize|preserve_semicolons)/})
end

#destroy(*paths) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/compressible/writable.rb', line 147

def destroy(*paths)
  paths.each do |path, print_path|
    if path != print_path
      File.delete(path) if File.exists?(path)
    end
  end
end

#localize(to, type, *paths) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/compressible/writable.rb', line 88

def localize(to, type, *paths)
  FileUtils.mkdir_p(to) unless File.exists?(to)
  local_paths = paths.map do |path|
    if remote?(path)
      local = File.join(to, File.basename(path))
      File.open(local, "w+") do |file|
        begin
          file.puts read(type, path)
        rescue Exception => e
          paths.delete(path)
          puts "#{e.message}: #{path}"
        end
      end
      [local, path]
    else
      [path, path]
    end
  end
end

#process(type, *paths, &block) ⇒ Object



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
71
72
73
74
75
# File 'lib/compressible/writable.rb', line 45

def process(type, *paths, &block)
  require 'yui/compressor' unless defined?(::YUI)
  options = paths.extract_options!
  to      = options[:to]
  
  raise 'must define result file name via :to => name' unless to
  
  compressor = compressor_for(type, options)
  
  paths = localize(to, type, *paths)
  
  start_size = size(type, *paths.map(&:first))
  
  compressed = paths.collect do |path, print_path|
    puts "Compressing '#{print_path}'... (#{size(type, path)})"
    result = compressor.compress(read(type, path))
    next if result.blank?
    result = yield(path, result).to_s if block_given?
    result
  end.join("")
  
  write(type, to, compressed)
  
  destroy(*paths)
  
  end_size = size(type, to)
  
  puts "Compressed to '#{to.to_s}' (#{end_size} from #{start_size})"
  
  compressed
end

#remote_path(domain, path, asset) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/compressible/writable.rb', line 108

def remote_path(domain, path, asset)
  # full
  if asset =~ /^http(?:s)?:\/\//
    asset
  # absolute
  elsif asset =~ /^\//
    asset = "#{domain}#{asset}"
  # relative
  else
    asset = "#{domain}#{path}/#{asset}"
  end
end

#scrape(page) ⇒ Object

returns css and javascripts => [], :css => [] requires nokogiri



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/compressible/writable.rb', line 123

def scrape(page)
  require 'nokogiri'
  url = URI.parse(page)
  domain =   "#{url.scheme}://#{url.host}"
  domain <<  ":#{url.port.to_s}"
  path = url.path.squeeze("/")
  html = Nokogiri::HTML(open(page).read)
  scripts = []
  
  html.css("script").each do |script|
    next if script["src"].blank?
    scripts << remote_path(domain, path, script["src"])
  end
  
  csses = []
  
  html.css("link[rel=stylesheet]").each do |css|
    next if css["href"].blank?
    csses << remote_path(domain, path, css["href"])
  end
  
  {:js => scripts, :css => csses}
end

#write(type, to, result) ⇒ Object



84
85
86
# File 'lib/compressible/writable.rb', line 84

def write(type, to, result)
  File.open(path_for(type, to), "w") {|f| f.puts result}
end

#write_javascript(*args, &block) ⇒ Object

figure out how to do alias_method_chain or something otherwise the modules are tightly coupled



28
29
30
31
32
33
34
35
# File 'lib/compressible/writable.rb', line 28

def write_javascript(*args, &block)
  paths = args.dup.flatten
  options = paths.extract_options!
  options[:to] = asset_name(options[:to])
  options[:munge] = options.has_key?(:munge) ? options[:munge] : true
  paths << options
  process(:javascript, *paths, &block)
end

#write_stylesheet(*args, &block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/compressible/writable.rb', line 37

def write_stylesheet(*args, &block)
  paths = args.dup.flatten
  options = paths.extract_options!
  options[:to] = asset_name(options[:to])
  paths << options
  process(:stylesheet, *paths, &block)
end