inline_svg 0.5.3 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4bd0dc863ab3a27cdd10a6c7377b0a065792367a
4
- data.tar.gz: 515ff976eac0d9b795d75a7906c9889a7c006b67
3
+ metadata.gz: fde0d0a49947181287301e1187f1997776562082
4
+ data.tar.gz: cf777cfc4ca0b6df101d1f40c18b1dc48d1421d7
5
5
  SHA512:
6
- metadata.gz: 030d7333cfc87e0bcbb0f47932a5e10405903aebaf55d2ceb350edbf11fcc4993dfd9eb95f37418a6aa34f67c60d79c365379aeadc874dddd4c94cd10f6d3ae0
7
- data.tar.gz: bb3fd6d2022b283f8494472bbf5a1865a818b1fd22c1b0b265a0d8328913b53889a08acf83a1923bbd50465a42972ac1eb065f883906fd9464c5a259187e3a99
6
+ metadata.gz: 2efa11f12a1c8caaae63242dd9f389e97edbbbd9ce3104c02ae7f319325bd8474743dc902ccea28cfb23481bf29c6337d31187895e84f58c0b70e91d21530995
7
+ data.tar.gz: 50613509d30a572898a1c7108a93885b202eb2a61b0afaf8179b4a8fcbc5c54e44ff38a259f061566585e1cb27f5d559c9aebd9769e81f323c16be572f417aea
@@ -5,6 +5,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
5
5
  ## [Unreleased][unreleased]
6
6
  - Nothing.
7
7
 
