Class: Boombera::ContentItem

Inherits:
CouchRest::Document
  • Object
show all
Defined in:
lib/boombera/content_item.rb

Overview

ContentItem is a specialization of CouchRest::Document that adds content-mapping semantics and method-based access to the attributes that Boombera knows about.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc_or_path, body = nil, database = nil) ⇒ ContentItem

:nodoc:



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/boombera/content_item.rb', line 26

def initialize(doc_or_path, body = nil, database = nil) #:nodoc:
  case doc_or_path
  when CouchRest::Document
    @database = doc_or_path.database
    super(doc_or_path)
  when String
    @database = database
    super('_id' => doc_or_path, 'body' => body)
  else
    raise ArgumentError, "doc_or_path must either be an instance of CouchRest::Document or a String"
  end
end

Instance Attribute Details

#bodyObject

:nodoc:



6
7
8
# File 'lib/boombera/content_item.rb', line 6

def body
  @body
end

#pathObject (readonly)

:nodoc:



10
11
12
# File 'lib/boombera/content_item.rb', line 10

def path
  @path
end

Class Method Details

.get(path, db) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/boombera/content_item.rb', line 12

def self.get(path, db)
  doc = get_pointer(path, db)
  until doc.nil? || doc.resolved?
    doc = get_pointer(doc.maps_to, db)
  end
  doc
end

.get_pointer(path, db) ⇒ Object



20
21
22
23
24
# File 'lib/boombera/content_item.rb', line 20

def self.get_pointer(path, db)
  Boombera::ContentItem.new(db.get(path))
rescue RestClient::ResourceNotFound
  nil
end

Instance Method Details

#map_to(source_path) ⇒ Object

:nodoc:



39
40
41
42
43
44
45
46
47
48
# File 'lib/boombera/content_item.rb', line 39

def map_to(source_path) #:nodoc:
  rows = @database.view('boombera/content_paths', :key => source_path)['rows']
  if rows.empty?
    raise Boombera::InvalidMapping,
      "Tried to map #{path} to #{source_path}, but #{source_path} doesn't exist."
  else
    self.body = nil
    self['maps_to'] = source_path
  end
end

#maps_toObject

:nodoc:



66
67
68
69
# File 'lib/boombera/content_item.rb', line 66

def maps_to #:nodoc:
  return path unless body.nil?
  self['maps_to'] || path
end

#referenced_byObject

Returns the paths that are aliased to this ContentItem



51
52
53
54
# File 'lib/boombera/content_item.rb', line 51

def referenced_by
  rows = @database.view('boombera/map_references', :key => path)['rows']
  rows.map{ |row| row['value'] }.sort
end

#resolved?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/boombera/content_item.rb', line 62

def resolved?
  path == maps_to
end

#save(*args) ⇒ Object



56
57
58
59
60
# File 'lib/boombera/content_item.rb', line 56

def save(*args)
  self['maps_to'] = maps_to
  self['type'] = 'content_item'
  super
end