Class: Spriteful::Stylesheet

Inherits:
Object
  • Object
show all
Defined in:
lib/spriteful/stylesheet.rb

Overview

Public: class responsible for putting together the CSS code to use a specific sprite.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sprite, destination, options = {}) ⇒ Stylesheet

Public: Initialize a Stylesheet

sprite - a ‘Sprite’ object to create the Stylesheet. destination - the directory where the Stylesheet will be created. options - additional Hash of options.

:format - the Stylesheet format.
:mixin  - Use mixins instead of Placeholder selector in the SCSS format.
:rails  - A flag to generate Asset Pipeline compatible Stylesheets.


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/spriteful/stylesheet.rb', line 27

def initialize(sprite, destination, options = {})
  @sprite = sprite
  @options = options
  @destination = destination

  @format = options[:format]
  @rails = !!options[:rails]
  @template_path = options[:template] || self.class.expand_template_path(@format)

  @path = File.join(@destination, name)
end

Instance Attribute Details

#formatObject (readonly)

Public: returns the format used to render the Stylesheet.



8
9
10
# File 'lib/spriteful/stylesheet.rb', line 8

def format
  @format
end

#pathObject (readonly)

Public: returns the path where the Stylesheet should be stored.



14
15
16
# File 'lib/spriteful/stylesheet.rb', line 14

def path
  @path
end

#spriteObject (readonly)

Public: returns the ‘Sprite’ of this Stylesheet.



11
12
13
# File 'lib/spriteful/stylesheet.rb', line 11

def sprite
  @sprite
end

#template_pathObject (readonly)

Public: returns the custom template path.



17
18
19
# File 'lib/spriteful/stylesheet.rb', line 17

def template_path
  @template_path
end

Class Method Details

.expand_template_path(format) ⇒ Object

Internal: Expands the path to the default stylesheet for the given format.

Returns the path as a String.



69
70
71
# File 'lib/spriteful/stylesheet.rb', line 69

def self.expand_template_path(format)
  File.expand_path("../stylesheets/template.#{format}.erb", __FILE__)
end

.read_template(format) ⇒ Object

Internal: Reads the default template Stylesheet for the given format.

Returns a String.



61
62
63
64
# File 'lib/spriteful/stylesheet.rb', line 61

def self.read_template(format)
  path = expand_template_path(format)
  File.read(path)
end

Instance Method Details

#nameObject

Public: returns this Stylesheet name, based on the Sprite name and the current format.

Returns a String.



53
54
55
56
# File 'lib/spriteful/stylesheet.rb', line 53

def name
  extension = rails? ? rails_extension : format
  "#{sprite_name}.#{extension}"
end

#renderObject

Public: renders the CSS code for this Stylesheet. An ERB template will be used to process the code, based on the stylesheet format.

Returns the CSS code as a ‘String’.



44
45
46
47
# File 'lib/spriteful/stylesheet.rb', line 44

def render
  template = Template.new(@sprite, template_options)
  template.render(File.read(template_path))
end