sass-json-vars 0.0.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64106623ea0e4f551ffd3fe0e2abb0452ae3f03a
4
- data.tar.gz: d94659199cde5cdf68cd31a3ff22a61d7bb882a5
3
+ metadata.gz: bee8311004adacb3fe1c3aaf810f585f74e0cd8b
4
+ data.tar.gz: 7fb0ca0f8a34da3ac0d8dd234f71f840fb9db7ec
5
5
  SHA512:
6
- metadata.gz: 89e4ba7d494b4255e2ce4281c694cbb2a48eda9a91ea6cd4683a5af802821847d182750554e9d87b77ba93f641cb33ed9ae2d0700476329d6e2535e760a709ec
7
- data.tar.gz: 04ac9cd7836a11c36d670a14b3097f730badb865c6a169b04c5e112a7d904ed7aa9519a74fbb6e19d17cad9ee288c4b54fdcd6220ff5c31887f05ed91f8b8258
6
+ metadata.gz: 1207db98b67ade60e3fb44990096d83f1529e1bc8262fb3098dbf357ef80d25a623b3acb4fef9020ce2d61d3a37483fb588bbd5f761ecb3e81d1bf3ca41f93c4
7
+ data.tar.gz: d6f3ca6e07666a05f9fcceeda7abacd706cea46085dc40e3583808338c71e04ee2047b8aad0d04b8d1e36129fc66546bb253e27980681ba6722a5e9f4d97a551
@@ -27,18 +27,23 @@ class SassJSONVars::Importer < Sass::Importers::Base
27
27
  other.class == self.class
28
28
  end
29
29
 
30
- def self.flat_hash(h, f=[], g={})
31
- return g.update({ f.join('-') => h }) unless h.is_a? Hash
32
- h.each { |k,r| flat_hash(r,f+[k],g) }
33
- g
30
+ def self.convert_to_sass(item)
31
+ if item.is_a? Array
32
+ '(' + item.map { |i| self.convert_to_sass(i) }.join(',') + ')'
33
+ elsif item.is_a? Hash
34
+ '(' + item.map {|key, value| key.to_s + ':' + self.convert_to_sass(value) }.join(',') + ')'
35
+ else
36
+ item
37
+ end
34
38
  end
35
39
 
36
- # Returns a Sass::Engine for this sprite object
37
40
  def self.sass_engine(uri, importer, options)
38
- flattened = self.flat_hash JSON.parse(IO.read(uri))
41
+ json = JSON.parse(IO.read(uri))
42
+
43
+ variables = json.map do |name, value|
44
+ output = self.convert_to_sass(value);
39
45
 
40
- variables = flattened.map do |name, value|
41
- "$#{name}: #{value}"
46
+ "$#{name}: #{output}"
42
47
  end
43
48
 
44
49
  Sass::Engine.new(variables.join("\n"))
@@ -1,3 +1,3 @@
1
1
  module SassJSONVars
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/readme.md CHANGED
@@ -1,15 +1,21 @@
1
1
  # Sass JSON Vars
2
2
 
3
- A work in progress.
3
+ `@import` json data into Sass `$variables`.
4
4
 
5
- ### Eventual Usage
5
+ ### Usage
6
+
7
+ ```shell
8
+ gem install sass-json-vars
9
+ ```
6
10
 
7
11
  Place variables in a JSON file:
8
12
 
9
13
  ```javascript
14
+ // variables.json
10
15
  {
11
- color: {
12
- red: "#c33"
16
+ "font-sans": "Helvetica, sans-serif",
17
+ "colors": {
18
+ "red": "#c33"
13
19
  }
14
20
  }
15
21
  ```
@@ -20,6 +26,13 @@ Import the file in Sass to expose variable names:
20
26
  @import "variables.json"
21
27
 
22
28
  body {
23
- color: $color-red;
29
+ color: map-get($colors, red);
30
+ font: $font-sans;
24
31
  }
25
32
  ```
33
+
34
+ Require sass-json-vars when compiling
35
+
36
+ ```shell
37
+ sass style.scss -r sass-json-vars
38
+ ```
@@ -0,0 +1,5 @@
1
+ @import 'test/fixtures/lists/variables.json';
2
+
3
+ body {
4
+ color: nth($colors, 1);
5
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "colors" : ["#c33", "#33c"]
3
+ }
@@ -0,0 +1,5 @@
1
+ @import 'test/fixtures/maps/variables.json';
2
+
3
+ body {
4
+ color: map-get($colors, red);
5
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "colors" : {
3
+ "red": "#c33"
4
+ }
5
+ }
@@ -0,0 +1,5 @@
1
+ @import 'test/fixtures/strings/variables.json';
2
+
3
+ body {
4
+ color: $color-red;
5
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "color-red" : "#c33",
3
+ "color-blue" : "#33c"
4
+ }
@@ -3,17 +3,43 @@ 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
6
30
 
7
- def test_can_flatten_hashes
8
- flattened = SassJSONVars::Importer.flat_hash({
9
- :color => { :red => "#c33" }
10
- })
11
- assert_match '#c33', flattened['color-red']
31
+ def test_strings
32
+ scss = Sass.compile_file("test/fixtures/strings/style.scss")
33
+ assert_equal "body {\n color: #cc3333; }\n", scss
12
34
  end
13
35
 
14
- def test_importability
15
- scss = Sass.compile_file("test/fixtures/full_path.scss")
36
+ def test_lists
37
+ scss = Sass.compile_file("test/fixtures/lists/style.scss")
16
38
  assert_equal "body {\n color: #cc3333; }\n", scss
17
39
  end
18
40
 
41
+ def test_maps
42
+ scss = Sass.compile_file("test/fixtures/maps/style.scss")
43
+ assert_equal "body {\n color: #cc3333; }\n", scss
44
+ end
19
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sass-json-vars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Hunzaker
@@ -36,8 +36,12 @@ files:
36
36
  - lib/sass-json-vars/importer.rb
37
37
  - lib/sass-json-vars/version.rb
38
38
  - readme.md
39
- - test/fixtures/full_path.scss
40
- - test/fixtures/variables.json
39
+ - test/fixtures/lists/style.scss
40
+ - test/fixtures/lists/variables.json
41
+ - test/fixtures/maps/style.scss
42
+ - test/fixtures/maps/variables.json
43
+ - test/fixtures/strings/style.scss
44
+ - test/fixtures/strings/variables.json
41
45
  - test/sass-json-vars_test.rb
42
46
  homepage: https://github.com/vigetlabs/sass-json-vars
43
47
  licenses:
@@ -64,6 +68,10 @@ signing_key:
64
68
  specification_version: 4
65
69
  summary: Allows the use of JSON to declare variables with @import 'file.json'.
66
70
  test_files:
67
- - test/fixtures/full_path.scss
68
- - test/fixtures/variables.json
71
+ - test/fixtures/lists/style.scss
72
+ - test/fixtures/lists/variables.json
73
+ - test/fixtures/maps/style.scss
74
+ - test/fixtures/maps/variables.json
75
+ - test/fixtures/strings/style.scss
76
+ - test/fixtures/strings/variables.json
69
77
  - test/sass-json-vars_test.rb
@@ -1,5 +0,0 @@
1
- @import 'test/fixtures/variables.json';
2
-
3
- body {
4
- color: $color-red;
5
- }
@@ -1,6 +0,0 @@
1
- {
2
- "color" : {
3
- "red" : "#c33",
4
- "blue" : "#33c"
5
- }
6
- }