Class: Sprite::Styles::StylusGenerator

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

Overview

renders a yml file that is later used by stylus when generating mixins

Instance Method Summary collapse

Constructor Details

#initialize(builder) ⇒ StylusGenerator

Returns a new instance of StylusGenerator.



5
6
7
# File 'lib/sprite/styles/stylus_generator.rb', line 5

def initialize(builder)
  @builder = builder
end

Instance Method Details

#extensionObject



38
39
40
# File 'lib/sprite/styles/stylus_generator.rb', line 38

def extension
  "styl"
end

#write(path, sprite_files) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sprite/styles/stylus_generator.rb', line 9

def write(path, sprite_files)
  # write the stylus mixins to disk
  File.open(File.join(Sprite.root, path), 'w') do |f|
    add_else = false

    f.puts "sprite(group_name, image_name, offset = 0)"
    sprite_files.each do |sprite_file, sprites|
      background_url = @builder.background_url(sprite_file)
      sprites.each do |sprite|
        f << "  "
        if add_else
          f << "else "
        end
        add_else = true
        if sprite[:align] == 'horizontal'
          background_offset = "#{sprite[:x]}px+offset #{sprite[:y]}px"
        else
          background_offset = "#{sprite[:x]}px #{sprite[:y]}px+offset"
        end

        f.puts %{if group_name == "#{sprite[:group]}" and image_name == "#{sprite[:name]}"}
        f.puts "    background: #{background_url} no-repeat #{background_offset}"
        f.puts "    width: #{sprite[:width]}px"
        f.puts "    height: #{sprite[:height]}px"
      end
    end
  end
end