Module: Compressible::Readable::ClassMethods

Defined in:
lib/compressible/readable.rb

Instance Method Summary collapse

Instance Method Details

#asset_name(path) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/compressible/readable.rb', line 56

def asset_name(path)
  result = path.to_s.split(".")
  if result.last =~ /(js|css)/
    result = result[0..-2].join(".")
  else
    result = result.join(".")
  end
  result
end

#assets_for(type, *keys) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/compressible/readable.rb', line 35

def assets_for(type, *keys)
  options = keys.extract_options!
  keys.map!(&:to_s)
  if !options[:current].blank?
    environment = options[:current].to_s
  elsif defined?(::Rails)
    environment = Rails.env.to_s
  elsif defined?(::Sinatra::Application)
    environment = Sinatra::Application.environment.to_s
  else
    environment = "production"
  end
  puts "ENV #{environment}"
  cache_environments = options[:environments] || "production"
  cache_environments = [cache_environments] unless cache_environments.is_a?(Array)
  cache_environments = cache_environments.collect(&:to_s)
  
  assets = cache_environments.include?(environment) ? keys : send("uncached_#{type.to_s}_paths", *keys)
  assets
end

#javascripts_for(*keys) ⇒ Object



31
32
33
# File 'lib/compressible/readable.rb', line 31

def javascripts_for(*keys)
  assets_for(:javascript, *keys)
end

#path_for(type, file) ⇒ Object

ultimately should return global path



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/compressible/readable.rb', line 67

def path_for(type, file)
  key = "#{type.to_s}_path".to_sym

  if config && config[key]
    path = File.join(config[key], file.to_s)
    
  elsif remote?(file)
    path = file.to_s
  elsif defined?(Rails)
    path = File.join(Rails.root.to_s, "public/#{type.to_s.pluralize}", file.to_s)
  else
    path = file.to_s
  end

  path << ".#{KEYS[type].to_s}" unless path.split(".").last == KEYS[type].to_s

  path
end

#read(type, from) ⇒ Object



100
101
102
# File 'lib/compressible/readable.rb', line 100

def read(type, from)
  open(path_for(type, from)).read
end

#remote?(path) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/compressible/readable.rb', line 104

def remote?(path)
  !!(path =~ /^http(?:s)?/)
end

#size(type, *paths) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/compressible/readable.rb', line 86

def size(type, *paths)
  result = paths.collect { |path| File.size(path_for(type, path)) }.inject(0) { |sum, x| sum + x }
  by = "kb"
  unless result <= 0
    result = case by
    when "kb"
      result / 1_000
    when "mb"
      result / 1_000_000
    end
  end
  return "#{result.to_s}#{by}"
end

#stylesheets_for(*keys) ⇒ Object



27
28
29
# File 'lib/compressible/readable.rb', line 27

def stylesheets_for(*keys)
  assets_for(:stylesheet, *keys)
end

#uncached_javascript_paths(*keys) ⇒ Object



13
14
15
# File 'lib/compressible/readable.rb', line 13

def uncached_javascript_paths(*keys)
  uncached_paths_for(:js, *keys)
end

#uncached_paths_for(type, *keys) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/compressible/readable.rb', line 17

def uncached_paths_for(type, *keys)
  result = []
  config[type].each do |item|
    keys.each do |key|
      result.concat(item[:paths]) if item[:to] == key.to_s
    end
  end
  result.flatten
end

#uncached_stylesheet_paths(*keys) ⇒ Object



9
10
11
# File 'lib/compressible/readable.rb', line 9

def uncached_stylesheet_paths(*keys)
  uncached_paths_for(:css, *keys)
end