Class: Noumenon::ContentRepository::FileSystem
- Inherits:
-
Noumenon::ContentRepository
- Object
- Noumenon::ContentRepository
- Noumenon::ContentRepository::FileSystem
- Defined in:
- lib/noumenon/content_repository/file_system.rb
Overview
A content repository which uses YAML files within the filesystem for storage.
Instance Attribute Summary
Attributes inherited from Noumenon::ContentRepository
Instance Method Summary collapse
-
#children(root = "/", depth = 1, include_root = true) ⇒ Object
Return any content items below the path specified.
-
#get(path, check_for_index = true) ⇒ Hash, ...
Loads a piece of content from the repository.
-
#initialize(options = {}) ⇒ FileSystem
constructor
A new instance of FileSystem.
-
#put(path, content = {}) ⇒ Object
Saves a piece of content to the repsitory.
Constructor Details
#initialize(options = {}) ⇒ FileSystem
Returns a new instance of FileSystem.
26 27 28 29 30 31 32 |
# File 'lib/noumenon/content_repository/file_system.rb', line 26 def initialize( = {}) unless .key? :path raise ArgumentError.new("You must provide a path to the content repository: Noumenon::ContentRepository::FileSystem.new(path: '/tmp')") end super end |
Instance Method Details
#children(root = "/", depth = 1, include_root = true) ⇒ Object
Return any content items below the path specified.
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.
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.
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 |