savagery 0.1.0 → 0.2.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: ee09c3c070917055eb7b817030e15f46f53d09a8
4
- data.tar.gz: 8d68da05a447d63420b5bfe84df9f15c05063a38
3
+ metadata.gz: af5e8dab154d57467a364bb5a2e6aa37d8b2f1f4
4
+ data.tar.gz: 253378ce924305f255292635211261ff4eaeb880
5
5
  SHA512:
6
- metadata.gz: 768b230c5568792fb1158e3c352149a26af9a07909a200a502d1f6310c10ceb21a8a59f9768a62b6aed0f51ead5ddb43e50f2b0fb95bc741f3216000718a0ede
7
- data.tar.gz: 31b6973438164b642ec7a724e3c3a687a6c3e19ee88c9d34ac12929f47d170a9d7af5b57e081e17321f865e873aed44aa012cdbda1d0f07c4193a16804a88e33
6
+ metadata.gz: 942fe2cc63dd8ad97578544be840c577dcb7b3e06f4a82a7b6a411e86e0ffe76f8c88b4920e377ed0f701fe18c73bcf7105eef8890b86cf44a0c447de9c4ad4c
7
+ data.tar.gz: fc59d5f61595b1cd5a40430a169e86b203ca01c0a643ddb959a93092e15c97a3c4db51804547b5eb2ae30e740fc2d5fa82f5332031d049a1b186772a7207d569
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Savagery
2
2
 
3
- Ruthlessly sprite SVGs in your Rails app.
3
+ Ruthlessly sprite SVGs in your Rails app. Includes Ember helpers, too!
4
4
 
5
5
  ## Installation
6
6
 
@@ -15,28 +15,36 @@ gem "savagery"
15
15
  Given the following directory structure:
16
16
 
17
17
  ```
18
- app/assets/images/
18
+ app/assets/svgs/
19
19
  └── dogs
20
20
     ├── chihuahua.svg
21
21
     ├── golden.svg
22
22
     └── lab.svg
23
23
  ```
24
24
 
25
- Use the `svg_sprite_use` helper in your view:
25
+ Rails: Use the `svg_sprite_use` helper in your view:
26
26
 
27
27
  ```ruby
28
28
  # app/views/dogs/show.html.erb
29
29
  <%= svg_sprite_use "dogs/chihuahua" %>
30
30
  ```
31
31
 
32
+ Ember: Require `savagery/ember` anywhere, then use the 'svg-sprite-use` helper in your template:
33
+
34
+ ```javascript
35
+ // app/assets/javascripts/templates/application.hbs
36
+ //= require savagery/ember
37
+ {{svg-sprite-use "dogs/chihuahua"}}
38
+ ```
39
+
32
40
  And Savagery will do all the hard work of spriting and embedding. A new .svg
33
- sprite file will appear in the images directory, named after the directory
41
+ sprite file will appear in the `app/assets/svgs` directory, named after the directory
34
42
  containing the sprites:
35
43
 
