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::SUMMARY_FILE_NAME, BlogUtilities::TAGS_DIR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BlogUtilities

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

Constructor Details

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

Returns a new instance of Post.



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

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
# 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}\n"
    end
  end

  # 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



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

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

#slugObject



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

def slug
  @slug ||= @title_slug
end

#slug=(new_slug) ⇒ Object



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

def slug=(new_slug)
  @slug = new_slug
end

#title_slugObject



64
65
66
# File 'lib/postwave/post.rb', line 64

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

#update_file_name!Object



81
82
83
84
85
86
87
# File 'lib/postwave/post.rb', line 81

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