Class: OakTree::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/oaktree/specification.rb

Overview

Specifications for the blog, operates similar to Gem::Specification. URLs and paths are strings and should not end in a slash.

Constant Summary collapse

@@KEY_VALUE_PATTERN =
/^\s*(?<key>[\w_]+)\s*:\s*(?<value>.*?)\s*(#|$)/
@@DEFAULT_DATE_PATH_FORMAT =
'%Y/%m/'
@@DEFAULT_SLUG_SEPARATOR =
'_'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Specification

Initializes the Specification with its default values.

Yields:

  • (_self)

Yield Parameters:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/oaktree/specification.rb', line 109

def initialize
  # initialize default values for most properties
  self.title = ''
  self.description = ''
  self.base_url = ''
  self.post_path = 'post/'
  self.author = ''
  self.posts_per_page = 10
  self.reversed = false
  self.date_path_format = self.class.default_date_path_format
  self.slug_separator = self.class.default_slug_separator
  self.rss_length = 20

  yield self if block_given?

  @blog_root = File.expand_path(Dir.getwd) + '/'
end

Instance Attribute Details

#authorObject

The name of the blog’s author (currently assumes a single author)



31
32
33
# File 'lib/oaktree/specification.rb', line 31

def author
  @author
end

#blog_rootObject (readonly)

The blog root, where files are stored locally. Beneath this directory, there should be /source and /public directories, where post sources and the blog output, respectively, are stored. If these don’t exist, they’ll be created when generating the blog. This cannot be changed.



29
30
31
# File 'lib/oaktree/specification.rb', line 29

def blog_root
  @blog_root
end

#date_path_formatObject

The date format for post paths



37
38
39
# File 'lib/oaktree/specification.rb', line 37

def date_path_format
  @date_path_format
end

#descriptionObject

A description of the blog



23
24
25
# File 'lib/oaktree/specification.rb', line 23

def description
  @description
end

#posts_per_pageObject

The number of posts displayed per page



33
34
35
# File 'lib/oaktree/specification.rb', line 33

def posts_per_page
  @posts_per_page
end

#reversedObject

Whether the timeline is reversed



35
36
37
# File 'lib/oaktree/specification.rb', line 35

def reversed
  @reversed
end

#rss_lengthObject

The length of the RSS feed in number of posts. Defaults to 20-most-recent.



42
43
44
# File 'lib/oaktree/specification.rb', line 42

def rss_length
  @rss_length
end

#slug_separatorObject

The separator for words in slugs. May not be whitespace if loaded from a blog_spec file.



40
41
42
# File 'lib/oaktree/specification.rb', line 40

def slug_separator
  @slug_separator
end

#titleObject

The blog’s title



21
22
23
# File 'lib/oaktree/specification.rb', line 21

def title
  @title
end

Class Method Details

.default_date_path_formatObject



12
13
14
# File 'lib/oaktree/specification.rb', line 12

def self.default_date_path_format
  @@DEFAULT_DATE_PATH_FORMAT
end

.default_slug_separatorObject



16
17
18
# File 'lib/oaktree/specification.rb', line 16

def self.default_slug_separator
  @@DEFAULT_SLUG_SEPARATOR
end

.from_file(path) ⇒ Object

Loads a specification from a file.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/oaktree/specification.rb', line 86

def self.from_file(path)
  raise "Spec file does not exist" unless File.exists? path

  self.new { |spec|

    spec_contents = File.open(path, 'r') { |io| io.read }
    spec_hash = Psych.load(spec_contents)
    spec_hash.each {
      |key, value|
      setter_sym = :"#{key}="
      if spec.respond_to? setter_sym
        spec.send setter_sym, value
      else
        raise "Invalid key/value in spec: #{key} => #{value}"
      end
    }

    Dir.chdir(File.dirname(path))

  }
end

Instance Method Details

#base_urlObject

Gets the base URL of the blog (i.e., localhost/blog).



77
78
79
# File 'lib/oaktree/specification.rb', line 77

def base_url
  @base_url
end

#base_url=(url) ⇒ Object

Sets the base URL of the blog (i.e., localhost/blog) - should have a trailing slash.



70
71
72
73
74
# File 'lib/oaktree/specification.rb', line 70

def base_url= url
  url = String.new(url)
  url << '/'  unless url.end_with? '/'
  @base_url = url.freeze()
end

#export_stringObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/oaktree/specification.rb', line 127

def export_string
  <<-EOT
# metadata
title:  #{@title}
description: #{@description}
author: #{@author}
posts_per_page: 10

# public URL
base_url:  #{@base_url}

# public content paths
post_path: #{@post_path}
  EOT
end

#post_pathObject

Gets the post path (i.e., the subdirectory where posts are stored).



56
57
58
# File 'lib/oaktree/specification.rb', line 56

def post_path
  @post_path
end

#post_path=(path) ⇒ Object

Sets the post path (i.e., the subdirectory where posts are stored). Should not begin with a slash, but can have a trailing slash if you want. If there is no trailing slash, it will be part of the filename up to a a point.



48
49
50
51
52
53
# File 'lib/oaktree/specification.rb', line 48

def post_path= path
  raise "post_path provided is nil" if path.nil?
  raise "post_path provided is not a string" unless path.kind_of?(String)

  @post_path = path.clone().freeze()
end

#sources_rootObject



81
82
83
# File 'lib/oaktree/specification.rb', line 81

def sources_root
  @sources_root ||= "#{@blog_root}source/"
end