Class: Sprite::Styles::SassYmlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/sprite/styles/sass_yml_generator.rb

Overview

renders a yml file that is later parsed by a sass extension when generating the mixins

Instance Method Summary collapse

Constructor Details

#initialize(builder) ⇒ SassYmlGenerator

Returns a new instance of SassYmlGenerator.



6
7
8
# File 'lib/sprite/styles/sass_yml_generator.rb', line 6

def initialize(builder)
  @builder = builder
end

Instance Method Details

#extensionObject



48
49
50
# File 'lib/sprite/styles/sass_yml_generator.rb', line 48

def extension
  "yml"
end

#write(path, sprite_files) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sprite/styles/sass_yml_generator.rb', line 10

def write(path, sprite_files)
  # build the yml file
  write_config(path, sprite_files)

  # Where to put the sass mixin file
  sass_path = path.gsub(".yml", ".sass")

  # write the sass mixins to disk
  File.open(File.join(Sprite.root, sass_path), 'w') do |f|
    f.puts "!sprite_data = '#{path}'"
    f.puts ""
    f.puts "= sprite(!group_name, !image_name)"
    f.puts "  background= sprite_background(!group_name, !image_name)"
    f.puts "  width= sprite_width(!group_name, !image_name)"
    f.puts "  height= sprite_height(!group_name, !image_name)"
    f.puts ""
  end
end

#write_config(path, sprite_files) ⇒ Object

write the sprite configuration file (used by the yml extension)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sprite/styles/sass_yml_generator.rb', line 30

def write_config(path, sprite_files)
  # build a grouped hash with all the sprites in it
  result = {}
  sprite_files.each do |sprite_file, sprites|
    sprites.each do |sprite|
      if sprite[:group]
        result[sprite[:group]] ||= {}
        result[sprite[:group]][sprite[:name]] = sprite
      end
    end
  end

  # write the config yml to disk
  File.open(File.join(Sprite.root, path), 'w') do |f|
    YAML.dump(result, f)
  end
end