Sha256: e4b53a315a1ce341e0d328ef8a92d063f81fb581f48f557ddfa6e0b0884c318c

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

require 'yaml'
module Sprite
  module Styles
    # renders a yml file that is later parsed by a sass extension when generating the mixins
    class SassYmlGenerator
      def initialize(builder)
        @builder = builder
      end

      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 "@mixin 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)"
        end
      end

      # write the sprite configuration file (used by the yml extension)
      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

      def extension
        "yml"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
amikula-sprite-0.1.13 lib/sprite/styles/sass_yml_generator.rb
amikula-sprite-0.1.12 lib/sprite/styles/sass_yml_generator.rb