36
44
  ```
37
- app/assets/images/
45
+ app/assets/svgs/
38
46
  ├── dogs
39
- │   ├── chilhuahua.svg
47
+ │   ├── chihuahua.svg
40
48
  │   ├── golden.svg
41
49
  │   └── lab.svg
42
50
  └── dogs.svg
@@ -44,10 +52,6 @@ app/assets/images/
44
52
 
45
53
  Make sure you check this file into version control.
46
54
 
47
- ## TODO
48
-
49
- * add handlebars helpers for ember apps
50
-
51
55
  ## Contributing
52
56
 
53
57
  1. Fork it ( https://github.com/[my-github-username]/savagery/fork )
@@ -0,0 +1,29 @@
1
+ Savagery = {
2
+ svgSpriteInclude: function(name) {
3
+ if(this._svgSpritesIncluded[name]) { return; }
4
+ this._svgSpritesIncluded[name] = true
5
+
6
+ $.get("/assets/" + name + ".svg", function(data) {
7
+ $("body").prepend(data);
8
+ }, "text");
9
+ },
10
+
11
+ svgSpriteUse: function(basename, cssClass) {
12
+ cssClass = cssClass || basename;
13
+ return '<svg class="' + cssClass + '"><use xlink:href="#' + basename + '"></use></svg>';
14
+ },
15
+
16
+ _svgSpritesIncluded: {}
17
+ };
18
+
19
+ Ember.Handlebars.helper("svg-sprite-use", function(name, options) {
20
+ var names = name.split("/");
21
+ var dirname = names[0];
22
+ var basename = names[1];
23
+ Savagery.svgSpriteInclude(dirname);
24
+
25
+ var cssClass = options.hash["class"];
26
+ var html = Savagery.svgSpriteUse(basename, cssClass);
27
+ return new Ember.Handlebars.SafeString(html);
28
+ });
29
+
@@ -1,10 +1,16 @@
1
1
  require "rails"
2
2
  require "savagery/helpers"
3
+ require "savagery/middleware"
3
4
 
4
5
  module Savagery
5
6
  class Engine < Rails::Engine
6
- initializer "include helpers" do |app|
7
+ initializer "savagery.helpers" do |app|
7
8
  ApplicationController.helper(Helpers)
8
9
  end
10
+
11
+ initializer "savagery.middleware" do |app|
12
+ app.config.middleware.use Middleware
13
+ end
9
14
  end
10
15
  end
16
+
@@ -4,9 +4,10 @@ module Savagery
4
4
  module Helpers
5
5
  def svg_sprite_include path
6
6
  return raw("") if svg_sprites_included.include?(path)
7
- sprite_file_path = Savagery.sprite!("app/assets/images/#{path}")
8
7
  svg_sprites_included.add path
9
- raw File.read(sprite_file_path)
8
+
9
+ sprite_file = "app/assets/svgs/#{path}.svg"
10
+ raw File.read(sprite_file)
10
11
  end
11
12
 
12
13
  def svg_sprite_use name, options={}
@@ -0,0 +1,18 @@
1
+ module Savagery
2
+ class Middleware < Struct.new(:app)
3
+ def call *args
4
+ sprite!
5
+ app.call *args
6
+ end
7
+
8
+ private
9
+
10
+ def sprite!
11
+ paths = Dir.glob("app/assets/svgs/*/").map(&:chop)
12
+ paths.each do |path|
13
+ Spriter.new(path).sprite!
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -10,7 +10,6 @@ module Savagery
10
10
 
11
11
  def sprite!
12
12
  File.write sprite_path, sprite
13
- sprite_path
14
13
  end
15
14
 
16
15
  private
@@ -20,7 +19,7 @@ module Savagery
20
19
  end
21
20
 
22
21
  def sprite
23
- "<svg><defs>\n\n#{symbols.join("\n")}\n</defs></svg>\n"
22
+ %(<svg style="display: none"><defs>\n\n#{symbols.join("\n")}\n</defs></svg>\n)
24
23
  end
25
24
 
26
25
  def symbols
@@ -1,3 +1,3 @@
1
1
  module Savagery
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,4 +1,4 @@
1
- <svg><defs>
1
+ <svg style="display: none"><defs>
2
2
 
3
3
  <symbol id="callout1">
4
4
  <g opacity="0.6">
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: savagery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-12 00:00:00.000000000 Z
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -78,9 +78,11 @@ files:
78
78
  - LICENSE.txt
79
79
  - README.md
80
80
  - Rakefile
81
+ - app/assets/javascripts/savagery/ember.js
81
82
  - lib/savagery.rb
82
83
  - lib/savagery/engine.rb
83
84
  - lib/savagery/helpers.rb
85
+ - lib/savagery/middleware.rb
84
86
  - lib/savagery/spriter.rb
85
87
  - lib/savagery/spriter/image.rb
86
88
  - lib/savagery/version.rb