sass-json-vars 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: bee8311004adacb3fe1c3aaf810f585f74e0cd8b
4
- data.tar.gz: 7fb0ca0f8a34da3ac0d8dd234f71f840fb9db7ec
3
+ metadata.gz: d4ce9ac9d7f27e142c4220cb100d7e2b14c6ea8a
4
+ data.tar.gz: 2dfca0a31019e3e0850c6e6907d33aef2e73febe
5
5
  SHA512:
6
- metadata.gz: 1207db98b67ade60e3fb44990096d83f1529e1bc8262fb3098dbf357ef80d25a623b3acb4fef9020ce2d61d3a37483fb588bbd5f761ecb3e81d1bf3ca41f93c4
7
- data.tar.gz: d6f3ca6e07666a05f9fcceeda7abacd706cea46085dc40e3583808338c71e04ee2047b8aad0d04b8d1e36129fc66546bb253e27980681ba6722a5e9f4d97a551
6
+ metadata.gz: 9bf0243a85026b1f841e25a2bcae4fd029e6989c300076e889a31a00b55be05ccab63cbcaed3f9e2b680546d98bcd036fa8f1d49f79be503bd80a4025399dd2d
7
+ data.tar.gz: 7499e303158c6f7f9497cccaf25359121a9e011f3bf26ebdb47f6090d50e341c8180bd983d2d1d53fd875553b3703b907b7bfec35cfee70213e67faa69629d7a
@@ -1,5 +1,4 @@
1
1
  module SassJSONVars; end
2
2
 
3
3
  require 'sass-json-vars/importer';
4
-
5
- Sass.load_paths << SassJSONVars::Importer.new()
4
+ require 'sass-json-vars/monkeypatch';
@@ -1,51 +1,64 @@
1
1
  require 'sass'
2
2
  require 'json'
3
3
 
4
- class SassJSONVars::Importer < Sass::Importers::Base
4
+ class SassJSONVars::Importer < Sass::Importers::Filesystem
5
5
 
6
- def find(uri, options)
7
- if File.extname(uri) == '.json'
8
- self.class.sass_engine(uri, self, options)
9
- else
10
- nil
11
- end
6
+ def watched_file?(uri)
7
+ filename =~ /\.json$/ && filename.start_with?(root + File::SEPARATOR)
12
8
  end
13
9
 
14
- def find_relative(uri, base, options)
15
- nil
10
+ def extensions
11
+ {'json' => :scss}
16
12
  end
17
13
 
18
- def to_s
19
- self.class.name
14
+ def find(name, options)
15
+ File.extname(name) == '.json' ? super(name, options) : nil
20
16
  end
21
17
 
22
- def hash
23
- self.class.name.hash
18
+ def find_relative(name, base, options)
19
+ File.extname(name) == '.json' ? super(name, base, options) : nil
24
20
  end
25
21
 
26
- def eql?(other)
27
- other.class == self.class
22
+ def sass_engine(uri, options)
23
+ json = JSON.parse(IO.read(uri))
24
+
25
+ variables = json.map do |name, value|
26
+ output = _convert_to_sass(value);
27
+ "$#{name}: #{output}"
28
+ end
29
+
30
+ Sass::Engine.new(variables.join("\n"))
28
31
  end
29
32
 
30
- def self.convert_to_sass(item)
33
+ private
34
+
35
+ def _find(dir, name, options)
36
+ full_filename, syntax = Sass::Util.destructure(find_real_file(dir, name, options))
37
+ return unless full_filename && File.readable?(full_filename)
38
+
39
+ options[:syntax] = syntax
40
+ options[:filename] = full_filename
41
+ options[:importer] = self
42
+
43
+ sass_engine(full_filename, options)
44
+ end
45
+
46
+ def _convert_to_sass(item)
31
47
  if item.is_a? Array
32
- '(' + item.map { |i| self.convert_to_sass(i) }.join(',') + ')'
48
+ _make_list(item)
33
49
  elsif item.is_a? Hash
34
- '(' + item.map {|key, value| key.to_s + ':' + self.convert_to_sass(value) }.join(',') + ')'
50
+ _make_map(item)
35
51
  else
36
52
  item
37
53
  end
38
54
  end
39
55
 
40
- def self.sass_engine(uri, importer, options)
41
- json = JSON.parse(IO.read(uri))
42
-
43
- variables = json.map do |name, value|
44
- output = self.convert_to_sass(value);
45
-
46
- "$#{name}: #{output}"
47
- end
56
+ def _make_list(item)
57
+ '(' + item.map { |i| _convert_to_sass(i) }.join(',') + ')'
58
+ end
48
59
 
49
- Sass::Engine.new(variables.join("\n"))
60
+ def _make_map(item)
61
+ '(' + item.map {|key, value| key.to_s + ':' + _convert_to_sass(value) }.join(',') + ')'
50
62
  end
63
+
51
64
  end
@@ -0,0 +1,19 @@
1
+ require 'sass-json-vars/importer'
2
+
3
+ # Taken from https://github.com/chriseppstein/sass-css-importer/blob/master/lib/sass/css_importer/monkey_patches.rb
4
+ # TODO: This feels wrong, surely there must be a better way to handle this
5
+
6
+ class Sass::Engine
7
+ alias initialize_without_json_importer initialize
8
+
9
+ def initialize(template, options={})
10
+ initialize_without_json_importer(template, options)
11
+
12
+ json_importer = self.options[:load_paths].find {|lp| lp.is_a?(SassJSONVars::Importer) }
13
+
14
+ unless json_importer
15
+ root = File.dirname(options[:filename] || ".")
16
+ self.options[:load_paths] << SassJSONVars::Importer.new(root)
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module SassJSONVars
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/readme.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Sass JSON Vars
2
2
 
