Class: Pressy::Store::FileStore

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileStore

Creates a new file store with the given root directory.

Parameters:

  • root (String)

    The root directory of the site.



15
16
17
# File 'lib/pressy/store/file_store.rb', line 15

def initialize(root)
  @root = root
end

Instance Attribute Details

#rootString (readonly)

The root directory of the site. This directory should contain the .pressy directory.

Returns:

  • (String)


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

.currentObject



82
83
84
# File 'lib/pressy/store/file_store.rb', line 82

def self.current
  self.new(Dir.pwd)
end

Instance Method Details

#all_postsArray<Pressy::RenderedPost>

Returns all of the posts found on disk

Returns:



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

#configurationHash

Returns the saved configuration of the site

Returns:

  • (Hash)

    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.

Parameters:

  • id (Fixnum)

    the ID of the post being deleted.

  • post (Pressy::RenderedPost)

    the post to delete



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

#digestsHash

Returns the saved digests for each post, by ID

Returns:

  • (Hash)

    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.

Parameters:

  • id (Fixnum)

    the ID of the post being written. This is used to update the cached digests for detecting conflicts.

  • post (Pressy::RenderedPost)

    the post to write



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