Class: OMF::Web::FileContentRepository
- Inherits:
-
ContentRepository
- Object
- Common::LObject
- ContentRepository
- OMF::Web::FileContentRepository
- Defined in:
- lib/omf-web/content/file_repository.rb
Overview
This class provides an interface to a directory based repository It retrieves, archives and versions content.
Constant Summary
Constants inherited from ContentRepository
ContentRepository::MIME_TYPE, ContentRepository::REPO_PLUGINS
Instance Attribute Summary
Attributes inherited from ContentRepository
Instance Method Summary collapse
- #_get_path(content_descr) ⇒ Object
-
#create_content_proxy_for(content_descr) ⇒ Object
Load content described by either a hash or a straightforward path and return a ‘ContentProxy’ holding it.
-
#get_url_for_path(path) ⇒ Object
Return a URL for a path in this repo.
-
#initialize(name, opts) ⇒ FileContentRepository
constructor
A new instance of FileContentRepository.
- #write(content_descr, content, message) ⇒ Object
Methods inherited from ContentRepository
#absolute_path, absolute_path_for, create_content_proxy_for, create_url, #find_files, find_files, find_repo_for, #mime_type_for_file, #path, #read, read_content, register_repo
Methods included from Common::Loggable
#_logger, #debug, #error, #fatal, #info, init_log, logger, set_environment, #warn
Constructor Details
#initialize(name, opts) ⇒ FileContentRepository
Returns a new instance of FileContentRepository.
46 47 48 49 |
# File 'lib/omf-web/content/file_repository.rb', line 46 def initialize(name, opts) super @url_prefix = "file:#{name}:" end |
Instance Method Details
#_get_path(content_descr) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/omf-web/content/file_repository.rb', line 107 def _get_path(content_descr) if content_descr.is_a? String path = content_descr.to_s if path.start_with? 'file:' path = path.split(':')[2] end elsif content_descr.is_a? Hash descr = content_descr if (url = descr[:url]) path = url.split(':')[2] # git:repo_name:path else path = descr[:path] end unless path raise "Missing 'path' or 'url' in content description (#{descr.inspect})" end path = path.to_s else raise "Unsupported type '#{content_descr.class}'" end unless path raise "Can't find path information in '#{content_descr.inspect}'" end return path end |
#create_content_proxy_for(content_descr) ⇒ Object
Load content described by either a hash or a straightforward path and return a ‘ContentProxy’ holding it.
If descr is true, return nil if file for which proxy is requested already exists.
@return: Content proxy
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/omf-web/content/file_repository.rb', line 70 def create_content_proxy_for(content_descr) path = _get_path(content_descr) # TODO: Make sure that key is really unique across multiple repositories descr = descr ? descr.dup : {} url = @url_prefix + path key = Digest::MD5.hexdigest(url) descr[:url] = url descr[:url_key] = key descr[:path] = path descr[:name] = url # Should be something human digestable if (descr[:strictly_new]) Dir.chdir(@top_dir) do return nil if File.exist?(path) end end proxy = ContentProxy.create(descr, self) return proxy end |
#get_url_for_path(path) ⇒ Object
Return a URL for a path in this repo
103 104 105 |
# File 'lib/omf-web/content/file_repository.rb', line 103 def get_url_for_path(path) "file:#{path}" end |
#write(content_descr, content, message) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/omf-web/content/file_repository.rb', line 89 def write(content_descr, content, ) path = _get_path(content_descr) Dir.chdir(@top_dir) do unless File.writable?(path) raise "Cannot write to file '#{path}'" end f = File.open(path, 'w') f.write(content) f.close end end |