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.



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

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

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#filterObject (readonly)

Filter for render post file.



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

def filter
  @filter
end

#publish_dateObject (readonly)

Post publish date, of course.



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

def publish_date
  @publish_date
end

#summaryObject (readonly)

Summary is a first paragraph of content.



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

def summary
  @summary
end

#tagsObject (readonly)

Tags accepts only one word per tag.



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

def tags
  @tags
end

#titleObject (readonly)

The title accepts Markdown syntax.



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

def title
  @title
end

Class Method Details

.load(file_name) ⇒ Object

Load all attributes from file name and read content.



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

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

Instance Method Details

#build_fileObject

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



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

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.



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

def create_into(directory)
  File.open(File.join(directory, @file), 'a') 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.



55
56
57
58
59
60
61
62
63
64
# File 'lib/postage/post.rb', line 55

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

#matched?(regexp) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/postage/post.rb', line 86

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

#to_sObject

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



67
68
69
70
71
# File 'lib/postage/post.rb', line 67

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