Class: Spriteful::Template
- Inherits:
-
Object
- Object
- Spriteful::Template
- Defined in:
- lib/spriteful/template.rb
Overview
Public: The Template class is the public API available in the ERB templates that will render a CSS/SCSS stylesheet for a sprite.
Instance Attribute Summary collapse
-
#cli_options ⇒ Object
readonly
Public: Gets the command line options used to create the current sprite.
-
#sprite ⇒ Object
readonly
Public: Gets the sprite object that is being rendered with this template.
Instance Method Summary collapse
-
#class_name_for(object) ⇒ Object
Public: sanitizes the ‘name’ of a given object to be used as a CSS selector class.
-
#data_uri(image) ⇒ Object
Public: Gets an embeddable Data URI of the image if it is a SVG image.
-
#extension_prefix ⇒ Object
Public: Gets the extension prefix for the SCSS selector, based on the ‘:mixin’ option.
-
#extension_strategy ⇒ Object
Internal: Gets the extension strategy for the SCSS selector, based on the ‘:mixin’ option.
-
#image_url(sprite) ⇒ Object
Public: computes a relative path between the sprite path and the stylesheet expected location.
-
#initialize(sprite, options = {}) ⇒ Template
constructor
Public: Initializes a Template object.
-
#mixin? ⇒ Boolean
Public: Gets the ‘:mixin’ flag.
-
#rails? ⇒ Boolean
Public: Gets the ‘:rails’ flag.
-
#render(source) ⇒ Object
Public: Renders the given source ERB template string in the context of the template object.
Constructor Details
#initialize(sprite, options = {}) ⇒ Template
Public: Initializes a Template object.
sprite - A instance of the Spriteful::Sprite object associated to this
template.
options - The Hash options that configure this template (default: {}):
:mixin - A flag marking if the template should generate SCSS
mixins instead of placeholders.
:rails - A flag marking if the template is being generated in a
Rails app.
:root - Root folder from where the generate stylesheets will be
served.
:destination - Folder where styleshee will live.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/spriteful/template.rb', line 26 def initialize(sprite, = {}) @sprite = sprite @options = @destination = Pathname.new([:destination]) @cli_options = Array([:cli_options]) if @options[:root] @root = Pathname.new(File.([:root])) else @root = nil end end |
Instance Attribute Details
#cli_options ⇒ Object (readonly)
Public: Gets the command line options used to create the current sprite.
12 13 14 |
# File 'lib/spriteful/template.rb', line 12 def @cli_options end |
#sprite ⇒ Object (readonly)
Public: Gets the sprite object that is being rendered with this template.
9 10 11 |
# File 'lib/spriteful/template.rb', line 9 def sprite @sprite end |
Instance Method Details
#class_name_for(object) ⇒ Object
Public: sanitizes the ‘name’ of a given object to be used as a CSS selector class.
object - A Spriteful::Sprite or Spriteful::Image instance.
Returns a String.
45 46 47 |
# File 'lib/spriteful/template.rb', line 45 def class_name_for(object) object.name.split('.').first.downcase.tr('_', '-') end |
#data_uri(image) ⇒ Object
Public: Gets an embeddable Data URI of the image if it is a SVG image.
image - A Spriteful::Image instance.
Returns a String.
97 98 99 100 101 |
# File 'lib/spriteful/template.rb', line 97 def data_uri(image) if image.svg? "'data:image/svg+xml;base64,#{Base64.encode64(image.blob).gsub(/\r?\n/, '')}'" end end |
#extension_prefix ⇒ Object
Public: Gets the extension prefix for the SCSS selector, based on the ‘:mixin’ option.
Returns a String.
53 54 55 |
# File 'lib/spriteful/template.rb', line 53 def extension_prefix mixin? ? '@mixin ' : '%' end |
#extension_strategy ⇒ Object
Internal: Gets the extension strategy for the SCSS selector, based on the ‘:mixin’ option.
Returns a String.
61 62 63 |
# File 'lib/spriteful/template.rb', line 61 def extension_strategy mixin? ? '@include ' : '@extend %' end |
#image_url(sprite) ⇒ Object
Public: computes a relative path between the sprite path and the stylesheet expected location.
sprite - A Spriteful::Sprite instance.
Returns a String.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/spriteful/template.rb', line 81 def image_url(sprite) path = Pathname.new(sprite.path) if rails? "sprites/#{sprite.filename}" elsif @root "/#{path.relative_path_from(@root)}" else path.relative_path_from(@destination).to_s end end |
#mixin? ⇒ Boolean
Public: Gets the ‘:mixin’ flag.
66 67 68 |
# File 'lib/spriteful/template.rb', line 66 def mixin? !!@options[:mixin] end |
#rails? ⇒ Boolean
Public: Gets the ‘:rails’ flag.
71 72 73 |
# File 'lib/spriteful/template.rb', line 71 def rails? !!@options[:rails] end |
#render(source) ⇒ Object
Public: Renders the given source ERB template string in the context of the template object.
source - A String containing the ERB template.
Returns a String.
109 110 111 |
# File 'lib/spriteful/template.rb', line 109 def render(source) ERB.new(source, nil, '-').result(binding) end |