Class: Noumenon::ContentRepository::FileSystem

Inherits:
Noumenon::ContentRepository show all
Defined in:
lib/noumenon/content_repository/file_system.rb

Overview

A content repository which uses YAML files within the filesystem for storage.

Examples:

The structure of a filesystem repository

index.yml
|- about
   |- index.yml
   |- team.yml
   |- company.yml
contact.yml

A piece of content

template: team_member
name: Jon Wood
position: Head Honcho of Awesomeness
bio: Jon's just awesome, we'll leave it at that.

Instance Attribute Summary

Attributes inherited from Noumenon::ContentRepository

#options

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FileSystem

Returns a new instance of FileSystem.

Parameters:

  • options (Hash) (defaults to: {})

    A hash of options.

Options Hash (options):

  • :path (String)

    The path to access the repository at.



26
27
28
29
30
31
32
# File 'lib/noumenon/content_repository/file_system.rb', line 26

def initialize(options = {})
  unless options.key? :path
    raise ArgumentError.new("You must provide a path to the content repository: Noumenon::ContentRepository::FileSystem.new(path: '/tmp')")
  end

  super options
end

Instance Method Details

#children(root = "/", depth = 1, include_root = true) ⇒ Object

Return any content items below the path specified.

See Also:

  • Repository#children


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/noumenon/content_repository/file_system.rb', line 63

def children(root = "/", depth = 1, include_root = true)
  root.gsub! /\/$/, ''
  
  pattern = File.join(repository_path(root), "*")
  
  items = Dir.glob(pattern).collect { |i|
    i.gsub(repository_path("/"), "/").
      gsub(".yml", "")
  }.reject { |i| i.gsub(root, "").split("/").size > depth + 1 }
  items.delete(File.join(root, "index")) unless include_root
   
  items.collect { |item|
    path = item == "" ? "/" : item.gsub("index", "")
    
    # Loading every item probably isn't scalable, but it'll do for now. We can add caching
    # at a later date if needed.
    page = get(path).merge({ path: path })
    page[:children] = children(page[:path], depth - 1, false) if depth - 1 > 0
    page
  }.sort { |a, b| a[:path] <=> b[:path] }
end

#get(path, check_for_index = true) ⇒ Hash, ...

Loads a piece of content from the repository.

Parameters:

  • path (String)

    The path to load from.

  • check_for_index (Boolean) (defaults to: true)

    Whether sub-directories of the same name should be checked for an index.yml file

Returns:

  • (Hash, #each, nil)

    The piece of content, or nil if it could not be found.



40
41
42
43
44
45
46
# File 'lib/noumenon/content_repository/file_system.rb', line 40

def get(path, check_for_index = true)
  if content = read_file("#{path}.yml")
    YAML.load(content).symbolize_keys
  elsif check_for_index
    return get("#{path}/index", false)
  end
end

#put(path, content = {}) ⇒ Object

Saves a piece of content to the repsitory.

See Also:

  • Repository#put


52
53
54
55
56
57
# File 'lib/noumenon/content_repository/file_system.rb', line 52

def put(path, content = {})
  create_tree(path)
   
  path = File.join(path, "index") if File.exist?(repository_path(path))
  write_file "#{path}.yml", content.symbolize_keys.to_yaml
end