asset_hat 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/HISTORY CHANGED
@@ -1,6 +1,14 @@
1
1
  HISTORY
2
2
  =======
3
3
 
4
+ Version 0.1.3 (2010-03-03)
5
+ --------------------------
6
+ * Allowed adding commit IDs and asset hosts to `url(/htc/...)` URLs (e.g.,
7
+ `/htc/iepngfix.htc`) in CSS, not just images.
8
+ * Changed `AssetHat.config` to memoize only if `cache?` is true. In
9
+ development environments, this means the config file will be reread with
10
+ every request.
11
+
4
12
  Version 0.1.2 (2010-01-27)
5
13
  --------------------------
6
14
  * Memoized HTML output from `include_css` and `include_js` when
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :minor: 1
3
- :patch: 2
2
+ :patch: 3
4
3
  :major: 0
4
+ :minor: 1
5
5
  :build:
@@ -88,6 +88,7 @@ module AssetHatHelper
88
88
  stylesheet_link_tag(src, options)
89
89
  when :js
90
90
  javascript_include_tag(src, options)
91
+ else nil
91
92
  end
92
93
  end.join("\n")
93
94
  end
data/asset_hat.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{asset_hat}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ron DeVera", "Mint Digital"]
12
- s.date = %q{2010-01-27}
12
+ s.date = %q{2010-03-03}
13
13
  s.description = %q{Minify, bundle, and optimize CSS/JS assets.}
14
14
  s.email = %q{ronald.devera@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/asset_hat.rb CHANGED
@@ -11,17 +11,21 @@ module AssetHat
11
11
  CONFIG_FILEPATH = File.join(RAILS_ROOT, 'config', 'assets.yml')
12
12
 
13
13
  class << self
14
- attr_accessor :html_cache
14
+ attr_accessor :config, :asset_exists, :html_cache
15
15
  end
16
16
 
17
17
  def self.config
18
- @@config ||= YAML.load(File.open(CONFIG_FILEPATH, 'r'))
18
+ if !cache? || @config.blank?
19
+ @config = YAML.load(File.open(CONFIG_FILEPATH, 'r'))
20
+ end
21
+ @config
19
22
  end
20
23
 
21
24
  def self.assets_dir(type)
22
25
  case type.to_sym
23
26
  when :css ; STYLESHEETS_DIR
24
27
  when :js ; JAVASCRIPTS_DIR
28
+ else nil
25
29
  end
26
30
  end
27
31
 
@@ -33,14 +37,14 @@ module AssetHat
33
37
  return
34
38
  end
35
39
 
36
- @@asset_exists ||= TYPES.inject({}) do |hsh, known_type|
40
+ @asset_exists ||= TYPES.inject({}) do |hsh, known_type|
37
41
  hsh.merge!(known_type => {})
38
42
  end
39
- if @@asset_exists[type][filename].nil?
40
- @@asset_exists[type][filename] =
43
+ if @asset_exists[type][filename].nil?
44
+ @asset_exists[type][filename] =
41
45
  File.exist?(File.join(self.assets_dir(type), filename))
42
46
  end
43
- @@asset_exists[type][filename]
47
+ @asset_exists[type][filename]
44
48
  end
45
49
 
46
50
  def self.cache? ; ActionController::Base.perform_caching ; end
data/lib/asset_hat/css.rb CHANGED
@@ -13,19 +13,19 @@ module AssetHat
13
13
  def self.minify(input_string, options={})
14
14
  options.reverse_merge!(:engine => :cssmin)
15
15
 
16
- options[:engine] = options[:engine].to_sym
17
- unless ENGINES.include?(options[:engine])
16
+ engine = options[:engine].to_sym
17
+ unless ENGINES.include?(engine)
18
18
  raise %Q{
19
- Unknown CSS minification engine '#{options[:engine]}'.
19
+ Unknown CSS minification engine '#{engine}'.
20
20
  Allowed: #{ENGINES.map{ |e| "'#{e}'" }.join(', ')}
21
21
  }.strip.gsub(/\s+/, ' ') and return
22
22
  end
23
23
 
24
- AssetHat::CSS::Engines.send(options[:engine], input_string)
24
+ AssetHat::CSS::Engines.send(engine, input_string)
25
25
  end
26
26
 
27
27
  # def self.add_asset_mtimes(css)
28
- # css.gsub(/url[\s]*\((\/images\/[^)]+)\)/) do |match|
28
+ # css.gsub(/url[\s]*\((\/(images|htc)\/[^)]+)\)/) do |match|
29
29
  # src = $1
30
30
  # mtime = File.mtime(File.join(Rails.public_path, src))
31
31
  # "url(#{src}?#{mtime.to_i})"
@@ -33,7 +33,7 @@ module AssetHat
33
33
  # end
34
34
 
35
35
  def self.add_asset_commit_ids(css)
36
- css.gsub(/url[\s]*\((\/images\/[^)]+)\)/) do |match|
36
+ css.gsub(/url[\s]*\((\/(images|htc)\/[^)]+)\)/) do |match|
37
37
  src = $1
