class SassC::SassYaml

Public Class Methods

new(options={}) click to toggle source

Global vars beginning with underscore will have their children promoted to globals and will be assigned without the underscore

For example: _colors: { yellow: '#fco' }

becomes: colors: { yellow: '#fco'}, yellow: '#fco'
# File lib/spark_engine/sass/sass_yaml.rb, line 13
def initialize(options={})
  @content = options[:content]

  if options[:file] && File.exist?(options[:file])
    @content = File.open(options[:file], 'rb') { |f| f.read }
  end

  @data = promote_keys YAML.load(@content)
end

Public Instance Methods

convert_dollar_values() click to toggle source

Allow vars to reference other vars in their value with $ Example Input:¬

blue: 'blue'¬
green: 'green'
gradient: [$blue, $green]

Output:

blue: 'blue'¬
green: 'green'
gradient: ['blue', 'green']
# File lib/spark_engine/sass/sass_yaml.rb, line 63
def convert_dollar_values
  @content.gsub(/\$(?<var>\w+)/) {
    @data[$~[:var]].inspect
  }
end
convert_to_sass_value(item) click to toggle source

Convert

# File lib/spark_engine/sass/sass_yaml.rb, line 70
def convert_to_sass_value(item)
  if item.is_a? Array
    make_list(item)
  elsif item.is_a? Hash
    make_map(item)
  else
    item.to_s
  end
end
make_list(item) click to toggle source

Convert arrays to Sass list syntax

# File lib/spark_engine/sass/sass_yaml.rb, line 81
def make_list(item)
  '(' + item.map { |i| convert_to_sass_value(i) }.join(',') + ')'
end
make_map(item) click to toggle source

Convert hashes to Sass map syntax

# File lib/spark_engine/sass/sass_yaml.rb, line 86
def make_map(item)
  '(' + item.map {|key, value| key.to_s + ':' + convert_to_sass_value(value) }.join(',') + ')'
end
promote_keys( data ) click to toggle source

If underscore keys, copy children to top level vars too Input:

_colors:
  yellow: '#fco'

Output:

colors: { yellow: '#fco' }
yellow: '#fco'
# File lib/spark_engine/sass/sass_yaml.rb, line 44
def promote_keys( data )
  data.keys.select{|k| k.start_with?('_') }.each do |key|
    data[key.sub(/^_/,'')] = data[key]
    data = data.delete(key).merge(data)
  end

  data
end
to_sass() click to toggle source

Convert each key to $key and process each value to a Sass data structure (creating maps, lists, strings)

# File lib/spark_engine/sass/sass_yaml.rb, line 30
def to_sass
  @data.map { |key, value| 
    "$#{key}: #{convert_to_sass_value(value)};" 
  }.join("\n")
end
to_yaml() click to toggle source

Flatten dollar values and promote keys before returning YAML

# File lib/spark_engine/sass/sass_yaml.rb, line 24
def to_yaml
  promote_keys YAML.load(convert_dollar_values)
end