Class: Post

Inherits:
Object
  • Object
show all
Defined in:
lib/persona/post.rb

Constant Summary collapse

POST_FOLDER =
'./contents/posts'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ Post

Returns a new instance of Post.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/persona/post.rb', line 7

def initialize(file_name)
  f = File.open("#{POST_FOLDER}/#{file_name}","r")
  @content =  f.content_as_string
  @title = f. 'title'
  @author = f. 'author'  
  @date = Date.strptime(f.('date'), '%Y/%m/%d')
  @excerpt = create_excerpt(@content)
      
  splits = /(\d{4}-\d{2}-\d{2})-([^\/]*)\.txt$/.match file_name    
  if splits
    @url = splits[1].gsub(/[-]/, "/") + "/" + splits[2] + "/"            
  end
          
  f.close     
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



3
4
5
# File 'lib/persona/post.rb', line 3

def author
  @author
end

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/persona/post.rb', line 3

def content
  @content
end

#dateObject (readonly)

Returns the value of attribute date.



3
4
5
# File 'lib/persona/post.rb', line 3

def date
  @date
end

#excerptObject (readonly)

Returns the value of attribute excerpt.



3
4
5
# File 'lib/persona/post.rb', line 3

def excerpt
  @excerpt
end

#is_excerptObject (readonly)

Returns the value of attribute is_excerpt.



3
4
5
# File 'lib/persona/post.rb', line 3

def is_excerpt
  @is_excerpt
end

#titleObject (readonly)

Returns the value of attribute title.



3
4
5
# File 'lib/persona/post.rb', line 3

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/persona/post.rb', line 3

def url
  @url
end

Class Method Details

.allObject



27
28
29
30
31
32
33
34
35
# File 'lib/persona/post.rb', line 27

def self.all
  posts = Dir.entries("#{POST_FOLDER}").sort.reverse.reject do |it|
    not it.end_with? '.txt'
  end

  posts.map do |it|
    Post.new it
  end  
end

.from_url(y, m, d, name) ⇒ Object



23
24
25
# File 'lib/persona/post.rb', line 23

def self.from_url(y,m,d,name)
  Post.new "#{y}-#{m}-#{d}-#{name}.txt"    
end