spritely 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 547b537b0bb23044c585abf41002bf689d94d3af
4
- data.tar.gz: dede178e411e13338852a0e331fab4ca7da38049
3
+ metadata.gz: 721132518267e110bd80c304728233e7205ee17b
4
+ data.tar.gz: 409be4e94f2e8c211e2c2ec409f0bb4d54f2f37b
5
5
  SHA512:
6
- metadata.gz: bd3f6cb6c50ef2404ac88f22ce1ac523388d0ab5a20b2d36923c3a958a44f02a6cc8ebca4b3b0d260345afe7efa999baecfa227d143fd09cbc6dd877baabc457
7
- data.tar.gz: cd38facbc1ac5ce4a817300633b8a80ad8e193e08e8597ce65eb88d51894ffd51d502aa379952c1c3f795966f47bbc367a5c7bfe1013176404c7c8e3eaf104b1
6
+ metadata.gz: 5140489bc42620192ebe3ca36fe6311baee5aece73a290d3a928345805b62745d93ed9bc8d667f135cda859502a9d143780771d453ce9dfa2713a905f0d883aa
7
+ data.tar.gz: 3eba0c2c8d580440ead81d95d9b0f893cf3f52247e2de139924b96dfddd405fa1759e0565ea6ed3bcac84f3b0d0d4640e90fb91cef485734fc8ea4711dd47249
data/lib/spritely.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'sass'
2
+ require 'sprockets/version'
2
3
  require 'spritely/sass_functions'
3
4
  require 'spritely/sprockets/manifest'
5
+ require 'spritely/adapters/sprockets_2'
6
+ require 'spritely/adapters/sprockets_3'
4
7
 
5
8
  module Spritely
6
9
  def self.environment
@@ -14,4 +17,12 @@ module Spritely
14
17
  def self.relative_folder_path
15
18
  Pathname.new(File.join('app', 'assets', 'images', 'sprites'))
16
19
  end
20
+
21
+ def self.sprockets_version
22
+ Gem::Version.new(Sprockets::VERSION).segments.first
23
+ end
24
+
25
+ def self.sprockets_adapter
26
+ Adapters.const_get("Sprockets#{sprockets_version}").new
27
+ end
17
28
  end
@@ -0,0 +1,13 @@
1
+ module Spritely
2
+ module Adapters
3
+ class Sprockets2
4
+ def reset_cache!(environment, filename)
5
+ environment.instance_variable_get(:@assets).delete_if { |_, asset| asset.pathname == filename }
6
+ environment.send(:trail).tap do |trail|
7
+ trail.instance_variable_get(:@entries).delete(Spritely.directory.to_s)
8
+ trail.instance_variable_get(:@stats).delete(filename.to_s)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Spritely
2
+ module Adapters
3
+ class Sprockets3
4
+ def reset_cache!(environment, filename)
5
+ environment.instance_variable_get(:@uris).delete_if { |_, asset| asset.pathname == filename }
6
+ environment.instance_variable_get(:@entries).delete(Spritely.directory.to_s)
7
+ environment.instance_variable_get(:@stats).delete(filename.to_s)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -4,7 +4,7 @@ module Spritely
4
4
  module SassFunctions
5
5
  def spritely_map(glob, kwargs = {})
6
6
  SpriteMap.create(glob.value, kwargs).tap do |sprite_map|
7
- reset_sprockets_caches!(sprite_map)
7
+ Spritely.sprockets_adapter.reset_cache!(sprockets_environment, sprite_map.filename)
8
8
  sprockets_context.depend_on(Spritely.directory)
9
9
  sprite_map.files.each do |file|
10
10
  sprockets_context.depend_on(file)
@@ -59,14 +59,6 @@ module Spritely
59
59
  def find_image(sprite_map, image_name)
60
60
  sprite_map.find(image_name.value) || raise(Sass::SyntaxError, "No image '#{image_name.value}' found in sprite map '#{sprite_map.name}'.")
61
61
  end
