dassets 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,5 +2,5 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rake'
6
- gem 'pry', "~> 0.9.0"
5
+ gem 'rake', "~> 10.4.0"
6
+ gem 'pry', "~> 0.9.0"
File without changes
data/README.md CHANGED
@@ -19,7 +19,8 @@ Dassets.configure do |c|
19
19
 
20
20
  # (optional) tell Dassets where to store digested asset files
21
21
  # if none given, Dassets will not write any digested output
22
- # use this to "cache" digested assets to the public dir (for example)
22
+ # use this to "cache" digested assets to the public dir so that
23
+ # your web server can serve them directly
23
24
  c.file_store '/path/to/public' # default: `FileStore::NullStore.new`
24
25
 
25
26
  end
@@ -12,21 +12,21 @@ class Dassets::AssetFile
12
12
  @dirname = File.dirname(@digest_path)
13
13
  @extname = File.extname(@digest_path)
14
14
  @basename = File.basename(@digest_path, @extname)
15
- @source_proxy = Dassets::SourceProxy.new(@digest_path, Dassets.config.cache)
15
+ @source_proxy = Dassets::SourceProxy.new(@digest_path, {
16
+ :content_cache => Dassets.config.content_cache,
17
+ :fingerprint_cache => Dassets.config.fingerprint_cache
18
+ })
16
19
  end
17
20
 
18
21
  def digest!
19
22
  return if !self.exists?
