Class: Boombera

Inherits:
Object
  • Object
show all
Extended by:
Information
Defined in:
lib/boombera.rb

Overview

This is the main interface to the Boombera content repository.

Usage examples:

# install/update the CouchDB design document
Boombera.install_design_doc!('my_database')

# connect to the database
boombera = Boombera.new('my_database')

# put and get some content
boombera.put('/hello', 'Hello, world!')
#=> true

content = boombera.get('/hello')
content.body
#=> 'Hello, world!'

# update the content via the ContentItem
content.body = 'Hello, friends!'
content.save
#=> true

# update the content without an object
boombera.put('/hello', 'Hello, everyone!')
content = boombera.get('/hello')
content.body
#=> 'Hello, everyone!'

# map an alias to the content
boombera.map('/hi', '/hello')
content = boombera.get('/hi')
content.path
#=> '/hello'

content.body
#=> 'Hello, everyone!'

# override the map with some different content
boombera.put('/hi', "G'day, mate!")
content = boombera.get('/hi')
content.path
#=> '/hi'

content.body
#=> "G'day, mate!"

content = boombera.get('/hello')
content.path
#=> '/hello'

content.body
#=> 'Hello, everyone!'

Defined Under Namespace

Modules: Information Classes: ContentItem, InvalidMapping, VersionMismatch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Information

database_version, design_doc, version

Constructor Details

#initialize(database_name) ⇒ Boombera

Connects to the CouchDB server and verifies the database version.

database_name

can be either a full url to a CouchDB server and database

(example.com:5984/my_database) or it can be just the database name itself (my_database). The latter will connect to the database at 127.0.0.1:5984.

raises

VersionMismatch



83
84
85
86
# File 'lib/boombera.rb', line 83

def initialize(database_name)
  @database = CouchRest.database!(database_name)
  check_database_version!
end

Instance Attribute Details

#databaseObject (readonly)

The CouchRest::Database instance



73
74
75
# File 'lib/boombera.rb', line 73

def database
  @database
end

Class Method Details

.install_design_doc!(database) ⇒ Object

Installs the CouchDB design document for this version of the library in the specified database!

WARNING

This will overwrite the current design document and prevent

applications that are using a different version of the Boombera library from accessing the database. This change will be replicated along with everything else in your database.

database_name

can be either a full url to a CouchDB server and database

(example.com:5984/my_database) or it can be just the database name itself (my_database). The latter will connect to the database at 127.0.0.1:5984.



100
101
102
103
104
105
106
# File 'lib/boombera.rb', line 100

def self.install_design_doc!(database)
  db = CouchRest.database!(database)
  existing = current_design_doc(db)
  design = design_doc
  design['_rev'] = existing['_rev'] unless existing.nil?
  db.save_doc(design)
end

Instance Method Details

#get(path) ⇒ Object

Returns the ContentItem associated with the specified path or nil if none is found. If path is mapped to another ContentItem, the resolved ContentItem will be returned.



122
123
124
# File 'lib/boombera.rb', line 122

def get(path)
  ContentItem.get(path, database)
end

#map(path, source_path) ⇒ Object

Creates a content mapping so that two paths can reference the same content without needing to store that content twice.

path

the new alias path for the ContentItem

source_path

the path of the ContentItem that should be returned for

requests to path. This path must point to an existing ContentItem.

raises

InvalidMapping



134
135
136
137
138
139
# File 'lib/boombera.rb', line 134

def map(path, source_path)
  content_map = ContentItem.get_pointer(path, database) \
    || ContentItem.new(path, nil, database)
  content_map.map_to source_path
  content_map.save
end

#put(path, body) ⇒ Object

Creates or updates the content stored at path with body.

body

can be # any object that can convert to JSON.

path

should be a String with /-separated tokens (i.e. “/foo/bar/baz”).



112
113
114
115
116
117
# File 'lib/boombera.rb', line 112

def put(path, body)
  content_item = ContentItem.get_pointer(path, database)  \
    and content_item.body = body
  content_item ||= ContentItem.new(path, body, database)
  content_item.save
end