38
38
  filepath = File.join(Rails.public_path, src)
39
39
  commit_id = AssetHat.last_commit_id(filepath)
@@ -43,7 +43,7 @@ module AssetHat
43
43
 
44
44
  def self.add_asset_hosts(css, asset_host)
45
45
  return if asset_host.blank?
46
- css.gsub(/url[\s]*\((\/images\/[^)]+)\)/) do |match|
46
+ css.gsub(/url[\s]*\((\/(images|htc)\/[^)]+)\)/) do |match|
47
47
  src = $1
48
48
  "url(#{(asset_host =~ /%d/) ? asset_host % (src.hash % 4) : asset_host}#{src})"
49
49
  end
data/lib/asset_hat/js.rb CHANGED
@@ -15,15 +15,15 @@ module AssetHat
15
15
  def self.minify(input_string, options={})
16
16
  options.reverse_merge!(:engine => :jsmin)
17
17
 
18
- options[:engine] = options[:engine].to_sym
19
- unless ENGINES.include?(options[:engine])
18
+ engine = options[:engine].to_sym
19
+ unless ENGINES.include?(engine)
20
20
  raise %Q{
21
- Unknown JS minification engine '#{options[:engine]}'.
21
+ Unknown JS minification engine '#{engine}'.
22
22
  Allowed: #{ENGINES.map{ |e| "'#{e}'" }.join(', ')}
23
23
  }.strip.gsub(/\s+/, ' ') and return
24
24
  end
25
25
 
26
- AssetHat::JS::Engines.send(options[:engine], input_string)
26
+ AssetHat::JS::Engines.send(engine, input_string)
27
27
  end
28
28
 
29
29
  module Engines
@@ -76,6 +76,7 @@ module AssetHat
76
76
  src = ActionController::Base.consider_all_requests_local ?
77
77
  "jquery-#{version}.min.js" :
78
78
  "http://ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js"
79
+ else nil
79
80
  end
80
81
  src
81
82
  end
data/lib/asset_hat/vcs.rb CHANGED
@@ -1,4 +1,8 @@
1
1
  module AssetHat
2
+ class << self
3
+ attr_accessor :last_commit_ids, :last_bundle_commit_ids
4
+ end
5
+
2
6
  def self.last_commit_id(*args)
3
7
  # Usage:
4
8
  #
@@ -20,13 +24,13 @@ module AssetHat
20
24
  raise 'Git is currently the only supported VCS.' and return
21
25
  end
22
26
 
23
- @@last_commit_ids ||= {}
24
- if @@last_commit_ids[filepaths].blank?
27
+ @last_commit_ids ||= {}
28
+ if @last_commit_ids[filepaths].blank?
25
29
  h = `git log -1 --pretty=format:%h #{filepaths} 2>/dev/null`
26
30
  # `h` has either the abbreviated Git commit hash or an empty string
27
- @@last_commit_ids[filepaths] = h if h.present?
31
+ @last_commit_ids[filepaths] = h if h.present?
28
32
  end
29
- @@last_commit_ids[filepaths]
33
+ @last_commit_ids[filepaths]
30
34
  end
31
35
 
32
36
  def self.last_bundle_commit_id(bundle, type)
@@ -46,20 +50,20 @@ module AssetHat
46
50
  end
47
51
 
48
52
  # Default to `{:css => {}, :js => {}}`
49
- @@last_bundle_commit_ids ||=
53
+ @last_bundle_commit_ids ||=
50
54
  TYPES.inject({}) { |hsh, t| hsh.merge(t => {}) }