8
+ ## [0.6.0] - 2015-07-07
9
+ ### Added
10
+ - Apply user-supplied [custom
11
+ transformations](https://github.com/jamesmartin/inline_svg/blob/master/README.md#custom-transformations) to a document.
12
+
8
13
  ## [0.5.3] - 2015-06-22
9
14
  ### Added
10
15
  - `preserveAspectRatio` transformation on SVG root node. Thanks, @paulozoom.
@@ -56,7 +61,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
56
61
  ### Added
57
62
  - Basic Railtie and view helper to inline SVG documents to Rails views.
58
63
 
59
- [unreleased]: https://github.com/jamesmartin/inline_svg/compare/v0.5.3...HEAD
64
+ [unreleased]: https://github.com/jamesmartin/inline_svg/compare/v0.6.0...HEAD
65
+ [0.6.0]: https://github.com/jamesmartin/inline_svg/compare/v0.5.3...v0.6.0
60
66
  [0.5.3]: https://github.com/jamesmartin/inline_svg/compare/v0.5.2...v0.5.3
61
67
  [0.5.2]: https://github.com/jamesmartin/inline_svg/compare/v0.5.1...v0.5.2
62
68
  [0.5.1]: https://github.com/jamesmartin/inline_svg/compare/v0.5.0...v0.5.1
data/README.md CHANGED
@@ -84,6 +84,47 @@ inline_svg("some-document.svg", id: 'some-id', class: 'some-class', data: {some:
84
84
  'Some description', nocomment: true, preserve_aspect_ratio: 'xMaxYMax meet')
85
85
  ```
86
86
 
87
+ ## Custom Transformations
88
+
89
+ The transformation behavior of `inline_svg` can be customized by creating custom transformation classes.
90
+
91
+ For example, inherit from `InlineSvg::CustomTransformation` and implement the `#transform` method:
92
+
93
+ ```ruby
94
+ # Sets the `custom` attribute on the root SVG element to supplied value
95
+ # Remember to return a document, as this will be passed along the transformation chain
96
+
97
+ class MyCustomTransform < InlineSvg::CustomTransformation
98
+ def transform(doc)
99
+ doc = Nokogiri::XML::Document.parse(doc.to_html)
100
+ svg = doc.at_css 'svg'
101
+ svg['custom'] = value
102
+ doc
103
+ end
104
+ end
105
+ ```
106
+
107
+ Add the custom configuration in an initializer (E.g. `./config/initializers/inline_svg.rb`):
108
+
109
+ ```ruby
110
+ # Note that the named `attribute` will be used to pass a value to your custom transform
111
+ InlineSvg.configure do |config|
112
+ config.add_custom_transformation(attribute: :my_custom_attribute, transform: MyCustomTransform)
113
+ end
114
+ ```
115
+
116
+ The custom transformation can then be called like so:
117
+ ```haml
118
+ %div
119
+ = inline_svg "some-document.svg", my_custom_attribute: 'some value'
120
+ ```
121
+
122
+ In this example, the following transformation would be applied to a SVG document:
123
+
124
+ ```xml
125
+ <svg custom="some value">...</svg>
126
+ ```
127
+
87
128
  ## Contributing
88
129
 
89
130
  1. Fork it ( [http://github.com/jamesmartin/inline_svg/fork](http://github.com/jamesmartin/inline_svg/fork) )
@@ -12,7 +12,11 @@ module InlineSvg
12
12
  class Configuration
13
13
  class Invalid < ArgumentError; end
14
14
 
15
- attr_reader :asset_finder
15
+ attr_reader :asset_finder, :custom_transformations
16
+
17
+ def initialize
18
+ @custom_transformations = {}
19
+ end
16
20
 
17
21
  def asset_finder=(finder)
18
22
  if finder.respond_to?(:find_asset)
@@ -22,6 +26,23 @@ module InlineSvg
22
26
  end
23
27
  asset_finder
24
28
  end
29
+
30
+ def add_custom_transformation(options)
31
+ if incompatible_transformation?(options.fetch(:transform))
32
+ raise InlineSvg::Configuration::Invalid.new("#{options.fetch(:transform)} should implement the .create_with_value and #transform methods")
33
+ end
34
+ @custom_transformations.merge!(Hash[ *[options.fetch(:attribute, :no_attribute), options.fetch(:transform, no_transform)] ])
35
+ end
36
+
37
+ private
38
+
39
+ def incompatible_transformation?(klass)
40
+ !klass.is_a?(Class) || !klass.respond_to?(:create_with_value) || !klass.instance_methods.include?(:transform)
41
+ end
42
+
43
+ def no_transform
44
+ InlineSvg::TransformPipeline::Transformations::NullTransformation
45
+ end
25
46
  end
26
47
 
27
48
  @configuration = InlineSvg::Configuration.new
@@ -36,5 +57,9 @@ module InlineSvg
36
57
  raise InlineSvg::Configuration::Invalid.new('Please set configuration options with a block')
37
58
  end
38
59
  end
60
+
61
+ def reset_configuration!
62
+ @configuration = InlineSvg::Configuration.new
63
+ end
39
64
  end
40
65
  end
@@ -1,5 +1,5 @@
1
1
  module InlineSvg::TransformPipeline::Transformations
2
- def self.all_transformations
2
+ def self.built_in_transformations
3
3
  {
4
4
  nocomment: NoComment,
5
5
  class: ClassAttribute,
@@ -14,6 +14,14 @@ module InlineSvg::TransformPipeline::Transformations
14
14
  }
15
15
  end
16
16
 
17
+ def self.custom_transformations
18
+ InlineSvg.configuration.custom_transformations
19
+ end
20
+
21
+ def self.all_transformations
22
+ built_in_transformations.merge(custom_transformations)
23
+ end
24
+
17
25
  def self.lookup(transform_params)
18
26
  without_empty_values(transform_params).map do |key, value|
19
27
  all_transformations.fetch(key, NullTransformation).create_with_value(value)
@@ -21,3 +21,15 @@ module InlineSvg::TransformPipeline::Transformations
21
21
  end
22
22
  end
23
23
  end
24
+
25
+ module InlineSvg
26
+ class CustomTransformation < InlineSvg::TransformPipeline::Transformations::Transformation
27
+ # Inherit from this class to keep custom transformation class definitions short
28
+ # E.g.
29
+ # class MyTransform < InlineSvg::CustomTransformation
30
+ # def transform(doc)
31
+ # # Your code here...
32
+ # end
33
+ # end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module InlineSvg
2
- VERSION = "0.5.3"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -1,5 +1,14 @@
1
1
  require 'inline_svg'
2
2
 
3
+ class WorkingCustomTransform < InlineSvg::CustomTransformation
4
+ def transform(doc)
5
+ doc = Nokogiri::XML::Document.parse(doc.to_html)
6
+ svg = doc.at_css 'svg'
7
+ svg['custom'] = value
8
+ doc
9
+ end
10
+ end
11
+
3
12
  describe InlineSvg::ActionView::Helpers do
4
13
 
5
14
  let(:helper) { ( Class.new { include InlineSvg::ActionView::Helpers } ).new }
@@ -79,6 +88,29 @@ SVG
79
88
  expect(helper.inline_svg('some-file', title: 'A title', desc: 'A description', nocomment: true)).to eq expected_output
80
89
  end
81
90
  end
91
+
92
+ context "with custom transformations" do
93
+ before(:each) do
94
+ InlineSvg.configure do |config|
95
+ config.add_custom_transformation({attribute: :custom, transform: WorkingCustomTransform})
96
+ end
97
+ end
98
+
99
+ after(:each) do
100
+ InlineSvg.reset_configuration!
101
+ end
102
+
103
+ it "applies custm transformations to the output" do
104
+ input_svg = <<-SVG
105
+ <svg></svg>
106
+ SVG
107
+ expected_output = <<-SVG
108
+ <svg custom="some value"></svg>
109
+ SVG
110
+ allow(InlineSvg::AssetFile).to receive(:named).with('some-file').and_return(input_svg)
111
+ expect(helper.inline_svg('some-file', custom: 'some value')).to eq expected_output
112
+ end
113
+ end
82
114
  end
83
115
  end
84
116
  end
@@ -1,5 +1,18 @@
1
1
  require_relative '../lib/inline_svg'
2
2
 
3
+ class MyCustomTransform
4
+ def self.create_with_value(value); end
5
+ def transform(doc); end
6
+ end
7
+
8
+ class MyInvalidCustomTransformKlass
9
+ def transform(doc); end
10
+ end
11
+
12
+ class MyInvalidCustomTransformInstance
13
+ def self.create_with_value(value); end
14
+ end
15
+
3
16
  describe InlineSvg do
4
17
  describe "configuration" do
5
18
  context "when a block is not given" do
@@ -28,5 +41,40 @@ describe InlineSvg do
28
41
  end.to raise_error(InlineSvg::Configuration::Invalid, /asset finder.*find_asset/i)
29
42
  end
30
43
  end
44
+
45
+ context "configuring custom transformation" do
46
+ it "allows a custom transformation to be added" do
47
+ InlineSvg.configure do |config|
48
+ config.add_custom_transformation(attribute: :my_transform, transform: MyCustomTransform)
49
+ end
50
+
51
+ expect(InlineSvg.configuration.custom_transformations).to eq({my_transform: MyCustomTransform})
52
+ end
53
+
54
+ it "rejects transformations that do not implement .create_with_value" do
55
+ expect do
56
+ InlineSvg.configure do |config|
57
+ config.add_custom_transformation(attribute: :irrelevant, transform: MyInvalidCustomTransformKlass)
58
+ end
59
+ end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformKlass} should implement the .create_with_value and #transform methods/)
60
+ end
61
+
62
+ it "rejects transformations that does not implement #transform" do
63
+ expect do
64
+ InlineSvg.configure do |config|
65
+ config.add_custom_transformation(attribute: :irrelevant, transform: MyInvalidCustomTransformInstance)
66
+ end
67
+ end.to raise_error(InlineSvg::Configuration::Invalid, /#{MyInvalidCustomTransformInstance} should implement the .create_with_value and #transform methods/)
68
+ end
69
+
70
+ it "rejects transformations that are not classes" do
71
+ expect do
72
+ InlineSvg.configure do |config|
73
+ config.add_custom_transformation(attribute: :irrelevant, transform: :not_a_class)
74
+ end
75
+ end.to raise_error(InlineSvg::Configuration::Invalid, /#{:not_a_class} should implement the .create_with_value and #transform methods/)
76
+ end
77
+
78
+ end
31
79
  end
32
80
  end
@@ -1,8 +1,15 @@
1
+ require 'inline_svg'
1
2
  require 'inline_svg/transform_pipeline'
2
3
 
4
+ class ACustomTransform < InlineSvg::CustomTransformation
5
+ def transform(doc)
6
+ doc
7
+ end
8
+ end
9
+
3
10
  describe InlineSvg::TransformPipeline::Transformations do
4
11
  context "looking up transformations" do
5
- it "returns configured transformations when parameters are supplied" do
12
+ it "returns built-in transformations when parameters are supplied" do
6
13
  transformations = InlineSvg::TransformPipeline::Transformations.lookup(
7
14
  nocomment: 'irrelevant',
8
15
  class: 'irrelevant',
@@ -48,4 +55,25 @@ describe InlineSvg::TransformPipeline::Transformations do
48
55
  expect(transformations.map(&:class)).to match_array([])
49
56
  end
50
57
  end
58
+
59
+ context "custom transformations" do
60
+ before(:each) do
61
+ InlineSvg.configure do |config|
62
+ config.add_custom_transformation({transform: ACustomTransform, attribute: :my_transform})
63
+ end
64
+ end
65
+
66
+ after(:each) do
67
+ InlineSvg.reset_configuration!
68
+ end
69
+
70
+ it "returns configured custom transformations" do
71
+ transformations = InlineSvg::TransformPipeline::Transformations.lookup(
72
+ my_transform: :irrelevant
73
+ )
74
+
75
+ expect(transformations.map(&:class)).to match_array([ACustomTransform])
76
+ end
77
+ end
78
+
51
79
  end
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: 0.5.3
4
+ version: 0.6.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: 2015-06-22 00:00:00.000000000 Z
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler