Class: Montage::Project

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

Overview

Represents a directory in which it is expected that there be a configuration file, and source images.

Defined Under Namespace

Classes: Paths

Constant Summary collapse

DEFAULTS =
{
  :sources => 'public/images/sprites/src',
  :sprites => 'public/images/sprites',
  :sass    => 'public/stylesheets/sass',
  :to      => "public/images/:name.png",
  :url     => "/images/:name.png",
  :padding => 20
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ Project

Creates a new Project instance.

Note that new does no validation of the given paths: it expects them to be correct. If you’re not sure of the exact paths, use Project.find instead.

Parameters:

  • config_path (String, Pathname)

    Path to the config file.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/montage/project.rb', line 45

def initialize(config_path)
  config_path = Pathname.new(config_path)
  config      = YAML.load_file(config_path)
  root_path   = determine_project_root(config_path, config)

  # Sass path may be a string representing a path, or `false`.
  sass_path = config.delete("config.sass") { DEFAULTS[:sass] }
  sass_path = sass_path.is_a?(String) ? root_path + sass_path : sass_path

  @paths = Paths.new(
    root_path, config_path, sass_path,
    config.delete('config.url') { DEFAULTS[:url] }
  )

  @padding = (config.delete('config.padding') || 20).to_i

  # All remaining config keys are sprite defintions.
  @sprites = config.map do |path, opts|
    Montage::SpriteDefinition.new(self, path, opts).to_sprites
  end.flatten
end

Instance Attribute Details

#paddingInteger (readonly)

Returns the amount of space to be used between each source image when saving sprites.

Returns:

  • (Integer)


34
35
36
# File 'lib/montage/project.rb', line 34

def padding
  @padding
end

#pathsMontage::Project::Paths (readonly)

Returns the Paths instance for the project.



21
22
23
# File 'lib/montage/project.rb', line 21

def paths
  @paths
end

#spritesArray<Montage::Sprite> (readonly)

Returns an Array containing all the Sprites defined in the project.

Returns:



27
28
29
# File 'lib/montage/project.rb', line 27

def sprites
  @sprites
end

Class Method Details

.find(path) ⇒ Montage::Project

Given a path to a directory, or config file, attempts to find the Montage root.

If given a path to a file:

* Montage assumes that the file is the configuration.

* The root directory is assumed to be the same directory as the one
  in which the configuration resides _unless_ the directory is
  called 'config', in which case the root is considered to be the
  parent directory.

If given a path to a directory:

* Montage will look for a .montage file.

* If a configuration couldn't be found, +find+ looks in the next
  directory up. It continues until it finds a valid project or runs
  out of parent directories.

Parameters:

  • path (String, Pathname)

    Path to the configuration or directory.

Returns:

Raises:

  • (MissingProject)

    Raised when a project directory couldn’t be found.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/montage/project.rb', line 153

def find(path)
  path = Pathname(path).expand_path
  config_path, root_path = nil, nil

  if path.file?
    config_path = path
  elsif path.directory?
    path.ascend do |directory|
      break if config_path = contains_config?(directory)
    end
  end

  raise MissingProject, "Montage couldn't find a project to work " \
    "on at `#{path}'" unless config_path

  new(config_path)
end

Instance Method Details

#sprite(name) ⇒ Montage::Sprite

Returns a particular sprite identified by name.

Parameters:

  • name (String)

    The name of the sprite to be retrieved.

Returns:



74
75
76
# File 'lib/montage/project.rb', line 74

def sprite(name)
  sprites.detect { |sprite| sprite.name == name }
end