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.

Direct Known Subclasses

FileContentRepository, GitContentRepository

Constant Summary collapse

MIME_TYPE =
{
  :js => 'text/javascript',       
  :md => 'text/markup',
  :rb => 'text/ruby',       
  :r => 'text/r',       
  :svg => 'text/svg',       
  :txt => 'text' 
}
@@primary_repository =

Repo to be used for all newly created content

nil

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(top_dir) ⇒ ContentRepository

Returns a new instance of ContentRepository.



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

def initialize(top_dir)
  @top_dir = top_dir
  @repo = Grit::Repo.new(top_dir)
end

Instance Attribute Details

#top_dirObject (readonly)

Returns the value of attribute top_dir.



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

def top_dir
  @top_dir
end

Class Method Details

.absolute_path_for(url) ⇒ Object



66
67
68
# File 'lib/omf-web/content/repository.rb', line 66

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/omf-web/content/repository.rb', line 33

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
    url = url_or_descr[:url]
  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.



93
94
95
96
# File 'lib/omf-web/content/repository.rb', line 93

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.


80
81
82
83
84
85
86
87
# File 'lib/omf-web/content/repository.rb', line 80

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



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

def self.find_repo_for(url)
  parts = url.split(':')
  case type = parts[0]
  when 'git'
    require 'omf-web/content/git_repository' 
    return GitContentRepository[parts[1]]
  when 'file'
    require 'omf-web/content/file_repository' 
    return FileContentRepository[parts[1]]
  else
    raise "Unknown repo type '#{type}'"
  end
end

.read_content(url, opts) ⇒ Object



70
71
72
# File 'lib/omf-web/content/repository.rb', line 70

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

Instance Method Details

#absolute_path(content_descr) ⇒ Object



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

def absolute_path(content_descr)
  path = _get_path(content_descr)
  File.join(@top_dir, path)
end

#mime_type_for_file(fname) ⇒ Object



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

def mime_type_for_file(fname)
  ext = fname.split('.')[-1]
  mt = MIME_TYPE[ext.to_sym] || 'text'
end

#read(content_descr) ⇒ Object



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

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