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) ⇒ Object
Return any content items below the path specified.
-
#get(path, check_for_index = true) ⇒ Hash, ...
Loads a piece of content from the repository.
-
#get_asset(path) ⇒ Object
Retreive a static asset to the repository.
- #get_asset_path(path) ⇒ Object
-
#initialize(options = {}) ⇒ FileSystem
constructor
A new instance of FileSystem.
-
#put(path, content = {}) ⇒ Object
Saves a piece of content to the repsitory.
-
#save_asset(path, content) ⇒ Object
Save a static asset to the repository.
Methods inherited from Noumenon::ContentRepository
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) ⇒ 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 84 |
# File 'lib/noumenon/content_repository/file_system.rb', line 63 def children(root = "/", depth = 1) root.gsub! /\/$/, '' pattern = File.join(repository_path(root), "*") items = Dir.glob(pattern).collect do |i| i.gsub(repository_path("/"), "/"). gsub(".yml", "") end # We're not interested in indexes, since they're not children. items.delete(File.join(root, "index")) items.reject { |i| i.gsub(root, "").split("/").size > depth + 1 }.collect do |item| path = item == "" ? "/" : item # 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) if depth - 1 > 0 page end 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 |
#get_asset(path) ⇒ Object
Retreive a static asset to the repository.
99 100 101 102 |
# File 'lib/noumenon/content_repository/file_system.rb', line 99 def get_asset(path) return nil unless File.extname(path).size > 0 read_file File.join("assets", path) end |
#get_asset_path(path) ⇒ Object
104 105 106 107 |
# File 'lib/noumenon/content_repository/file_system.rb', line 104 def get_asset_path(path) return nil unless File.extname(path).size > 0 repository_path "assets/#{path}" 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 |
#save_asset(path, content) ⇒ Object
Save a static asset to the repository.
90 91 92 93 |
# File 'lib/noumenon/content_repository/file_system.rb', line 90 def save_asset(path, content) raise ArgumentError.new("Assets must have a file extension.") unless File.extname(path).size > 0 write_file File.join("assets", path), content end |