inline_svg 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of inline_svg might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -1
- data/README.md +26 -0
- data/lib/inline_svg.rb +15 -1
- data/lib/inline_svg/action_view/helpers.rb +5 -1
- data/lib/inline_svg/version.rb +1 -1
- data/spec/inline_svg_spec.rb +38 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6932c866bafacb7f12847808bb7f4b4d4c95174
|
4
|
+
data.tar.gz: 7238dcab1a92255185e0caeae55b454a1cddc886
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 792b0ab6aeb91af7c8edcab1cbd37eca42d4fd96317a8bba31f41f4e0f53649e9b92039a8a11b8c6d880ce1fda4524c7ebec66bdac9d1a121787f51adef57ba3
|
7
|
+
data.tar.gz: 28605844152d134e18810c12db87c4df901b55d7c365c4b399e7b8ea0cce6b05508d90b0632d52a9e2f3d071e625405cb767ebcb595eb2c913e8d40cff8eb5d9
|
data/CHANGELOG.md
CHANGED
@@ -3,7 +3,8 @@ 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
|
-
-
|
6
|
+
- Allow configurable asset file implementations
|
7
|
+
[#61](https://github.com/jamesmartin/inline_svg/pull/61)
|
7
8
|
|
8
9
|
## [1.0.1] - 2017-04-10
|
9
10
|
### Fixed
|
data/README.md
CHANGED
@@ -206,6 +206,32 @@ Transforms are applied in ascending order (lowest number first).
|
|
206
206
|
***Note***: Custom transformations are always applied *after* all built-in
|
207
207
|
transformations, regardless of priority.
|
208
208
|
|
209
|
+
## Custom asset file loader
|
210
|
+
|
211
|
+
An asset file loader returns a `String` representing a SVG document given a
|
212
|
+
filename. Custom asset loaders should be a Ruby object that responds to a
|
213
|
+
method called `named`, that takes one argument (a string representing the
|
214
|
+
filename of the SVG document).
|
215
|
+
|
216
|
+
A simple example might look like this:
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
class MyAssetFileLoader
|
220
|
+
def self.named(filename)
|
221
|
+
# ... load SVG document however you like
|
222
|
+
return "<svg>some document</svg>"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
```
|
226
|
+
|
227
|
+
Configure your custom asset file loader in an initializer like so:
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
InlineSvg.configure do |config|
|
231
|
+
config.asset_file = MyAssetFileLoader
|
232
|
+
end
|
233
|
+
```
|
234
|
+
|
209
235
|
## Contributing
|
210
236
|
|
211
237
|
1. Fork it ( [http://github.com/jamesmartin/inline_svg/fork](http://github.com/jamesmartin/inline_svg/fork) )
|
data/lib/inline_svg.rb
CHANGED
@@ -14,10 +14,24 @@ module InlineSvg
|
|
14
14
|
class Configuration
|
15
15
|
class Invalid < ArgumentError; end
|
16
16
|
|
17
|
-
attr_reader :asset_finder, :custom_transformations
|
17
|
+
attr_reader :asset_file, :asset_finder, :custom_transformations
|
18
18
|
|
19
19
|
def initialize
|
20
20
|
@custom_transformations = {}
|
21
|
+
@asset_file = InlineSvg::AssetFile
|
22
|
+
end
|
23
|
+
|
24
|
+
def asset_file=(custom_asset_file)
|
25
|
+
begin
|
26
|
+
method = custom_asset_file.method(:named)
|
27
|
+
if method.arity == 1
|
28
|
+
@asset_file = custom_asset_file
|
29
|
+
else
|
30
|
+
raise InlineSvg::Configuration::Invalid.new("asset_file should implement the #named method with arity 1")
|
31
|
+
end
|
32
|
+
rescue NameError
|
33
|
+
raise InlineSvg::Configuration::Invalid.new("asset_file should implement the #named method")
|
34
|
+
end
|
21
35
|
end
|
22
36
|
|
23
37
|
def asset_finder=(finder)
|
@@ -9,7 +9,7 @@ module InlineSvg
|
|
9
9
|
svg_file = if InlineSvg::IOResource === filename
|
10
10
|
InlineSvg::IOResource.read filename
|
11
11
|
else
|
12
|
-
|
12
|
+
configured_asset_file.named filename
|
13
13
|
end
|
14
14
|
rescue InlineSvg::AssetFile::FileNotFound
|
15
15
|
return "<svg><!-- SVG file not found: '#{filename}' #{extension_hint(filename)}--></svg>".html_safe
|
@@ -20,6 +20,10 @@ module InlineSvg
|
|
20
20
|
|
21
21
|
private
|
22
22
|
|
23
|
+
def configured_asset_file
|
24
|
+
InlineSvg.configuration.asset_file
|
25
|
+
end
|
26
|
+
|
23
27
|
def extension_hint(filename)
|
24
28
|
filename.ends_with?(".svg") ? "" : "(Try adding .svg to your filename) "
|
25
29
|
end
|
data/lib/inline_svg/version.rb
CHANGED
data/spec/inline_svg_spec.rb
CHANGED
@@ -13,6 +13,10 @@ class MyInvalidCustomTransformInstance
|
|
13
13
|
def self.create_with_value(value); end
|
14
14
|
end
|
15
15
|
|
16
|
+
class MyCustomAssetFile
|
17
|
+
def self.named(filename); end
|
18
|
+
end
|
19
|
+
|
16
20
|
describe InlineSvg do
|
17
21
|
describe "configuration" do
|
18
22
|
context "when a block is not given" do
|
@@ -42,6 +46,40 @@ describe InlineSvg do
|
|
42
46
|
end
|
43
47
|
end
|
44
48
|
|
49
|
+
context "configuring a custom asset file" do
|
50
|
+
it "falls back to the built-in asset file implementation by deafult" do
|
51
|
+
expect(InlineSvg.configuration.asset_file).to eq(InlineSvg::AssetFile)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "adds a collaborator that meets the interface specification" do
|
55
|
+
InlineSvg.configure do |config|
|
56
|
+
config.asset_file = MyCustomAssetFile
|
57
|
+
end
|
58
|
+
|
59
|
+
expect(InlineSvg.configuration.asset_file).to eq MyCustomAssetFile
|
60
|
+
end
|
61
|
+
|
62
|
+
it "rejects a collaborator that does not conform to the interface spec" do
|
63
|
+
bad_asset_file = double("bad_asset_file")
|
64
|
+
|
65
|
+
expect do
|
66
|
+
InlineSvg.configure do |config|
|
67
|
+
config.asset_file = bad_asset_file
|
68
|
+
end
|
69
|
+
end.to raise_error(InlineSvg::Configuration::Invalid, /asset_file should implement the #named method/)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "rejects a collaborator that implements the correct interface with the wrong arity" do
|
73
|
+
bad_asset_file = double("bad_asset_file", named: nil)
|
74
|
+
|
75
|
+
expect do
|
76
|
+
InlineSvg.configure do |config|
|
77
|
+
config.asset_file = bad_asset_file
|
78
|
+
end
|
79
|
+
end.to raise_error(InlineSvg::Configuration::Invalid, /asset_file should implement the #named method with arity 1/)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
45
83
|
context "configuring custom transformation" do
|
46
84
|
it "allows a custom transformation to be added" do
|
47
85
|
InlineSvg.configure do |config|
|
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.0
|
4
|
+
version: 1.1.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-
|
11
|
+
date: 2017-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|