Class: Postwave::Post

Inherits:
Object
  • Object
show all
Includes:
BlogUtilities
Defined in:
lib/postwave/post.rb

Constant Summary collapse

KNOWN_FIELDS =
%w(title date tags title_slug body draft)
REQUIRED_FIELDS =
%w(title date)
MEATADATA_DELIMTER =
"---"

Constants included from BlogUtilities

BlogUtilities::CONFIG_FILE_NAME, BlogUtilities::INDEX_FILE_NAME, BlogUtilities::META_DIR, BlogUtilities::POSTS_DIR, BlogUtilities::RSS_FILE_NAME, BlogUtilities::SUMMARY_FILE_NAME, BlogUtilities::TAGS_DIR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BlogUtilities

#config_values, #directory_paths, #file_paths, #find_missing_paths, #is_set_up?

Constructor Details

#initialize(file_name, field_content = {}) ⇒ Post

Returns a new instance of Post.



58
59
60
61
62
63
64
65
# File 'lib/postwave/post.rb', line 58

def initialize(file_name, field_content = {})
  @file_name = file_name

  field_content.each do |field, value|
    instance_variable_set("@#{field}", value)
    self.class.send(:attr_accessor, field)
  end
end

Instance Attribute Details

#file_nameObject

Returns the value of attribute file_name.



11
12
13
# File 'lib/postwave/post.rb', line 11

def file_name
  @file_name
end

Class Method Details

.new_from_file_path(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/postwave/post.rb', line 13

def self.new_from_file_path(path)
   = 0
  body_buffer_count = 0
  field_content = { "body" => "" }

  File.readlines(path).each do |line|
    clean_line = line.strip
    if clean_line == MEATADATA_DELIMTER
       += 1
      next
    end

    if  == 0
      next
    elsif  == 1
      field, value = clean_line.split(":", 2).map(&:strip)
      field_content[field] = value
    else
      if body_buffer_count == 0
        body_buffer_count += 1
        next if clean_line.empty?
      end

      field_content["body"] += "#{line}"
    end
  end

  # turn "date" into a Time object
  field_content["date"] = Time.parse(field_content["date"])

  # turn "tags" into an array
  if field_content["tags"]
    field_content["tags"] = field_content["tags"].split(",").map do |tag|
      tag.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
    end
  end

  # turn "draft" into boolean
  if field_content["draft"]
    field_content["draft"] = field_content["draft"].downcase == "true"
  end

  self.new(path, field_content)
end

Instance Method Details

#generated_file_nameObject



79
80
81
82
# File 'lib/postwave/post.rb', line 79

def generated_file_name
  # YYYY-MM-DD-slug-from-title.md
  "#{@date.to_s[..9]}-#{slug}.md"
end

#slugObject



71
72
73
# File 'lib/postwave/post.rb', line 71

def slug
  @slug ||= @title_slug
end

#slug=(new_slug) ⇒ Object



75
76
77
# File 'lib/postwave/post.rb', line 75

def slug=(new_slug)
  @slug = new_slug
end

#title_slugObject



67
68
69
# File 'lib/postwave/post.rb', line 67

def title_slug
  @title_slug ||= @title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end

#update_file_name!Object



84
85
86
87
88
89
90
# File 'lib/postwave/post.rb', line 84

def update_file_name!
  desired_file_name = generated_file_name
  return false if @file_name == desired_file_name

  File.rename(@file_name, File.join(Dir.pwd, POSTS_DIR, desired_file_name))
  @file_name = desired_file_name
end