Class: Broadway::Slug

Inherits:
Object
  • Object
show all
Defined in:
lib/broadway/resources/slug.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slug, resource) ⇒ Slug

Returns a new instance of Slug.



18
19
20
21
# File 'lib/broadway/resources/slug.rb', line 18

def initialize(slug, resource)
  self.value     = slug
  self.resource  = resource
end

Class Attribute Details

.stylesObject

Returns the value of attribute styles.



6
7
8
# File 'lib/broadway/resources/slug.rb', line 6

def styles
  @styles
end

Instance Attribute Details

#resourceObject

Returns the value of attribute resource.



3
4
5
# File 'lib/broadway/resources/slug.rb', line 3

def resource
  @resource
end

#scopeObject

Returns the value of attribute scope.



3
4
5
# File 'lib/broadway/resources/slug.rb', line 3

def scope
  @scope
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/broadway/resources/slug.rb', line 3

def value
  @value
end

Class Method Details

.default_stylesObject



8
9
10
11
12
13
14
15
# File 'lib/broadway/resources/slug.rb', line 8

def default_styles
  {
    :category => "/:categories/:slug",
    :date => "/:categories/:slug.:format",
    :category_and_date => "/:categories/:year/:month/:day/:slug",
    :page => "/:categories/:slug"
  }
end

Instance Method Details

#pathObject



44
45
46
47
48
49
50
# File 'lib/broadway/resources/slug.rb', line 44

def path
  @path ||= permalink
  @path ||= resource.sluggify.inject(template) do |result, token|
    result.gsub(/:#{token.first}/, token.last.nested_parameterize)
  end.gsub(/#{site.posts_path}/, "").squeeze("/").gsub(/\/$/, "")
  @path
end


23
24
25
# File 'lib/broadway/resources/slug.rb', line 23

def permalink
  resource.permalink
end

#siteObject



27
28
29
# File 'lib/broadway/resources/slug.rb', line 27

def site
  resource.site
end

#templateObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/broadway/resources/slug.rb', line 31

def template
  case self.site.permalink_style
  when :pretty
    "/:categories/:year/:month/:day/:title"
  when :none
    "/:categories/:title.html"
  when :date
    "/:categories/:year/:month/:day/:title.html"
  else
    self.site.permalink_style.to_s
  end
end

#titleizeObject



60
61
62
# File 'lib/broadway/resources/slug.rb', line 60

def titleize
  self.value.split('-').select { |w| w.capitalize! || w }.join(' ')
end

#to_sObject



64
65
66
# File 'lib/broadway/resources/slug.rb', line 64

def to_s
  path
end

#urlObject



52
53
54
# File 'lib/broadway/resources/slug.rb', line 52

def url
  ::File.join(site.url, path)
end