Class: Postage::Post

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

Overview

end_content

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Post

Initialize new post using options.



51
52
53
# File 'lib/postage/post.rb', line 51

def initialize(options = {})
  options.instance_variables_set_to(self)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



40
41
42
# File 'lib/postage/post.rb', line 40

def content
  @content
end

#fileObject (readonly)

Post text file



45
46
47
# File 'lib/postage/post.rb', line 45

def file
  @file
end

#filterObject (readonly)

Filter for render post file.



42
43
44
# File 'lib/postage/post.rb', line 42

def filter
  @filter
end

#pathObject (readonly)

Post file path



48
49
50
# File 'lib/postage/post.rb', line 48

def path
  @path
end

#publish_dateObject (readonly)

Post publish date, of course.



33
34
35
# File 'lib/postage/post.rb', line 33

def publish_date
  @publish_date
end

#summaryObject (readonly)

Summary is a first paragraph of content.



39
40
41
# File 'lib/postage/post.rb', line 39

def summary
  @summary
end

#tagsObject (readonly)

Tags accepts only one word per tag.



37
38
39
# File 'lib/postage/post.rb', line 37

def tags
  @tags
end

#titleObject (readonly)

The title accepts Markdown syntax.



35
36
37
# File 'lib/postage/post.rb', line 35

def title
  @title
end

Class Method Details

.load(file_name) ⇒ Object

Load all attributes from file name and read content.



56
57
58
# File 'lib/postage/post.rb', line 56

def self.load(file_name)
  new.extract_attributes(file_name)
end

Instance Method Details

#<=>(post) ⇒ Object

Sort posts by format.



100
101
102
# File 'lib/postage/post.rb', line 100

def <=>(post)
  to_s <=> post.to_s
end

#build_fileObject

Build post file name and return following format: yyyymmdd-post_name.tags.separated.by.points.filter



81
82
83
# File 'lib/postage/post.rb', line 81

def build_file
  @file = "#{build_publish_date}-#{build_file_name}.#{build_tags}.#{build_filter}"
end

#create_into(directory) ⇒ Object

Get post file name and creates content and save into directory.



86
87
88
89
90
91
92
# File 'lib/postage/post.rb', line 86

def create_into(directory)
  build_file
  Pathname.new(directory).join(@file).open 'w+' do |file|
    post = self
    file << ERB.new(load_template).result(binding)
  end
end

#extract_attributes(file_name) ⇒ Object

Check and extract all attributes from file name.



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/postage/post.rb', line 61

def extract_attributes(file_name)
  extract_publish_date(file_name)
  extract_tags(file_name)
  extract_filter(file_name)
  extract_title_and_content(file_name)
  @path    = Pathname.new(file_name)
  @file    = @path.to_s
  @title   = @file.gsub('_', ' ').capitalize if @title.to_s.empty?
  @summary = @content.match(%r{<p>.*</p>}).to_s
  self
end

#matched?(regexp) ⇒ Boolean

Test pattern in title or filename.

Returns:

  • (Boolean)


95
96
97
# File 'lib/postage/post.rb', line 95

def matched?(regexp)
  @title.match(regexp) || @file.match(regexp)
end

#to_sObject

Return post name formatted (“year/month/day/name”).



74
75
76
77
78
# File 'lib/postage/post.rb', line 74

def to_s
  @file.to_s.scan(%r{(\d{4})(\d{2})(\d{2})(.*?)-(.*?)\..*}) do |year,month,day,time,name|
    return "#{year}/#{month}/#{day}/#{name}"
  end
end