Class: Adva::Static::Import::Source::Post

Inherits:
Base
  • Object
show all
Defined in:
lib/adva/static/import/source/post.rb

Constant Summary collapse

%r((?:^|/)(\d{4})(?:\-|\/)(\d{1,2})(?:\-|\/)(\d{1,2})(?:\-|\/)(.*)$)

Instance Attribute Summary

Attributes inherited from Base

#path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#<=>, #initialize, #model_name, #to_hash

Constructor Details

This class inherits a constructor from Adva::Static::Import::Source::Base

Class Method Details

.permalink?(path) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/adva/static/import/source/post.rb', line 18

def permalink?(path)
  path.to_s =~ PERMALINK
end

.recognize(paths) ⇒ Object



11
12
13
14
15
16
# File 'lib/adva/static/import/source/post.rb', line 11

def recognize(paths)
  posts = paths.map { |path| path.self_and_descendants }.flatten.select { |path| permalink?(path) }
  posts = posts.map { |path| new(path) }
  paths.replace(paths - posts.map(&:permalink_paths).flatten)
  posts
end

Instance Method Details

#categoriesObject



23
24
25
# File 'lib/adva/static/import/source/post.rb', line 23

def categories
  @categories ||= Array(read.categories).map { |category| category.split(',') }.flatten.map(&:strip)
end

#dataObject



51
52
53
# File 'lib/adva/static/import/source/post.rb', line 51

def data
  super.merge(:categories => categories, :title => title, :slug => slug, :published_at => published_at)
end


39
40
41
# File 'lib/adva/static/import/source/post.rb', line 39

def permalink
  @permalink ||= path_tokens.to_a[0..-2] << slug
end


43
44
45
# File 'lib/adva/static/import/source/post.rb', line 43

def permalink_paths
  path.self_and_parents - path.root.self_and_parents
end

#published_atObject



35
36
37
# File 'lib/adva/static/import/source/post.rb', line 35

def published_at
  @published_at ||= DateTime.civil(*permalink[0..-2].map(&:to_i))
end

#slugObject



31
32
33
# File 'lib/adva/static/import/source/post.rb', line 31

def slug
  @slug ||= read.slug || SimpleSlugs::Slug.new(title).to_s
end


47
48
49
# File 'lib/adva/static/import/source/post.rb', line 47

def strip_permalink
  path.gsub(Post::PERMALINK, '')
end

#titleObject



27
28
29
# File 'lib/adva/static/import/source/post.rb', line 27

def title
  @title ||= read.title || path_tokens.last.titleize
end