3
+ [![Build Status](https://travis-ci.org/vigetlabs/sass-json-vars.png?branch=master)](https://travis-ci.org/vigetlabs/sass-json-vars) [![Gem Version](https://badge.fury.io/rb/sass-json-vars.png)](http://badge.fury.io/rb/sass-json-vars)
4
+
3
5
  `@import` json data into Sass `$variables`.
4
6
 
5
7
  ### Usage
@@ -1,4 +1,4 @@
1
- @import 'test/fixtures/lists/variables.json';
1
+ @import 'variables.json';
2
2
 
3
3
  body {
4
4
  color: nth($colors, 1);
@@ -1,5 +1,5 @@
1
- @import 'test/fixtures/maps/variables.json';
1
+ @import 'variables.json';
2
2
 
3
3
  body {
4
- color: map-get($colors, red);
4
+ color: map-get($colors, red);
5
5
  }
@@ -0,0 +1,4 @@
1
+ @import "variables.json"
2
+
3
+ body
4
+ color: $color-red
@@ -0,0 +1,4 @@
1
+ {
2
+ "color-red" : "#c33",
3
+ "color-blue" : "#33c"
4
+ }
@@ -1,4 +1,4 @@
1
- @import 'test/fixtures/strings/variables.json';
1
+ @import 'variables.json';
2
2
 
3
3
  body {
4
4
  color: $color-red;
@@ -3,32 +3,7 @@ require 'sass'
3
3
  require 'sass-json-vars'
4
4
 
5
5
  class SassJSONVarsTest < Test::Unit::TestCase
6
- def test_can_convert_arrays_to_lists
7
- flattened = SassJSONVars::Importer.convert_to_sass(['#c33', '#33c'])
8
- assert_equal '(#c33,#33c)', flattened
9
- end
10
-
11
- def test_can_convert_nested_arrays_to_lists
12
- flattened = SassJSONVars::Importer.convert_to_sass([['#c33', 'red'], '#33c'])
13
- assert_equal '((#c33,red),#33c)', flattened
14
- end
15
-
16
- def test_can_convert_hashes_to_maps
17
- flattened = SassJSONVars::Importer.convert_to_sass({ red: '#c33' })
18
- assert_equal '(red:#c33)', flattened
19
- end
20
-
21
- def test_can_convert_nested_hashes_to_maps
22
- flattened = SassJSONVars::Importer.convert_to_sass({ colors: { red: '#c33' } })
23
- assert_equal '(colors:(red:#c33))', flattened
24
- end
25
-
26
- def test_can_convert_complicated_objects
27
- flattened = SassJSONVars::Importer.convert_to_sass({ colors: ['red', 'blue', 'green'] })
28
- assert_equal '(colors:(red,blue,green))', flattened
29
- end
30
-
31
- def test_strings
6
+ def test_strings
32
7
  scss = Sass.compile_file("test/fixtures/strings/style.scss")
33
8
  assert_equal "body {\n color: #cc3333; }\n", scss
34
9
  end
data/test/sass_test.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ require 'sass'
3
+ require 'sass-json-vars'
4
+
5
+ class SassTest < Test::Unit::TestCase
6
+ def test_sass_syntax
7
+ scss = Sass.compile_file("test/fixtures/sass/style.sass")
8
+ assert_equal "body {\n color: #cc3333; }\n", scss
9
+ end
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sass-json-vars
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
  - Nate Hunzaker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-08 00:00:00.000000000 Z
11
+ date: 2014-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sass
@@ -34,15 +34,19 @@ files:
34
34
  - Rakefile
35
35
  - lib/sass-json-vars.rb
36
36
  - lib/sass-json-vars/importer.rb
37
+ - lib/sass-json-vars/monkeypatch.rb
37
38
  - lib/sass-json-vars/version.rb
38
39
  - readme.md
39
40
  - test/fixtures/lists/style.scss
40
41
  - test/fixtures/lists/variables.json
41
42
  - test/fixtures/maps/style.scss
42
43
  - test/fixtures/maps/variables.json
44
+ - test/fixtures/sass/style.sass
45
+ - test/fixtures/sass/variables.json
43
46
  - test/fixtures/strings/style.scss
44
47
  - test/fixtures/strings/variables.json
45
48
  - test/sass-json-vars_test.rb
49
+ - test/sass_test.rb
46
50
  homepage: https://github.com/vigetlabs/sass-json-vars
47
51
  licenses:
48
52
  - MIT
@@ -72,6 +76,9 @@ test_files:
72
76
  - test/fixtures/lists/variables.json
73
77
  - test/fixtures/maps/style.scss
74
78
  - test/fixtures/maps/variables.json
79
+ - test/fixtures/sass/style.sass
80
+ - test/fixtures/sass/variables.json
75
81
  - test/fixtures/strings/style.scss
76
82
  - test/fixtures/strings/variables.json
77
83
  - test/sass-json-vars_test.rb
84
+ - test/sass_test.rb