20
- Dassets.config.file_store.save(self.path){ self.content }
21
- end
22
-
23
- def path
24
- path_basename = "#{@basename}-#{self.fingerprint}#{@extname}"
25
- File.join(@dirname, path_basename).sub(/^\.\//, '').sub(/^\//, '')
23
+ Dassets.config.file_store.save(self.url){ self.content }
26
24
  end
27
25
 
28
26
  def url
29
- "#{dassets_base_url}/#{self.path}"
27
+ path_basename = "#{@basename}-#{self.fingerprint}#{@extname}"
28
+ path = File.join(@dirname, path_basename).sub(/^\.\//, '').sub(/^\//, '')
29
+ "#{dassets_base_url}/#{path}"
30
30
  end
31
31
 
32
32
  alias_method :href, :url
@@ -4,7 +4,7 @@ module Dassets::Cache
4
4
  class MemCache
5
5
  require 'thread'
6
6
 
7
- # this is a thread-safe in-memory cache for use with the `SourceCache`
7
+ # this is a thread-safe in-memory cache
8
8
 
9
9
  def initialize
10
10
  @hash = {}
@@ -10,14 +10,15 @@ module Dassets
10
10
  include NsOptions::Proxy
11
11
 
12
12
  option :file_store, FileStore, :default => proc{ FileStore::NullStore.new }
13
- option :cache, :default => proc{ Cache::NoCache.new }
14
13
 
15
14
  attr_reader :sources, :combinations
16
15
 
17
16
  def initialize
18
17
  super
19
- @sources = []
20
- @combinations = Hash.new{ |h, k| [k] } # digest pass-thru if none defined
18
+ @sources = []
19
+ @combinations = Hash.new{ |h, k| [k] } # digest pass-thru if none defined
20
+ @content_cache = Dassets::Cache::NoCache.new
21
+ @fingerprint_cache = Dassets::Cache::NoCache.new
21
22
  end
22
23
 
23
24
  def base_url(value = nil)
@@ -29,6 +30,16 @@ module Dassets
29
30
  @base_url = value
30
31
  end
31
32
 
33
+ def content_cache(cache = nil)
34
+ @content_cache = cache if !cache.nil?
35
+ @content_cache
36
+ end
37
+
38
+ def fingerprint_cache(cache = nil)
39
+ @fingerprint_cache = cache if !cache.nil?
40
+ @fingerprint_cache
41
+ end
42
+
32
43
  def source(path, &block)
33
44
  @sources << Source.new(path).tap{ |s| block.call(s) if block }
34
45
  end
@@ -1,4 +1,4 @@
1
- require 'rack/request'
1
+ require 'rack'
2
2
 
3
3
  module Dassets; end
4
4
  class Dassets::Server
@@ -7,13 +7,13 @@ module Dassets
7
7
 
8
8
  class SourceFile
9
9
 
10
- def self.find_by_digest_path(path, cache = nil)
10
+ def self.find_by_digest_path(path, options = nil)
11
11
  # look in the configured source list
12
12
  source_files = Dassets.source_list.map{ |p| self.new(p) }
13
13
 
14
14
  # get the last matching one (in case two source files have the same digest
15
15
  # path the last one *should* be correct since it was last to be configured)
16
- source_files.select{ |s| s.digest_path == path }.last || NullSourceFile.new(path, cache)
16
+ source_files.select{ |s| s.digest_path == path }.last || NullSourceFile.new(path, options)
17
17
  end
18
18
 
19
19
  attr_reader :file_path
@@ -86,11 +86,11 @@ module Dassets
86
86
 
87
87
  class NullSourceFile < SourceFile
88
88
 
89
- def initialize(digest_path, cache = nil)
89
+ def initialize(digest_path, options = nil)
90
90
  @file_path, @ext_list = '', []
91
91
  @digest_path = digest_path
92
92
  @source_proxy = if Dassets.config.combination?(@digest_path)
93
- SourceProxy.new(@digest_path, cache)
93
+ SourceProxy.new(@digest_path, options)
94
94
  else
95
95
  NullSourceProxy.new
96
96
  end
@@ -5,12 +5,18 @@ require 'dassets/source_file'
5
5
  module Dassets; end
6
6
  class Dassets::SourceProxy
7
7
 
8
- attr_reader :digest_path, :cache, :source_files
8
+ attr_reader :digest_path, :content_cache, :fingerprint_cache
9
+ attr_reader :source_files
9
10
 
10
- def initialize(digest_path, cache = nil)
11
- @digest_path = digest_path
12
- @cache = cache || Dassets::Cache::NoCache.new
13
- @source_files = get_source_files(@digest_path, @cache)
11
+ def initialize(digest_path, options = nil)
12
+ options ||= {}
13
+ @digest_path = digest_path
14
+ @content_cache = options[:content_cache] || Dassets::Cache::NoCache.new
15
+ @fingerprint_cache = options[:fingerprint_cache] || Dassets::Cache::NoCache.new
16
+ @source_files = get_source_files(@digest_path, {
17
+ :content_cache => @content_cache,
18
+ :fingerprint_cache => @fingerprint_cache
19
+ })
14
20
  end
15
21
 
16
22
  def key
@@ -18,11 +24,11 @@ class Dassets::SourceProxy
18
24
  end
19
25
 
20
26
  def content
21
- @cache["#{self.key} -- content"] ||= source_content
27
+ @content_cache[self.key] ||= source_content
22
28
  end
23
29
 
24
30
  def fingerprint
25
- @cache["#{self.key} -- fingerprint"] ||= source_fingerprint
31
+ @fingerprint_cache[self.key] ||= source_fingerprint
26
32
  end
27
33
 
28
34
  def mtime
@@ -43,9 +49,9 @@ class Dassets::SourceProxy
43
49
  Digest::MD5.new.hexdigest(source_content)
44
50
  end
45
51
 
46
- def get_source_files(digest_path, cache)
52
+ def get_source_files(digest_path, options)
47
53
  Dassets.config.combinations[digest_path.to_s].map do |source_digest_path|
48
- Dassets::SourceFile.find_by_digest_path(source_digest_path, cache)
54
+ Dassets::SourceFile.find_by_digest_path(source_digest_path, options)
49
55
  end
50
56
  end
51
57
 
@@ -1,3 +1,3 @@
1
1
  module Dassets
2
- VERSION = "0.12.0"
2
+ VERSION = "0.13.0"
3
3
  end
@@ -3,4 +3,8 @@ require 'assert/factory'
3
3
  module Factory
4
4
  extend Assert::Factory
5
5
 
6
+ def self.base_url
7
+ Factory.boolean ? Factory.url : nil
8
+ end
9
+
6
10
  end
@@ -81,19 +81,21 @@ module Dassets
81
81
 
82
82
  class DigestTests < SuccessTests
83
83
  setup do
84
+ base_url = Factory.base_url
85
+ Assert.stub(Dassets.config, :base_url){ base_url }
84
86
  Dassets.config.file_store = TEST_SUPPORT_PATH.join('public').to_s
85
- @url = 'file1-daa05c683a4913b268653f7a7e36a5b4.txt'
87
+ @url = Dassets['file1.txt'].url
86
88
  @url_file = Dassets.config.file_store.store_path(@url)
87
- FileUtils.rm(@url_file)
88
89
  end
89
90
  teardown do
91
+ FileUtils.rm(@url_file)
90
92
  Dassets.config.file_store = FileStore::NullStore.new
91
93
  end
92
94
 
93
95
  should "digest the asset" do
94
96
  assert_not_file_exists @url_file
95
97
 
96
- resp = get "/#{@url}"
98
+ resp = get @url
97
99
  assert_equal 200, resp.status
98
100
  assert_file_exists @url_file
99
101
  end
@@ -15,7 +15,7 @@ class Dassets::AssetFile
15
15
  subject{ @asset_file }
16
16
 
17
17
  should have_readers :digest_path, :dirname, :extname, :basename, :source_proxy
18
- should have_imeths :digest!, :path, :url, :href, :fingerprint, :content
18
+ should have_imeths :digest!, :url, :href, :fingerprint, :content
19
19
  should have_imeths :mtime, :size, :mime_type, :exists?, :==
20
20
 
21
21
  should "know its digest path, dirname, extname, and basename" do
@@ -39,9 +39,12 @@ class Dassets::AssetFile
39
39
  end
40
40
 
41
41
  should "know its source proxy" do
42
- assert_not_nil subject.source_proxy
43
- assert_kind_of Dassets::SourceProxy, subject.source_proxy
44
- assert_equal subject.digest_path, subject.source_proxy.digest_path
42
+ source_proxy = subject.source_proxy
43
+ assert_not_nil source_proxy
44
+ assert_kind_of Dassets::SourceProxy, source_proxy
45
+ assert_equal subject.digest_path, source_proxy.digest_path
46
+ assert_equal Dassets.config.content_cache, source_proxy.content_cache
47
+ assert_equal Dassets.config.fingerprint_cache, source_proxy.fingerprint_cache
45
48
  end
46
49
 
47
50
  should "have a fingerprint" do
@@ -68,14 +71,7 @@ class Dassets::AssetFile
68
71
  assert_equal exp_content, without_output.content
69
72
  end
70
73
 
71
- should "build it's path from the file and the fingerprint" do
72
- assert_match /^file1-[a-f0-9]{32}\.txt$/, subject.path
73
-
74
- nested = Dassets::AssetFile.new('nested/file1.txt')
75
- assert_equal "nested/file1-.txt", nested.path
76
- end
77
-
78
- should "build it's url/href from the path and any configured base url" do
74
+ should "build it's url/href from the file, fingerpint and any configured base url" do
79
75
  assert_match /^\/file1-[a-f0-9]{32}\.txt$/, subject.url
80
76
  assert_match subject.url, subject.href
81
77
 
@@ -84,7 +80,7 @@ class Dassets::AssetFile
84
80
  assert_equal nested.url, nested.href
85
81
 
86
82
  base_url = Factory.url
87
- Dassets.config.base_url base_url
83
+ Assert.stub(Dassets.config, :base_url){ base_url }
88
84
 
89
85
  assert_match /^#{base_url}\/file1-[a-f0-9]{32}\.txt$/, subject.url
90
86
  assert_match subject.url, subject.href
@@ -94,10 +90,6 @@ class Dassets::AssetFile
94
90
  end
95
91
 
96
92
  should "not memoize its attributes" do
97
- path1 = subject.path
98
- path2 = subject.path
99
- assert_not_same path2, path1
100
-
101
93
  url1 = subject.url
102
94
  url2 = subject.url
103
95
  assert_not_same url2, url1
@@ -120,11 +112,15 @@ class Dassets::AssetFile
120
112
  class DigestTests < UnitTests
121
113
  desc "being digested with an output path configured"
122
114
  setup do
115
+ base_url = Factory.base_url
116
+ Assert.stub(Dassets.config, :base_url){ base_url }
123
117
  Dassets.config.file_store = TEST_SUPPORT_PATH.join('public').to_s
118
+
124
119
  @save_path = @asset_file.digest!
125
- @outfile = Dassets.config.file_store.store_path(@asset_file.path)
120
+ @outfile = Dassets.config.file_store.store_path(@asset_file.url)
126
121
  end
127
122
  teardown do
123
+ FileUtils.rm(@outfile)
128
124
  Dassets.config.file_store = Dassets::FileStore::NullStore.new
129
125
  end
130
126
 
@@ -16,9 +16,10 @@ class Dassets::Config
16
16
  end
17
17
  subject{ @config }
18
18
 
19
- should have_options :file_store, :cache
19
+ should have_options :file_store
20
20
  should have_readers :combinations
21
21
  should have_imeths :base_url, :set_base_url
22
+ should have_imeths :content_cache, :fingerprint_cache
22
23
  should have_imeths :source, :combination, :combination?
23
24
 
24
25
  should "have no base url by default" do
@@ -47,8 +48,30 @@ class Dassets::Config
47
48
  assert_kind_of Dassets::FileStore::NullStore, subject.file_store
48
49
  end
49
50
 
50
- should "default the cache option to no caching" do
51
- assert_kind_of Dassets::Cache::NoCache, subject.cache
51
+ should "default its content cache" do
52
+ assert_instance_of Dassets::Cache::NoCache, subject.content_cache
53
+ end
54
+
55
+ should "allow configuring non-nil content caches" do
56
+ cache = Dassets::Cache::MemCache.new
57
+ subject.content_cache(cache)
58
+ assert_equal cache, subject.content_cache
59
+
60
+ subject.content_cache(nil)
61
+ assert_equal cache, subject.content_cache
62
+ end
63
+
64
+ should "default its fingerprint cache" do
65
+ assert_instance_of Dassets::Cache::NoCache, subject.fingerprint_cache
66
+ end
67
+
68
+ should "allow configuring non-nil fingerprint caches" do
69
+ cache = Dassets::Cache::MemCache.new
70
+ subject.fingerprint_cache(cache)
71
+ assert_equal cache, subject.fingerprint_cache
72
+
73
+ subject.fingerprint_cache(nil)
74
+ assert_equal cache, subject.fingerprint_cache
52
75
  end
53
76
 
54
77
  should "register new sources with the `source` method" do
@@ -2,6 +2,7 @@ require 'assert'
2
2
  require 'dassets/source_file'
3
3
 
4
4
  require 'dassets/asset_file'
5
+ require 'dassets/cache'
5
6
  require 'dassets/source_proxy'
6
7
 
7
8
  class Dassets::SourceFile
@@ -61,6 +62,12 @@ class Dassets::SourceFile
61
62
  end
62
63
 
63
64
  class NullSourceTests < UnitTests
65
+ setup do
66
+ Dassets.config.combination 'file3.txt', ['file1.txt', 'file2.txt']
67
+ end
68
+ teardown do
69
+ Dassets.config.combinations.delete('file3.txt')
70
+ end
64
71
 
65
72
  should "find a null src file if finding by an unknown digest path" do
66
73
  null_src = Dassets::NullSourceFile.new('not/found/digest/path')
@@ -75,16 +82,49 @@ class Dassets::SourceFile
75
82
  assert_nil null_src.mtime
76
83
  end
77
84
 
85
+ should "pass options to a null src when finding by an unknown digest path" do
86
+ null_src = Dassets::NullSourceFile.new('not/found/digest/path')
87
+ null_src_new_called_with = []
88
+ Assert.stub(Dassets::NullSourceFile, :new) do |*args|
89
+ null_src_new_called_with = args
90
+ null_src
91
+ end
92
+
93
+ options = {
94
+ :content_cache => Dassets::Cache::NoCache.new,
95
+ :fingerprint_cache => Dassets::Cache::NoCache.new
96
+ }
97
+ Dassets::SourceFile.find_by_digest_path('not/found/digest/path', options)
98
+
99
+ exp = ['not/found/digest/path', options]
100
+ assert_equal exp, null_src_new_called_with
101
+ end
102
+
78
103
  should "'proxy' the digest path if the path is a combination" do
79
- Dassets.config.combination 'file3.txt', ['file1.txt', 'file2.txt']
80
104
  src_proxy = Dassets::SourceProxy.new('file3.txt')
81
105
  null_combo_src = Dassets::NullSourceFile.new('file3.txt')
82
106
 
83
107
  assert_equal src_proxy.exists?, null_combo_src.exists?
84
108
  assert_equal src_proxy.content, null_combo_src.compiled
85
109
  assert_equal src_proxy.mtime, null_combo_src.mtime
110
+ end
86
111
 
87
- Dassets.config.combinations.delete('file3.txt')
112
+ should "pass options to its source proxy when the path is a combination" do
113
+ src_proxy = Dassets::SourceProxy.new('file3.txt')
114
+ src_proxy_new_called_with = []
115
+ Assert.stub(Dassets::SourceProxy, :new) do |*args|
116
+ src_proxy_new_called_with = args
117
+ src_proxy
118
+ end
119
+
120
+ options = {
121
+ :content_cache => Dassets::Cache::NoCache.new,
122
+ :fingerprint_cache => Dassets::Cache::NoCache.new
123
+ }
124
+ Dassets::NullSourceFile.new('file3.txt', options)
125
+
126
+ exp = ['file3.txt', options]
127
+ assert_equal exp, src_proxy_new_called_with
88
128
  end
89
129
 
90
130
  end
@@ -15,7 +15,8 @@ class Dassets::SourceProxy
15
15
  end
16
16
  subject{ @source_proxy }
17
17
 
18
- should have_readers :digest_path, :cache, :source_files
18
+ should have_readers :digest_path, :content_cache, :fingerprint_cache
19
+ should have_readers :source_files
19
20
  should have_imeths :key, :content, :fingerprint, :mtime, :exists?
20
21
 
21
22
  end
@@ -30,8 +31,9 @@ class Dassets::SourceProxy
30
31
  assert_equal 'file1.txt', subject.digest_path
31
32
  end
32
33
 
33
- should "use a `NoCache` cache handler by default" do
34
- assert_kind_of Dassets::Cache::NoCache, subject.cache
34
+ should "use no cache by default" do
35
+ assert_kind_of Dassets::Cache::NoCache, subject.content_cache
36
+ assert_kind_of Dassets::Cache::NoCache, subject.fingerprint_cache
35
37
  end
36
38
 
37
39
  should "have a single source file" do
@@ -155,7 +157,10 @@ class Dassets::SourceProxy
155
157
  desc "with a `NoCache` cache handler"
156
158
  setup do
157
159
  @cache = Dassets::Cache::NoCache.new
158
- @source_proxy = Dassets::SourceProxy.new('file1.txt', @cache)
160
+ @source_proxy = Dassets::SourceProxy.new('file1.txt', {
161
+ :content_cache => @cache,
162
+ :fingerprint_cache => @cache
163
+ })
159
164
  end
160
165
 
161
166
  should "not cache its source content/fingerprint" do
@@ -173,8 +178,12 @@ class Dassets::SourceProxy
173
178
  class MemCacheTests < UnitTests
174
179
  desc "with a `MemCache` cache handler"
175
180
  setup do
176
- @cache = Dassets::Cache::MemCache.new
177
- @source_proxy = Dassets::SourceProxy.new('file1.txt', @cache)
181
+ @content_cache = Dassets::Cache::MemCache.new
182
+ @fingerprint_cache = Dassets::Cache::MemCache.new
183
+ @source_proxy = Dassets::SourceProxy.new('file1.txt', {
184
+ :content_cache => @content_cache,
185
+ :fingerprint_cache => @fingerprint_cache
186
+ })
178
187
  end
179
188
 
180
189
  should "cache its source content/fingerprint in memory" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dassets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 47
4
+ hash: 43
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 12
8
+ - 13
9
9
  - 0
10
- version: 0.12.0
10
+ version: 0.13.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2015-11-24 00:00:00 Z
19
+ date: 2015-12-22 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -106,7 +106,7 @@ extra_rdoc_files: []
106
106
  files:
107
107
  - .gitignore
108
108
  - Gemfile
109
- - LICENSE.txt
109
+ - LICENSE
110
110
  - README.md
111
111
  - Rakefile
112
112
  - dassets.gemspec
@@ -133,7 +133,6 @@ files:
133
133
  - test/support/app/assets/nested/file3.txt
134
134
  - test/support/empty/.gitkeep
135
135
  - test/support/factory.rb
136
- - test/support/public/file1-daa05c683a4913b268653f7a7e36a5b4.txt
137
136
  - test/support/public/file2-9bbe1047cffbb590f59e0e5aeff46ae4.txt
138
137
  - test/support/public/grumpy_cat-b0d1f399a916f7a25c4c0f693c619013.jpg
139
138
  - test/support/public/nested/a-thing.txt-7413d18f2eba9c695a880aff67fde135.no-use
@@ -185,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
184
  requirements: []
186
185
 
187
186
  rubyforge_project:
188
- rubygems_version: 1.8.29
187
+ rubygems_version: 1.8.25
189
188
  signing_key:
190
189
  specification_version: 3
191
190
  summary: Digested asset files
@@ -199,7 +198,6 @@ test_files:
199
198
  - test/support/app/assets/nested/file3.txt
200
199
  - test/support/empty/.gitkeep
201
200
  - test/support/factory.rb
202
- - test/support/public/file1-daa05c683a4913b268653f7a7e36a5b4.txt
203
201
  - test/support/public/file2-9bbe1047cffbb590f59e0e5aeff46ae4.txt
204
202
  - test/support/public/grumpy_cat-b0d1f399a916f7a25c4c0f693c619013.jpg
205
203
  - test/support/public/nested/a-thing.txt-7413d18f2eba9c695a880aff67fde135.no-use