Class: Pressy::Store::FileStore
- Inherits:
-
Object
- Object
- Pressy::Store::FileStore
- Defined in:
- lib/pressy/store/file_store.rb
Overview
FileStore is the default Store for Pressy sites.
FileStore saves each post as a file on disk. It also creates a
.pressy folder to hold the digests and configuration for the
site in YAML format.
Instance Attribute Summary collapse
-
#root ⇒ String
readonly
The root directory of the site.
Class Method Summary collapse
Instance Method Summary collapse
-
#all_posts ⇒ Array<Pressy::RenderedPost>
All of the posts found on disk.
-
#configuration ⇒ Hash
The saved configuration of the site.
- #configuration=(config) ⇒ Object
- #create(path, config = {}) ⇒ Object
-
#delete(id, post) ⇒ Object
Deletes a post from disk.
-
#digests ⇒ Hash
The saved digests for each post, by ID.
-
#initialize(root) ⇒ FileStore
constructor
Creates a new file store with the given root directory.
-
#write(id, post) ⇒ Object
Writes a rendered post to disk.
Constructor Details
Instance Attribute Details
#root ⇒ String (readonly)
The root directory of the site. This directory should contain the
.pressy directory.
11 12 13 |
# File 'lib/pressy/store/file_store.rb', line 11 def root @root end |
Class Method Details
.create(path, config = {}) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/pressy/store/file_store.rb', line 75 def self.create(path, config = {}) FileUtils.mkdir_p path store = self.new(path) store.configuration = config store end |
.current ⇒ Object
82 83 84 |
# File 'lib/pressy/store/file_store.rb', line 82 def self.current self.new(Dir.pwd) end |
Instance Method Details
#all_posts ⇒ Array<Pressy::RenderedPost>
Returns all of the posts found on disk
20 21 22 23 24 25 26 27 28 |
# File 'lib/pressy/store/file_store.rb', line 20 def all_posts Dir.glob("**/*.md", base: root).map do |path| content = File.read(File.join(root, path)) Pressy::RenderedPost.new( path, content, Digest::SHA256.hexdigest(content)) end end |
#configuration ⇒ Hash
Returns the saved configuration of the site
61 62 63 |
# File 'lib/pressy/store/file_store.rb', line 61 def configuration YAML.load_file(configuration_path) rescue {} end |
#configuration=(config) ⇒ Object
65 66 67 68 |
# File 'lib/pressy/store/file_store.rb', line 65 def configuration=(config) create_parent_directory configuration_path File.write(configuration_path, YAML.dump(config)) end |
#create(path, config = {}) ⇒ Object
70 71 72 73 |
# File 'lib/pressy/store/file_store.rb', line 70 def create(path, config = {}) path = Pathname.new(path).relative? ? File.join(root, path) : path self.class.create(path, config) end |
#delete(id, post) ⇒ Object
Deletes a post from disk.
47 48 49 50 51 52 53 |
# File 'lib/pressy/store/file_store.rb', line 47 def delete(id, post) File.unlink(post_path(post)) update_digests do |digests| digests.delete(id) end end |
#digests ⇒ Hash
Returns the saved digests for each post, by ID
56 57 58 |
# File 'lib/pressy/store/file_store.rb', line 56 def digests YAML.load_file(digests_path) rescue {} end |
#write(id, post) ⇒ Object
Writes a rendered post to disk.
34 35 36 37 38 39 40 41 42 |
# File 'lib/pressy/store/file_store.rb', line 34 def write(id, post) path = post_path(post) create_parent_directory path File.write(path, post.content) update_digests do |digests| digests[id] = post.digest end end |