51
55
 
52
- if @@last_bundle_commit_ids[type][bundle].blank?
56
+ if @last_bundle_commit_ids[type][bundle].blank?
53
57
  dir = self.assets_dir(type)
54
58
  filepaths = self.bundle_filepaths(bundle, type)
55
59
  if filepaths.present?
56
- @@last_bundle_commit_ids[type][bundle] = self.last_commit_id(*filepaths)
60
+ @last_bundle_commit_ids[type][bundle] = self.last_commit_id(*filepaths)
57
61
  end
58
62
  end
59
63
 
60
- @@last_bundle_commit_ids[type][bundle]
64
+ @last_bundle_commit_ids[type][bundle]
61
65
  end
62
66
 
63
- def self.last_commit_ids ; @@last_commit_ids ; end
67
+ def self.last_commit_ids ; @last_commit_ids ; end
64
68
 
65
69
  end
@@ -1,13 +1,39 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class AssetHatTest < ActiveSupport::TestCase
4
+ context 'AssetHat' do
5
+ context 'with caching enabled' do
6
+ setup do
7
+ flexmock(AssetHat).should_receive(:cache? => true)
8
+ end
9
+
10
+ should 'memoize config' do
11
+ AssetHat.config
12
+ flexmock(YAML).should_receive(:load).never
13
+ 3.times { AssetHat.config }
14
+ end
15
+ end # context 'with caching enabled'
16
+
17
+ context 'with caching disabled' do
18
+ setup do
19
+ flexmock(AssetHat).should_receive(:cache? => false)
20
+ end
21
+
22
+ should 'not memoize config' do
23
+ AssetHat.config
24
+ flexmock(YAML).should_receive(:load).times(3)
25
+ 3.times { AssetHat.config }
26
+ end
27
+ end # context 'with caching disabled'
28
+ end # context 'AssetHat'
29
+
4
30
  context 'AssetHat::CSS' do
5
31
  should 'return path to minified file' do
6
32
  assert_equal 'foo/bar/baz.min.css',
7
33
  AssetHat::CSS.min_filepath('foo/bar/baz.css')
8
34
  end
9
35
 
10
- should 'add asset commit IDs' do
36
+ should 'add image asset commit IDs' do
11
37
  commit_id = 111
12
38
  flexmock(AssetHat).should_receive(:last_commit_id => commit_id)
13
39
  flexmock(Rails).should_receive(:public_path => '')
@@ -17,7 +43,17 @@ class AssetHatTest < ActiveSupport::TestCase
17
43
  'p{background:url(/images/foo.png)}')
18
44
  end
19
45
 
20
- should 'add asset hosts' do
46
+ should 'add .htc asset commit IDs' do
47
+ commit_id = 111
48
+ flexmock(AssetHat).should_receive(:last_commit_id => commit_id)
49
+ flexmock(Rails).should_receive(:public_path => '')
50
+
51
+ assert_equal "p{background:url(/htc/iepngfix.htc?#{commit_id})}",
52
+ AssetHat::CSS.add_asset_commit_ids(
53
+ 'p{background:url(/htc/iepngfix.htc)}')
54
+ end
55
+
56
+ should 'add image asset hosts' do
21
57
  asset_host = 'http://media%d.example.com'
22
58
  assert_match(
23
59
  /^p\{background:url\(http:\/\/media[\d]\.example\.com\/images\/foo.png\)\}$/,
@@ -25,6 +61,15 @@ class AssetHatTest < ActiveSupport::TestCase
25
61
  'p{background:url(/images/foo.png)}', asset_host)
26
62
  )
27
63
  end
64
+
65
+ should 'add .htc asset hosts' do
66
+ asset_host = 'http://media%d.example.com'
67
+ assert_match(
68
+ /^p\{background:url\(http:\/\/media[\d]\.example\.com\/htc\/iepngfix.htc\)\}$/,
69
+ AssetHat::CSS.add_asset_hosts(
70
+ 'p{background:url(/htc/iepngfix.htc)}', asset_host)
71
+ )
72
+ end
28
73
  end # context 'AssetHat::CSS'
29
74
 
30
75
  context 'AssetHat::JS' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asset_hat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ron DeVera
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-01-27 00:00:00 -05:00
13
+ date: 2010-03-03 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency