inline_svg 1.1.0 → 1.2.0

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.

Potentially problematic release.


This version of inline_svg might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6932c866bafacb7f12847808bb7f4b4d4c95174
4
- data.tar.gz: 7238dcab1a92255185e0caeae55b454a1cddc886
3
+ metadata.gz: 9af50a1ce42f8f67f9df531b740b4d0f8adedc2b
4
+ data.tar.gz: 4c4399fffe814ff7587851cefd33deb3cb1910ce
5
5
  SHA512:
6
- metadata.gz: 792b0ab6aeb91af7c8edcab1cbd37eca42d4fd96317a8bba31f41f4e0f53649e9b92039a8a11b8c6d880ce1fda4524c7ebec66bdac9d1a121787f51adef57ba3
7
- data.tar.gz: 28605844152d134e18810c12db87c4df901b55d7c365c4b399e7b8ea0cce6b05508d90b0632d52a9e2f3d071e625405cb767ebcb595eb2c913e8d40cff8eb5d9
6
+ metadata.gz: d0facb0d977943f429edd52e7ac6307b3c092dd53a16bd694214048157a3b128cbdd6eb8c168f9a71889837a4427e30b1e97ffffd4b55fd353c87e6aae798fca
7
+ data.tar.gz: 1e3e2fe9c6f563bd654fbbba92a216eef5efa340f2d755807ad2934d4b1bccf1bb70877f8ee74e2e1c5192aa8edef894f175f19890c76e6882eeec9e25deab48
data/CHANGELOG.md CHANGED
@@ -3,6 +3,15 @@ All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
5
  ## [Unreleased][unreleased]
