Class: CouchStore

Inherits:
Store
  • Object
show all
Defined in:
lib/wiki/stores/couch.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Store

get_hash, method_missing, put_hash, set

Class Attribute Details

.dbObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/wiki/stores/couch.rb', line 8

def db
  unless @db
    couchdb_server = ENV['COUCHDB_URL'] || raise('please set ENV["COUCHDB_URL"]')
    @db = CouchRest.database!("#{couchdb_server}/sfw")
    begin
      @db.save_doc "_id" => "_design/recent-changes", :views => {}
    rescue RestClient::Conflict
      # design document already exists, do nothing
    end
  end
  @db
end

Class Method Details

.annotated_pages(pages_dir) ⇒ Object

COLLECTIONS



64
65
66
67
68
69
70
71
72
# File 'lib/wiki/stores/couch.rb', line 64

def annotated_pages(pages_dir)
  changes = pages pages_dir
  changes.map do |change|
    page = JSON.parse change['value']['data']
    page.merge! 'updated_at' => Time.parse(change['value']['updated_at'])
    page.merge! 'name' => change['value']['name']
    page
  end
end

.create_view(design_name, view_name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/wiki/stores/couch.rb', line 87

def create_view(design_name, view_name)
  design = db.get "_design/#{design_name}"
  design['views'][view_name] = {
    :map => "
      function(doc) {
        if (doc.directory == '#{view_name}')
          emit(doc._id, doc)
      }
    "
  }
  design.save
end

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/wiki/stores/couch.rb', line 108

def exists?(path)
  !(get_text path).nil?
end

.farm?(_) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/wiki/stores/couch.rb', line 100

def farm?(_)
  !!ENV['FARM_MODE']
end

.get_blob(path) ⇒ Object



32
33
34
35
# File 'lib/wiki/stores/couch.rb', line 32

def get_blob(path)
  blob = get_text path
  Base64.decode64 blob if blob
end

.get_text(path) ⇒ Object

GET



23
24
25
26
27
28
29
30
# File 'lib/wiki/stores/couch.rb', line 23

def get_text(path)
  path = relative_path(path)
  begin
    db.get(path)['data']
  rescue RestClient::ResourceNotFound
    nil
  end
end

.mkdir(_) ⇒ Object



104
105
106
# File 'lib/wiki/stores/couch.rb', line 104

def mkdir(_)
  # do nothing
end

.pages(pages_dir) ⇒ Object

UTILITY



76
77
78
79
80
81
82
83
84
85
# File 'lib/wiki/stores/couch.rb', line 76

def pages(pages_dir)
  pages_dir = relative_path pages_dir
  pages_dir_safe = CGI.escape pages_dir
  begin
    db.view("recent-changes/#{pages_dir_safe}")['rows']
  rescue RestClient::ResourceNotFound
    create_view 'recent-changes', pages_dir
    db.view("recent-changes/#{pages_dir_safe}")['rows']
  end
end

.put_blob(path, blob) ⇒ Object



57
58
59
60
# File 'lib/wiki/stores/couch.rb', line 57

def put_blob(path, blob)
  put_text path, Base64.strict_encode64(blob)
  blob
end

.put_text(path, text, metadata = {}) ⇒ Object

PUT



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wiki/stores/couch.rb', line 39

def put_text(path, text, ={})
  path = relative_path(path)
   = .each{ |k,v| [k] = relative_path(v) }
  attrs = {
    'data' => text,
    'updated_at' => Time.now.utc.iso8601
  }.merge! 

  begin
    db.save_doc attrs.merge('_id' => path)
  rescue RestClient::Conflict
    doc = db.get path
    doc.merge! attrs
    doc.save
  end
  text
end

.relative_path(path) ⇒ Object



112
113
114
115
# File 'lib/wiki/stores/couch.rb', line 112

def relative_path(path)
  raise "Please set @app_root" unless @app_root
  path.match(%r[^#{Regexp.escape @app_root}/?(.+?)$]) ? $1 : path
end