Class: OMF::Web::ContentRepository

Inherits:
Common::LObject show all
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.

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

Class Method Summary collapse

Instance Method Summary collapse

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.



144
145
146
147
148
149
# File 'lib/omf-web/content/repository.rb', line 144

def initialize(name, opts)
  @name = name
  if @top_dir = opts[:top_dir]
    @top_dir = File.expand_path(@top_dir)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



142
143
144
# File 'lib/omf-web/content/repository.rb', line 142

def name
  @name
end

#top_dirObject (readonly)

Returns the value of attribute top_dir.



142
143
144
# File 'lib/omf-web/content/repository.rb', line 142

def top_dir
  @top_dir
end

Class Method Details

.absolute_path_for(url) ⇒ Object



99
100
101
# File 'lib/omf-web/content/repository.rb', line 99

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/omf-web/content/repository.rb', line 73

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.



136
137
138
139
# File 'lib/omf-web/content/repository.rb', line 136

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.


123
124
125
126
127
128
129
130
# File 'lib/omf-web/content/repository.rb', line 123

def self.find_files(selector, opts = {})
  # TODO: Search across ALL registered repos
  fs = @@primary_repository.find_files(selector, opts)
  if (max = opts[:max])
    fs = fs[0, max]
  end
  fs
end

.find_repo_for(url) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/omf-web/content/repository.rb', line 107

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



103
104
105
# File 'lib/omf-web/content/repository.rb', line 103

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
# 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]
end

Instance Method Details

#absolute_path(content_descr) ⇒ Object



180
181
182
183
# File 'lib/omf-web/content/repository.rb', line 180

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’



155
156
157
# File 'lib/omf-web/content/repository.rb', line 155

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



191
192
193
# File 'lib/omf-web/content/repository.rb', line 191

def get_url_for_path(path)
  raise "Missing implementation"
end

#mime_type_for_file(content_descriptor) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/omf-web/content/repository.rb', line 160

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



185
186
187
# File 'lib/omf-web/content/repository.rb', line 185

def path(content_descr)
  path = _get_path(content_descr)
end

#read(content_descr) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/omf-web/content/repository.rb', line 169

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