62
-
63
- def reset_sprockets_caches!(sprite_map)
64
- sprockets_environment.instance_variable_get(:@assets).delete_if { |_, asset| asset.pathname == sprite_map.filename }
65
- sprockets_environment.send(:trail).tap do |trail|
66
- trail.instance_variable_get(:@entries).delete(Spritely.directory.to_s)
67
- trail.instance_variable_get(:@stats).delete(sprite_map.filename.to_s)
68
- end
69
- end
70
62
  end
71
63
  end
72
64
 
@@ -1,3 +1,3 @@
1
1
  module Spritely
2
- VERSION = "0.2.7"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ # Rails 4.2 turns on asset digest in development.
2
+ # This turns them on for all Rails versions.
3
+ Rails.application.config.assets.digest = true
@@ -2,12 +2,14 @@ require 'spec_helper'
2
2
  require 'active_support/core_ext/string/strip'
3
3
 
4
4
  describe 'Stylesheet generation', :integration do
5
- subject(:stylesheet) { render_asset('sprites.css') }
5
+ subject!(:stylesheet) { render_asset('sprites.css') }
6
+
7
+ let!(:background_image_url) { spite_image_path("application") }
6
8
 
7
9
  describe 'body CSS with repetition' do
8
10
  it { should include(<<-CSS.strip_heredoc
9
11
  body {
10
- background-image: url(/assets/sprites/application.png);
12
+ background-image: url(#{background_image_url});
11
13
  background-position: 0 0;
12
14
  }
13
15
  CSS
@@ -17,7 +19,7 @@ describe 'Stylesheet generation', :integration do
17
19
  describe '#mario CSS with no repetition' do
18
20
  it { should include(<<-CSS.strip_heredoc
19
21
  #mario {
20
- background-image: url(/assets/sprites/application.png);
22
+ background-image: url(#{background_image_url});
21
23
  background-position: -150px -806px;
22
24
  width: 200px;
23
25
  height: 214px;
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,8 @@ RSpec.configure do |config|
12
12
  c.syntax = :expect
13
13
  end
14
14
 
15
+ config.include "Sprockets#{Spritely.sprockets_version}SassFunctionsHelpers".constantize, :sass_functions
16
+
15
17
  config.include RailsAppHelpers, :integration
16
18
  config.include GeneratorSpec::GeneratorExampleGroup, :generator
17
19
  config.around(:each, :integration) { |example| within_rails_app(&example) }
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  require 'sprockets'
3
3
  require 'ostruct'
4
4
 
5
- describe Spritely::SassFunctions do
5
+ describe Spritely::SassFunctions, :sass_functions do
6
6
  class SpriteMapDouble < Sass::Script::Literal
7
7
  def name; 'test'; end
8
8
  def filename; 'test.png'; end
@@ -15,6 +15,7 @@ describe Spritely::SassFunctions do
15
15
 
16
16
  before do
17
17
  allow(Spritely).to receive(:directory).and_return(directory)
18
+ allow(Spritely).to receive_message_chain(:sprockets_adapter, :reset_cache!)
18
19
  allow(Spritely::SpriteMap).to receive(:create).and_return(sprite_map)
19
20
  allow(sprite_map).to receive(:find).with('bar').and_return(image)
20
21
  end
@@ -28,11 +29,10 @@ describe Spritely::SassFunctions do
28
29
  end
29
30
 
30
31
  shared_examples "a sprite function that resets the sprockets directory caches" do
31
- it 'should clear out the trail caches' do
32
+ it 'should clear the Sprockets cache' do
32
33
  subject
33
- expect(environment.instance_variable_get(:@assets).length).to eq(0)
34
- expect(environment.send(:trail).instance_variable_get(:@entries)).to_not have_key(directory)
35
- expect(environment.send(:trail).instance_variable_get(:@stats)).to_not have_key('test.png')
34
+
35
+ expect(Spritely.sprockets_adapter).to have_received(:reset_cache!).with(sprockets_environment, 'test.png')
36
36
  end
37
37
  end
38
38
 
@@ -85,21 +85,4 @@ describe Spritely::SassFunctions do
85
85
 
86
86
  it { should eq(".height {\n height: 50px; }\n") }
87
87
  end
88
-
89
- def evaluate(value)
90
- Sprockets::ScssTemplate.new { value }.evaluate(environment.context_class.new(environment, nil, nil), nil)
91
- end
92
-
93
- def environment
94
- @environment ||= Sprockets::Environment.new.tap do |environment|
95
- environment.instance_variable_set(:@assets, {'test' => OpenStruct.new(pathname: 'test.png')})
96
- environment.send(:trail).instance_variable_set(:@entries, {'spritely-directory' => 'blah'})
97
- environment.send(:trail).instance_variable_set(:@stats, {'test.png' => 'blah'})
98
- environment.context_class.class_eval do
99
- def asset_path(path, options = {})
100
- path
101
- end
102
- end
103
- end
104
- end
105
88
  end
@@ -16,4 +16,10 @@ describe Spritely do
16
16
  describe '.relative_folder_path' do
17
17
  its(:relative_folder_path) { should eq(Pathname.new('app/assets/images/sprites')) }
18
18
  end
19
+
20
+ describe '.sprockets_version' do
21
+ before { stub_const('Sprockets::VERSION', '1.0') }
22
+
23
+ its(:sprockets_version) { should eq(1) }
24
+ end
19
25
  end
@@ -3,7 +3,8 @@ module RailsAppHelpers
3
3
  '--skip-active-record',
4
4
  '--skip-test-unit',
5
5
  '--skip-javascript',
6
- '--skip-spring'
6
+ '--skip-spring',
7
+ '--skip-bundle'
7
8
  ]
8
9
 
9
10
  def within_rails_app(&block)
@@ -16,6 +17,7 @@ module RailsAppHelpers
16
17
  Bundler.with_clean_env do
17
18
  File.open('Gemfile', 'a') do |f|
18
19
  f.write("gem 'spritely', path: '#{__dir__}/../../'\n")
20
+ f.write("gem 'sprockets', '#{Sprockets::VERSION}'")
19
21
  end
20
22
  %x(bundle install)
21
23
  %x(rails generate spritely:install)
@@ -31,6 +33,12 @@ module RailsAppHelpers
31
33
  runner "puts Rails.application.assets[#{filename.inspect}]"
32
34
  end
33
35
 
36
+ def spite_image_path(sprite_name)
37
+ # TODO: When Sprockets 2.0 support is dropped (probably when Sprockets 4.0 is released), swap `digest` for `hexdigest`
38
+ fingerprint = runner(%~puts Rails.application.assets["sprites/#{sprite_name}.png"].digest~)
39
+ "/assets/sprites/application-#{fingerprint.strip}.png"
40
+ end
41
+
34
42
  def compile_assets
35
43
  %x(RAILS_ENV=production rake assets:precompile)
36
44
  end
@@ -0,0 +1,13 @@
1
+ module Sprockets2SassFunctionsHelpers
2
+ def evaluate(value)
3
+ Sprockets::ScssTemplate.new { value }.evaluate(sprockets_environment.context_class.new(sprockets_environment, nil, nil), nil)
4
+ end
5
+
6
+ def sprockets_environment
7
+ @sprockets_environment ||= Sprockets::Environment.new.tap do |sprockets_environment|
8
+ sprockets_environment.context_class.class_eval do
9
+ def asset_path(path, options = {}); path; end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Sprockets3SassFunctionsHelpers
2
+ def evaluate(value)
3
+ Sprockets::ScssProcessor.call(environment: sprockets_environment, data: value, filename: "test.scss", metadata: {}, cache: Sprockets::Cache.new)[:data]
4
+ end
5
+
6
+ def sprockets_environment
7
+ @sprockets_environment ||= Sprockets::CachedEnvironment.new(Sprockets::Environment.new).tap do |sprockets_environment|
8
+ sprockets_environment.context_class.class_eval do
9
+ def depend_on(path); end
10
+ def depend_on_asset(path); end
11
+ def asset_path(path, options = {}); path; end
12
+ end
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spritely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Robbin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-26 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 4.0.0
33
+ version: '4.1'
34
34
  - - "<"
35
35
  - !ruby/object:Gem::Version
36
36
  version: '5.0'
@@ -40,7 +40,7 @@ dependencies:
40
40
  requirements:
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
- version: 4.0.0
43
+ version: '4.1'
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '5.0'
@@ -62,14 +62,14 @@ dependencies:
62
62
  name: sprockets-rails
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - "~>"
65
+ - - ">="
66
66
  - !ruby/object:Gem::Version
67
67
  version: '2.0'
68
68
  type: :runtime
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - "~>"
72
+ - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: '2.0'
75
75
  - !ruby/object:Gem::Dependency
@@ -165,6 +165,8 @@ extra_rdoc_files: []
165
165
  files:
166
166
  - lib/generators/spritely/install_generator.rb
167
167
  - lib/spritely.rb
168
+ - lib/spritely/adapters/sprockets_2.rb
169
+ - lib/spritely/adapters/sprockets_3.rb
168
170
  - lib/spritely/cache.rb
169
171
  - lib/spritely/collection.rb
170
172
  - lib/spritely/generators/base.rb
@@ -186,6 +188,7 @@ files:
186
188
  - spec/fixtures/rails-app/app/assets/images/foo/fool.png
187
189
  - spec/fixtures/rails-app/app/assets/stylesheets/sprites.css.scss
188
190
  - spec/fixtures/rails-app/app/assets/stylesheets/sprites_2.css.scss
191
+ - spec/fixtures/rails-app/config/initializers/assets.rb
189
192
  - spec/fixtures/test/foo.png
190
193
  - spec/generators/spritely/install_generator_spec.rb
191
194
  - spec/integration/precompilation_spec.rb
@@ -202,6 +205,8 @@ files:
202
205
  - spec/spritely_spec.rb
203
206
  - spec/support/rails_app_helpers.rb
204
207
  - spec/support/shared_examples.rb
208
+ - spec/support/shared_examples/sprockets_2_sass_functions_helpers.rb
209
+ - spec/support/shared_examples/sprockets_3_sass_functions_helpers.rb
205
210
  homepage: https://github.com/agrobbin/spritely
206
211
  licenses:
207
212
  - MIT
@@ -222,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
227
  version: '0'
223
228
  requirements: []
224
229
  rubyforge_project:
225
- rubygems_version: 2.2.2
230
+ rubygems_version: 2.4.5.1
226
231
  signing_key:
227
232
  specification_version: 4
228
233
  summary: Hooks into the Rails asset pipeline to allow you to easily generate sprite
@@ -236,6 +241,7 @@ test_files:
236
241
  - spec/fixtures/rails-app/app/assets/images/foo/fool.png
237
242
  - spec/fixtures/rails-app/app/assets/stylesheets/sprites.css.scss
238
243
  - spec/fixtures/rails-app/app/assets/stylesheets/sprites_2.css.scss
244
+ - spec/fixtures/rails-app/config/initializers/assets.rb
239
245
  - spec/fixtures/rails-app-changes/app/assets/images/application/dropdown-arrow.png
240
246
  - spec/fixtures/rails-app-changes/app/assets/stylesheets/sprites.css.scss
241
247
  - spec/fixtures/test/foo.png
@@ -253,4 +259,6 @@ test_files:
253
259
  - spec/spritely/sprite_map_spec.rb
254
260
  - spec/spritely_spec.rb
255
261
  - spec/support/rails_app_helpers.rb
262
+ - spec/support/shared_examples/sprockets_2_sass_functions_helpers.rb
263
+ - spec/support/shared_examples/sprockets_3_sass_functions_helpers.rb
256
264
  - spec/support/shared_examples.rb