6
+ - Nothing
7
+
8
+ ## [1.2.0] - 2017-04-20
9
+ ### Added
10
+ - Cached asset file (load assets into memory at boot time)
11
+ [#62](https://github.com/jamesmartin/inline_svg/pull/62)
12
+
13
+ ## [1.1.0] - 2017-04-12
14
+ ### Added
6
15
  - Allow configurable asset file implementations
7
16
  [#61](https://github.com/jamesmartin/inline_svg/pull/61)
8
17
 
@@ -145,7 +154,9 @@ transformations](https://github.com/jamesmartin/inline_svg/blob/master/README.md
145
154
  ### Added
146
155
  - Basic Railtie and view helper to inline SVG documents to Rails views.
147
156
 
148
- [unreleased]: https://github.com/jamesmartin/inline_svg/compare/v1.0.1...HEAD
157
+ [unreleased]: https://github.com/jamesmartin/inline_svg/compare/v1.2.0...HEAD
158
+ [1.2.0]: https://github.com/jamesmartin/inline_svg/compare/v1.1.0...v1.2.0
159
+ [1.1.0]: https://github.com/jamesmartin/inline_svg/compare/v1.0.1...v1.1.0
149
160
  [1.0.1]: https://github.com/jamesmartin/inline_svg/compare/v1.0.0...v1.0.1
150
161
  [1.0.0]: https://github.com/jamesmartin/inline_svg/compare/v0.12.1...v1.0.0
151
162
  [0.12.1]: https://github.com/jamesmartin/inline_svg/compare/v0.12.0...v0.12.1
data/README.md CHANGED
@@ -42,8 +42,7 @@ actual path of the file on disk is resolved using
42
42
  [Sprockets](://github.com/sstephenson/sprockets) (when available), a naive file finder (`/public/assets/...`) or in the case of `IO` objects the SVG data is read from the object.
43
43
  This means you can pre-process and fingerprint your SVG files like other Rails assets, or choose to find SVG data yourself.
44
44
 
45
- Here's an example of embedding an SVG document and applying a 'class' attribute in
46
- HAML:
45
+ Here's an example of embedding an SVG document and applying a 'class' attribute:
47
46
 
48
47
  ```erb
49
48
  <html>
@@ -232,6 +231,32 @@ InlineSvg.configure do |config|
232
231
  end
233
232
  ```
234
233
 
234
+ ## Caching all assets at boot time
235
+
236
+ When your deployment strategy prevents dynamic asset file loading from disk it
237
+ can be helpful to cache all possible SVG assets in memory at application boot
238
+ time. In this case, you can configure the `InlineSvg::CachedAssetFile` to scan
239
+ any number of paths on disks and load all the assets it finds into memory.
240
+
241
+ For example, in this configuration we load every `*.svg` file found beneath the
242
+ configured paths into memory:
243
+
244
+ ```ruby
245
+ InlineSvg.configure do |config|
246
+ config.asset_file = InlineSvg::CachedAssetFile.new(
247
+ paths: [
248
+ "#{Rails.root}/public/path/to/assets",
249
+ "#{Rails.root}/public/other/path/to/assets"
250
+ ],
251
+ filters: /\.svg/
252
+ )
253
+ end
254
+ ```
255
+
256
+ **Note:** Paths are read recursively, so think about keeping your SVG assets
257
+ restricted to as few paths as possible, and using the filter option to further
258
+ restrict assets to only those likely to be used by `inline_svg`.
259
+
235
260
  ## Contributing
236
261
 
237
262
  1. Fork it ( [http://github.com/jamesmartin/inline_svg/fork](http://github.com/jamesmartin/inline_svg/fork) )
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InlineSvg
4
+ class CachedAssetFile
5
+ attr_reader :assets, :filters, :paths
6
+
7
+ # For each of the given paths, recursively reads each asset and stores its
8
+ # contents alongside the full path to the asset.
9
+ #
10
+ # paths - One or more String representing directories on disk to search
11
+ # for asset files. Note: paths are searched recursively.
12
+ # filters - One or more Strings/Regexps to match assets against. Only
13
+ # assets matching all filters will be cached and available to load.
14
+ # Note: Specifying no filters will cache every file found in
15
+ # paths.
16
+ #
17
+ def initialize(paths:, filters: [])
18
+ @paths = Array(paths).compact.map { |p| Pathname.new(p) }
19
+ @filters = Array(filters).map { |f| Regexp.new(f) }
20
+ @assets = @paths.reduce({}) { |assets, p| assets.merge(read_assets(assets, p)) }
21
+ end
22
+
23
+ # Public: Finds the named asset and returns the contents as a string.
24
+ #
25
+ # asset_name - A string representing the name of the asset to load
26
+ #
27
+ # Returns: A String or raises InlineSvg::AssetFile::FileNotFound error
28
+ def named(asset_name)
29
+ assets[key_for_asset(asset_name)] or
30
+ raise InlineSvg::AssetFile::FileNotFound.new("Asset not found: #{asset_name}")
31
+ end
32
+
33
+ private
34
+ # Internal: Finds the key for a given asset name (using a Regex).
35
+ #
36
+ # Returns a String representing the key for the named asset.
37
+ def key_for_asset(asset_name)
38
+ assets.keys.map { |k| k.to_s }.select { |k| /#{asset_name}/.match(k) }.first
39
+ end
40
+
41
+ # Internal: Recursively descends through current_paths reading each file it
42
+ # finds and adding them to the accumulator if the fullpath of the file
43
+ # matches all configured filters.
44
+ #
45
+ # acc - Hash representing the accumulated assets keyed by full path
46
+ # paths - Pathname representing the current node in the directory
47
+ # structure to consider
48
+ #
49
+ # Returns a Hash containing the contents of each asset, keyed by fullpath
50
+ # to the asset.
51
+ def read_assets(acc, paths)
52
+ paths.each_child do |child|
53
+ if child.directory?
54
+ read_assets(acc, child)
55
+ elsif child.readable_real?
56
+ acc[child.to_s] = File.read(child) if matches_all_filters?(child)
57
+ end
58
+ end
59
+ acc
60
+ end
61
+
62
+ def matches_all_filters?(path)
63
+ filters.all? { |f| f.match(path.to_s) }
64
+ end
65
+ end
66
+ end
@@ -1,3 +1,3 @@
1
1
  module InlineSvg
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/inline_svg.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "inline_svg/version"
2
2
  require "inline_svg/action_view/helpers"
3
3
  require "inline_svg/asset_file"
4
+ require "inline_svg/cached_asset_file"
4
5
  require "inline_svg/finds_asset_paths"
5
6
  require "inline_svg/static_asset_finder"
6
7
  require "inline_svg/transform_pipeline"
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+ require 'pathname'
3
+ require_relative '../lib/inline_svg'
4
+
5
+ describe InlineSvg::CachedAssetFile do
6
+ let(:fixture_path) { Pathname.new(File.expand_path("../files/static_assets", __FILE__)) }
7
+
8
+ it "loads assets under configured paths" do
9
+ known_document = File.read(fixture_path.join("assets0", "known-document.svg"))
10
+
11
+ asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path.join("assets0"))
12
+
13
+ expect(asset_loader.named("known-document.svg")).to eq(known_document)
14
+ end
15
+
16
+ it "does not include assets outside of configured paths" do
17
+ asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path.join("assets0"))
18
+
19
+ expect(fixture_path.join("assets1", "other-document.svg")).to be_file
20
+ expect do
21
+ asset_loader.named("other-document.svg")
22
+ end.to raise_error InlineSvg::AssetFile::FileNotFound
23
+ end
24
+
25
+ it "differentiates two files with the same name" do
26
+ known_document_0 = File.read(fixture_path.join("assets0", "known-document.svg"))
27
+ known_document_1 = File.read(fixture_path.join("assets1", "known-document.svg"))
28
+
29
+ expect(known_document_0).not_to eq(known_document_1)
30
+
31
+ asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path)
32
+
33
+ expect(known_document_0).to eq(asset_loader.named("assets0/known-document.svg"))
34
+ expect(known_document_1).to eq(asset_loader.named("assets1/known-document.svg"))
35
+ end
36
+
37
+ it "filters wanted files by simple string matching" do
38
+ known_document_0 = File.read(fixture_path.join("assets0", "known-document.svg"))
39
+ known_document_1 = File.read(fixture_path.join("assets1", "known-document.svg"))
40
+
41
+ asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path, filters: "assets1")
42
+
43
+ expect do
44
+ asset_loader.named("assets0/known-document.svg")
45
+ end.to raise_error InlineSvg::AssetFile::FileNotFound
46
+
47
+ expect(known_document_1).to eq(asset_loader.named("assets1/known-document.svg"))
48
+ end
49
+
50
+ it "filters wanted files by regex matching" do
51
+ known_document_1 = File.read(fixture_path.join("assets1", "known-document.svg"))
52
+
53
+ asset_loader = InlineSvg::CachedAssetFile.new(paths: fixture_path, filters: ["assets1", /\.svg/])
54
+
55
+ expect do
56
+ asset_loader.named("assets1/some-file.txt")
57
+ end.to raise_error InlineSvg::AssetFile::FileNotFound
58
+
59
+ expect(known_document_1).to eq(asset_loader.named("assets1/known-document.svg"))
60
+ end
61
+ end
@@ -0,0 +1 @@
1
+ <svg>interesting content</svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M3.273 7.151l-2.917-2.916c-.23.665-.356 1.361-.356 2.057 0 1.608.624 3.216 1.851 4.442 1.35 1.351 3.163 1.957 4.928 1.821.933-.072 1.851.268 2.513.93l9.646 9.646c.58.579 1.338.869 2.097.869 1.636 0 2.965-1.326 2.965-2.965 0-.759-.29-1.518-.868-2.097l-9.647-9.646c-.661-.662-1.002-1.581-.93-2.514.136-1.766-.47-3.578-1.821-4.928-.372-.372-.778-.686-1.209-.945l-6.252 6.246zm18.727 13.849c0 .552-.448 1-1 1s-1-.448-1-1 .448-1 1-1 1 .447 1 1zm-12.153-13.396l-3.061 3.061-2.566-2.567 3.062-3.061 2.565 2.567zm-.933.096l-.762.761-1.705-1.705.762-.762 1.705 1.706zm-2.991-.42l-.761.762 1.706 1.705.762-.762-1.707-1.705zm2.484-6.903l-2.893 2.893-2.412-2.412c.953-.556 2.044-.858 3.165-.858.707 0 1.425.12 2.128.373l.012.004z"/></svg>
@@ -0,0 +1 @@
1
+ <svg>Another known document</svg>
@@ -0,0 +1,3 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
3
+ <svg>Other document</svg>
@@ -0,0 +1 @@
1
+ Some file contents.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inline_svg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Martin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-12 00:00:00.000000000 Z
11
+ date: 2017-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -126,6 +126,7 @@ files:
126
126
  - lib/inline_svg.rb
127
127
  - lib/inline_svg/action_view/helpers.rb
128
128
  - lib/inline_svg/asset_file.rb
129
+ - lib/inline_svg/cached_asset_file.rb
129
130
  - lib/inline_svg/finds_asset_paths.rb
130
131
  - lib/inline_svg/id_generator.rb
131
132
  - lib/inline_svg/io_resource.rb
@@ -147,7 +148,13 @@ files:
147
148
  - lib/inline_svg/transform_pipeline/transformations/width.rb
148
149
  - lib/inline_svg/version.rb
149
150
  - spec/asset_file_spec.rb
151
+ - spec/cached_asset_file_spec.rb
150
152
  - spec/files/example.svg
153
+ - spec/files/static_assets/assets0/known-document.svg
154
+ - spec/files/static_assets/assets0/some-document.svg
155
+ - spec/files/static_assets/assets1/known-document.svg
156
+ - spec/files/static_assets/assets1/other-document.svg
157
+ - spec/files/static_assets/assets1/some-file.txt
151
158
  - spec/finds_asset_paths_spec.rb
152
159
  - spec/helpers/inline_svg_spec.rb
153
160
  - spec/id_generator_spec.rb
@@ -190,7 +197,13 @@ specification_version: 4
190
197
  summary: Embeds an SVG document, inline.
191
198
  test_files:
192
199
  - spec/asset_file_spec.rb
200
+ - spec/cached_asset_file_spec.rb
193
201
  - spec/files/example.svg
202
+ - spec/files/static_assets/assets0/known-document.svg
203
+ - spec/files/static_assets/assets0/some-document.svg
204
+ - spec/files/static_assets/assets1/known-document.svg
205
+ - spec/files/static_assets/assets1/other-document.svg
206
+ - spec/files/static_assets/assets1/some-file.txt
194
207
  - spec/finds_asset_paths_spec.rb
195
208
  - spec/helpers/inline_svg_spec.rb
196
209
  - spec/id_generator_spec.rb