Class: Blogue::Post

Inherits:
Object
  • Object
show all
Defined in:
app/models/blogue/post.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Post

Returns a new instance of Post.



51
52
53
# File 'app/models/blogue/post.rb', line 51

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



49
50
51
# File 'app/models/blogue/post.rb', line 49

def path
  @path
end

Class Method Details

.allObject



8
9
10
11
12
13
14
# File 'app/models/blogue/post.rb', line 8

def all
  posts = all_post_paths.
    map(&method(:new)).
    sort(&method(:sort_posts))

  Rails.env.production? ? posts.reject(&:private?) : posts
end

.all_paths_in_posts_dirObject



20
21
22
# File 'app/models/blogue/post.rb', line 20

def all_paths_in_posts_dir
  Dir["#{Blogue.posts_path}/*"]
end

.all_post_pathsObject



16
17
18
# File 'app/models/blogue/post.rb', line 16

def all_post_paths
  all_paths_in_posts_dir.select(&method(:post_file?))
end

.cache_keyObject



44
45
46
# File 'app/models/blogue/post.rb', line 44

def cache_key
  "blogue/#{Blogue.blanket_checksum}"
end

.extract_post_id(path) ⇒ Object



28
29
30
# File 'app/models/blogue/post.rb', line 28

def extract_post_id(path)
  File.basename(path)[/[^\.]+/]
end

.find(id) ⇒ Object



4
5
6
# File 'app/models/blogue/post.rb', line 4

def find(id)
  all.find{ |p| p.id == id }
end

.load_meta(text) ⇒ Object



36
37
38
39
40
41
42
# File 'app/models/blogue/post.rb', line 36

def load_meta(text)
  YAML.load(
    text.lines.select do |line|
      !!(line[/^<!--\s*meta/]..line[/--!>$/])
    end[1..-2].try(:join, "\n") || ''
  ) || {}
end

.post_file?(path) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/models/blogue/post.rb', line 24

def post_file?(path)
  File.file?(path) && !File.basename(path).starts_with?('.')
end

.sort_posts(post_a, post_b) ⇒ Object



32
33
34
# File 'app/models/blogue/post.rb', line 32

def sort_posts(post_a, post_b)
  post_b.time <=> post_a.time
end

Instance Method Details

#author_nameObject



79
80
81
# File 'app/models/blogue/post.rb', line 79

def author_name
  meta_author_name || config_author_name || whoami_author_name
end

#bodyObject



71
72
73
# File 'app/models/blogue/post.rb', line 71

def body
  File.read(path)
end

#cache_keyObject



91
92
93
# File 'app/models/blogue/post.rb', line 91

def cache_key
  "blogue/posts/#{id}/#{Blogue.checksums[id]}"
end

#dateObject



59
60
61
# File 'app/models/blogue/post.rb', line 59

def date
  time.try(:to_date)
end

#idObject



55
56
57
# File 'app/models/blogue/post.rb', line 55

def id
  self.class.extract_post_id(path)
end

#metaObject



87
88
89
# File 'app/models/blogue/post.rb', line 87

def meta
  self.class.load_meta(body)
end

#private?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/blogue/post.rb', line 83

def private?
  filename_with_underscore? || meta_private?
end

#timeObject



63
64
65
# File 'app/models/blogue/post.rb', line 63

def time
  meta_time || file_ctime
end

#titleObject



67
68
69
# File 'app/models/blogue/post.rb', line 67

def title
  meta_title || parsed_title || filename_title
end

#tldrObject



75
76
77
# File 'app/models/blogue/post.rb', line 75

def tldr
  meta['tldr']
end

#to_helper_nameObject



99
100
101
# File 'app/models/blogue/post.rb', line 99

def to_helper_name
  id.underscore
end

#to_partial_pathObject



95
96
97
# File 'app/models/blogue/post.rb', line 95

def to_partial_path
  'posts/post'
end