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 collapse
- @@file_repositories =
{}
Constants inherited from ContentRepository
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#top_dir ⇒ Object
readonly
Returns the value of attribute top_dir.
Class Method Summary collapse
-
.[](name) ⇒ Object
Return the repository which is referenced to by elements in ‘opts’.
-
.register_file_repo(name, top_dir, is_primary = false) ⇒ Object
Register an existing directory to the system.
Instance Method Summary collapse
-
#_get_path(content_descr) ⇒ Object
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.
-
#initialize(name, top_dir) ⇒ 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_repo_for, #mime_type_for_file, #read, read_content
Methods included from Common::Loggable
#_logger, #debug, #error, #fatal, #info, init_log, logger, set_environment, #warn
Constructor Details
#initialize(name, top_dir) ⇒ FileContentRepository
Returns a new instance of FileContentRepository.
47 48 49 50 51 |
# File 'lib/omf-web/content/file_repository.rb', line 47 def initialize(name, top_dir) @name = name @top_dir = top_dir @url_prefix = "file:#{name}:" end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
45 46 47 |
# File 'lib/omf-web/content/file_repository.rb', line 45 def name @name end |
#top_dir ⇒ Object (readonly)
Returns the value of attribute top_dir.
45 46 47 |
# File 'lib/omf-web/content/file_repository.rb', line 45 def top_dir @top_dir end |
Class Method Details
.[](name) ⇒ Object
Return the repository which is referenced to by elements in ‘opts’.
20 21 22 23 24 25 |
# File 'lib/omf-web/content/file_repository.rb', line 20 def self.[](name) unless repo = @@file_repositories[name.to_sym] raise "Unknown file repo '#{name}'" end repo end |
.register_file_repo(name, top_dir, is_primary = false) ⇒ Object
Register an existing directory to the system. It will be consulted for all content url’s strarting with ‘file:top_dir:’. If ‘is_primary’ is set to true, it will become the default repo for all newly created content in this app.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/omf-web/content/file_repository.rb', line 33 def self.register_file_repo(name, top_dir, is_primary = false) name = name.to_sym if @@file_repositories[name] warn "Ignoring repeated registration of file rep '#{name}'" return end repo = @@file_repositories[name] = FileContentRepository.new(name, top_dir) if is_primary @@primary_repository = repo end end |
Instance Method Details
#_get_path(content_descr) ⇒ Object
end
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/omf-web/content/file_repository.rb', line 146 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/omf-web/content/file_repository.rb', line 72 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 |
#write(content_descr, content, message) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/omf-web/content/file_repository.rb', line 91 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 |