Class: Adrift::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/adrift/pattern.rb

Overview

Provides a way for Attachment to generally define its paths and urls, allowing to specialize them for every individual instance.

In order to do this, a Pattern is build from a String comprised of Tags (or more precisely, their labels) and when is asked to be specialized for a given Attachment and style, it replaces these Tags with they specialized values for that Attachment and that style.

Defined Under Namespace

Modules: Tags

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Pattern

Creates a new Pattern from a string comprised of one o more tag’s labels.



194
195
196
# File 'lib/adrift/pattern.rb', line 194

def initialize(string)
  @string = string.dup
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



190
191
192
# File 'lib/adrift/pattern.rb', line 190

def string
  @string
end

Class Method Details

.tagsObject

Tags every instance of Pattern will be able to recognize (and specialize).



186
187
188
# File 'lib/adrift/pattern.rb', line 186

def self.tags
  @tags ||= []
end

Instance Method Details

#specialize(options = {}) ⇒ Object

Returns #string with the known Tags replaced with their specific values the given options. While options is just a Hash, it’s expected to include the Attachment this Pattern belongs to (:attachment key), and the selected style (:style key).



202
203
204
205
206
# File 'lib/adrift/pattern.rb', line 202

def specialize(options={})
  sorted_tags.inject(string) do |result, tag|
    result.gsub(tag.label) { tag.specialize(options) }
  end
end