Class: OMF::Web::ContentRepository
- Inherits:
-
Common::LObject
- Object
- Common::LObject
- OMF::Web::ContentRepository
- Defined in:
- lib/omf-web/content/repository.rb
Overview
This class provides an interface to a particular content repository. It retrieves, archives and versions content.
Direct Known Subclasses
FileContentRepository, GitContentRepository, IRodsContentRepository, StaticContentRepository
Constant Summary collapse
- MIME_TYPE =
{ :js => 'text/javascript', :md => 'text/markup', :rb => 'text/ruby', :r => 'text/r', :svg => 'text/svg', :txt => 'text' }
- REPO_PLUGINS =
{ git: lambda do |name, opts| require 'omf-web/content/git_repository' return GitContentRepository.new(name, opts) end, file: lambda do |name, opts| require 'omf-web/content/file_repository' return FileContentRepository.new(name, opts) end, irods: lambda do |name, opts| require 'omf-web/content/irods_repository' return IRodsContentRepository.new(name, opts) end, static: lambda do |name, opts| require 'omf-web/content/static_repository' return StaticContentRepository.new(name, opts) end }
- @@primary_repository =
Repo to be used for all newly created content
nil
- @@repositories =
{}
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
- .absolute_path_for(url) ⇒ Object
-
.create_content_proxy_for(url_or_descr, opts = {}) ⇒ Object
Load content described by either a hash or a straightforward url and return a ‘ContentProxy’ holding it.
-
.create_url(path, strictly_new = true) ⇒ Object
Create a URL for a file with ‘path’ in the user’s primary repository.
-
.find_files(selector, opts = {}) ⇒ Object
Find files whose file name matches ‘selector’.
- .find_repo_for(url) ⇒ Object
- .read_content(url, opts) ⇒ Object
- .register_repo(name, opts) ⇒ Object
Instance Method Summary collapse
- #absolute_path(content_descr) ⇒ Object
-
#find_files(search_pattern, opts = {}) ⇒ Object
Return an array of file names which are in the repository and match ‘search_pattern’.
-
#get_url_for_path(path) ⇒ Object
Return a URL for a path in this repo.
-
#initialize(name, opts) ⇒ ContentRepository
constructor
A new instance of ContentRepository.
- #mime_type_for_file(content_descriptor) ⇒ Object
- #path(content_descr) ⇒ Object
- #read(content_descr) ⇒ Object
Methods included from Common::Loggable
#_logger, #debug, #error, #fatal, #info, init_log, logger, set_environment, #warn
Constructor Details
#initialize(name, opts) ⇒ ContentRepository
Returns a new instance of ContentRepository.
148 149 150 151 152 153 |
# File 'lib/omf-web/content/repository.rb', line 148 def initialize(name, opts) @name = name if @top_dir = opts[:top_dir] @top_dir = File.(@top_dir) end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
146 147 148 |
# File 'lib/omf-web/content/repository.rb', line 146 def name @name end |
#top_dir ⇒ Object (readonly)
Returns the value of attribute top_dir.
146 147 148 |
# File 'lib/omf-web/content/repository.rb', line 146 def top_dir @top_dir end |
Class Method Details
.absolute_path_for(url) ⇒ Object
100 101 102 |
# File 'lib/omf-web/content/repository.rb', line 100 def self.absolute_path_for(url) find_repo_for(url).absolute_path(url) end |
.create_content_proxy_for(url_or_descr, opts = {}) ⇒ Object
Load content described by either a hash or a straightforward url and return a ‘ContentProxy’ holding it.
@return: Content proxy
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/omf-web/content/repository.rb', line 74 def self.create_content_proxy_for(url_or_descr, opts = {}) debug "self.create_content_proxy_for: '#{url_or_descr.inspect}'" if url_or_descr.is_a? ContentProxy return url_or_descr end if url_or_descr.is_a? String url = url_or_descr else if (text = url_or_descr[:text]) # a bit of a hack for small static text blocks # Much better for maintenance is to use a separate file url = "static:-" else url = url_or_descr[:url] end end unless url throw "Can't find url in '#{url_or_descr.inspect}" end repo = find_repo_for(url) repo.create_content_proxy_for(url_or_descr) end |
.create_url(path, strictly_new = true) ⇒ Object
Create a URL for a file with ‘path’ in the user’s primary repository. If ‘strictly_new’ is true, returns nil if ‘path’ already exists.
140 141 142 143 |
# File 'lib/omf-web/content/repository.rb', line 140 def self.create_url(path, strictly_new = true) # TODO: Need to add code to select proper repository return GitContentRepository.create_url(path, strictly_new) end |
.find_files(selector, opts = {}) ⇒ Object
Find files whose file name matches ‘selector’.
Supported options:
* :max - Maximum numbers of matches to return
* :mime_type - Only return files with that specific mime type.
* :repo_iterator [Iterator] - Iterator over repos to search
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/omf-web/content/repository.rb', line 125 def self.find_files(selector, opts = {}) fsa = (opts[:repo_iterator] || [@@primary_repository]).map do |repo| repo.find_files(selector, opts) end fs = fsa.flatten if (max = opts[:max]) fs = fs[0, max] end fs end |
.find_repo_for(url) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/omf-web/content/repository.rb', line 108 def self.find_repo_for(url) parts = url.split(':') name = parts[1] unless repo = @@repositories[name.to_sym] raise "Unknown repo '#{name}'" end return repo end |
.read_content(url, opts) ⇒ Object
104 105 106 |
# File 'lib/omf-web/content/repository.rb', line 104 def self.read_content(url, opts) find_repo_for(url).read(url) end |
.register_repo(name, opts) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/omf-web/content/repository.rb', line 48 def self.register_repo(name, opts) raise "ArgumentMismatch: Expected Hash, but got #{opts}" unless opts.is_a? Hash name = name.to_sym if @@repositories[name] warn "Ignoring repeated registration of repo '#{name}'" return end unless type = opts[:type] raise "Missing type in repo opts (#{opts})" end unless repo_creator = REPO_PLUGINS[type.to_sym] raise "Unknown repository type '#{type}'" end @@repositories[name] = r = repo_creator.call(name, opts) @@primary_repository = r if opts[:is_primary] r end |
Instance Method Details
#absolute_path(content_descr) ⇒ Object
184 185 186 187 |
# File 'lib/omf-web/content/repository.rb', line 184 def absolute_path(content_descr) path = _get_path(content_descr) File.join(@top_dir, path) end |
#find_files(search_pattern, opts = {}) ⇒ Object
Return an array of file names which are in the repository and match ‘search_pattern’
159 160 161 |
# File 'lib/omf-web/content/repository.rb', line 159 def find_files(search_pattern, opts = {}) raise "Missing implementation" end |
#get_url_for_path(path) ⇒ Object
Return a URL for a path in this repo
195 196 197 |
# File 'lib/omf-web/content/repository.rb', line 195 def get_url_for_path(path) raise "Missing implementation" end |
#mime_type_for_file(content_descriptor) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/omf-web/content/repository.rb', line 164 def mime_type_for_file(content_descriptor) fname = content_descriptor if content_descriptor.is_a? Hash fname = content_descriptor[:path] end ext = fname.split('.')[-1] mt = MIME_TYPE[ext.to_sym] || 'text' end |
#path(content_descr) ⇒ Object
189 190 191 |
# File 'lib/omf-web/content/repository.rb', line 189 def path(content_descr) path = _get_path(content_descr) end |
#read(content_descr) ⇒ Object
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/omf-web/content/repository.rb', line 173 def read(content_descr) path = _get_path(content_descr) Dir.chdir(@top_dir) do unless File.readable?(path) raise "Cannot read file '#{path}'" end content = File.open(path).read return content end end |