Class: CouchRest::Model::Design

Inherits:
Design
  • Object
show all
Includes:
CouchRest::Model::Designs::Migrations
Defined in:
lib/couchrest/model/design.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CouchRest::Model::Designs::Migrations

#migrate, #migrate!

Constructor Details

#initialize(model, prefix = nil) ⇒ Design

Instantiate a new design document for this model



17
18
19
20
21
22
23
# File 'lib/couchrest/model/design.rb', line 17

def initialize(model, prefix = nil)
  self.model       = model
  self.method_name = self.class.method_name(prefix)
  suffix = prefix ? "_#{prefix}" : ''
  self["_id"] = "_design/#{model.to_s}#{suffix}"
  apply_defaults
end

Instance Attribute Details

#auto_updateObject

Can this design save itself to the database? If false, the design will be loaded automatically before a view is executed.



13
14
15
# File 'lib/couchrest/model/design.rb', line 13

def auto_update
  @auto_update
end

#method_nameObject

The model Class that this design belongs to and method name



9
10
11
# File 'lib/couchrest/model/design.rb', line 9

def method_name
  @method_name
end

#modelObject

The model Class that this design belongs to and method name



9
10
11
# File 'lib/couchrest/model/design.rb', line 9

def model
  @model
end

Class Method Details

.method_name(prefix = nil) ⇒ Object



177
178
179
# File 'lib/couchrest/model/design.rb', line 177

def method_name(prefix = nil)
  (prefix ? "#{prefix}_" : '') + 'design_doc'
end

Instance Method Details

#checksumObject



60
61
62
63
64
65
66
67
# File 'lib/couchrest/model/design.rb', line 60

def checksum
  sum = self['couchrest-hash']
  if sum && (@_original_hash == to_hash)
    sum
  else
    checksum!
  end
end

#create_filter(name, function) ⇒ Object

FILTER HANDLING ########



106
107
108
109
# File 'lib/couchrest/model/design.rb', line 106

def create_filter(name, function)
  filters = (self['filters'] ||= {})
  filters[name.to_s] = function
end

#create_view(name, opts = {}) ⇒ Object

Add the specified view to the design doc the definition was made in and create quick access methods in the model.



100
101
102
# File 'lib/couchrest/model/design.rb', line 100

def create_view(name, opts = {})
  Designs::View.define_and_create(self, name, opts)
end

#create_view_lib(name, function) ⇒ Object

VIEW LIBS #########



113
114
115
116
# File 'lib/couchrest/model/design.rb', line 113

def create_view_lib(name, function)
  filters = (self['views']['lib'] ||= {})
  filters[name.to_s] = function
end

#databaseObject



69
70
71
# File 'lib/couchrest/model/design.rb', line 69

def database
  model.database
end

#has_view?(name) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/couchrest/model/design.rb', line 94

def has_view?(name)
  view_names.include?(name.to_s)
end

#sync(db = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/couchrest/model/design.rb', line 25

def sync(db = nil)
  if auto_update
    db ||= database
    if cache_checksum(db) != checksum
      sync!(db)
      set_cache_checksum(db, checksum)
    end
  end
  self
end

#sync!(db = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/couchrest/model/design.rb', line 36

def sync!(db = nil)
  db ||= database

  # Load up the last copy. We never blindly overwrite the remote copy
  # as it may contain views that are not used or known about by
  # our model.
  doc = load_from_database(db)

  if !doc || doc['couchrest-hash'] != checksum
    # We need to save something
    if doc
      # Different! Update.
      doc.merge!(to_hash)
    else
      # No previous doc, use a *copy* of our version.
      # Using a copy prevents reverse updates.
      doc = to_hash.dup
    end
    db.save_doc(doc)
  end

  self
end

#uri(db = database) ⇒ Object

Override the default #uri method for one that accepts the current database. This is used by the caching code.



76
77
78
# File 'lib/couchrest/model/design.rb', line 76

def uri(db = database)
  "#{db.root}/#{self['_id']}"
end

#view(name, opts = {}) ⇒ Object

Create a new view object. This overrides the normal CouchRest Design view method



85
86
87
# File 'lib/couchrest/model/design.rb', line 85

def view(name, opts = {})
  CouchRest::Model::Designs::View.new(self, model, opts, name)
end

#view_namesObject

Helper method to provide a list of all the views



90
91
92
# File 'lib/couchrest/model/design.rb', line 90

def view_names
  self['views'].keys
end