Class: OMF::Web::ContentRepository

Inherits:
Base::LObject
  • Object
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',
  :oedl => '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

Constructor Details

#initialize(name, opts) ⇒ ContentRepository

params opts [Hash] opts read_only [Boolean] If true, write will fail



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

def initialize(name, opts)
  @name = name
  @read_only = (opts[:read_only] == true)

  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.



157
158
159
# File 'lib/omf-web/content/repository.rb', line 157

def name
  @name
end

#top_dirObject (readonly)

Returns the value of attribute top_dir.



157
158
159
# File 'lib/omf-web/content/repository.rb', line 157

def top_dir
  @top_dir
end

Class Method Details

.absolute_path_for(url) ⇒ Object



106
107
108
# File 'lib/omf-web/content/repository.rb', line 106

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/omf-web/content/repository.rb', line 78

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
      require 'omf-web/content/static_repository'
      url = OMF::Web::StaticContentRepository.create_from_text(url_or_descr, opts)
      #url = repo.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.



151
152
153
154
# File 'lib/omf-web/content/repository.rb', line 151

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


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/omf-web/content/repository.rb', line 131

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 (mt = opts[:mime_type])
    fs = fs.select { |f| File.fnmatch(mt, f[:mime_type]) }
  end

  if (max = opts[:max])
    fs = fs[0, max]
  end
  fs
end

.find_repo_for(url) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/omf-web/content/repository.rb', line 114

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



110
111
112
# File 'lib/omf-web/content/repository.rb', line 110

def self.read_content(url, opts)
  find_repo_for(url).read(url)
end

.register_repo(name, opts) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/omf-web/content/repository.rb', line 52

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



203
204
205
206
# File 'lib/omf-web/content/repository.rb', line 203

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’



174
175
176
# File 'lib/omf-web/content/repository.rb', line 174

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



214
215
216
# File 'lib/omf-web/content/repository.rb', line 214

def get_url_for_path(path)
  raise "Missing implementation"
end

#mime_type_for_file(content_descriptor) ⇒ Object



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

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



208
209
210
# File 'lib/omf-web/content/repository.rb', line 208

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

#read(content_descr) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/omf-web/content/repository.rb', line 188

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

#write(content_descr, content, message) ⇒ Object



199
200
201
# File 'lib/omf-web/content/repository.rb', line 199

def write(content_descr, content, message)
  raise "Missing implementation"
end