Class: Montage::SpriteDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/montage/sprite_definition.rb

Overview

Represents a pseudo-file system path, where a directory can be replaced with the :sprite named segment. :name will match any directory – all files with the same :name value will be placed into the same sprite file.

Where a sprite source defines a :name segment, no sprite name needs to be explicitly set in the .montage file.

For example, given the following directory structure:

/path/to/something/
  big/
    one.png
    two.png
  small/
    three.png
    four.png

… then segmented path “/path/to/something/:name/*” will match all four PNG files, with “one” and “two” being placed in the “big” sprite, “three” and “four” in the “small” sprite.

Instance Method Summary collapse

Constructor Details

#initialize(project, path, options = {}) ⇒ SpriteDefinition

Creates a new SpriteDefinition instance.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/montage/sprite_definition.rb', line 30

def initialize(project, path, options = {})
  @project = project
  @path    = project.paths.root + path

  # Symbolize option keys.
  @options = options.inject({}) do |opts, (key, value)|
    opts[key.to_sym] = value ; opts
  end

  @options[:to] = (@project.paths.root +
                  (@options[:to] || Project::DEFAULTS[:to])).to_s

  @options[:url]     ||= project.paths.url
  @options[:padding] ||= project.padding

  if has_name_segment? and @options.has_key?(:name)
    raise Montage::DuplicateName, <<-ERROR.compress_lines
      Sprite `#{path}' has both a :name path segment and a "name"
      option; please use only one.
    ERROR
  elsif not has_name_segment? and not @options.has_key?(:name)
    # Infer the sprite name from the filename.
    @options[:name] =
      File.basename(@options[:to], File.extname(@options[:to]))
  elsif has_name_segment? and not @options[:to] =~ /:name/
    raise Montage::MissingName, <<-ERROR.compress_lines
      Sprite `#{path}' requires :name in the "to" option.
    ERROR
  end
end

Instance Method Details

#to_spritesArray(Montage::Sprite)

Returns an array of Sprites defined.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/montage/sprite_definition.rb', line 65

def to_sprites
  matching_sources.map do |sprite_name, sources|
    save_path = Pathname.new(@options[:to].gsub(/:name/, sprite_name))

    url = @options[:url].dup # Since it may be the DEFAULT string
    url.gsub!(/:name/, sprite_name)
    url.gsub!(/:filename/, save_path.basename.to_s)

    Montage::Sprite.new(sprite_name, sources, save_path, @project,
      :url     => url,
      :padding => @options[:padding]
    )
  end
end