Module: Recliner::Views::ClassMethods

Defined in:
lib/recliner/views.rb

Instance Method Summary collapse

Instance Method Details

#countObject

Fetch the number of objects of this type from the database. FIXME: Use a more efficient view



111
112
113
# File 'lib/recliner/views.rb', line 111

def count
  all.size
end

#default_conditions(conditions = nil) ⇒ Object

Sets or gets the default view conditions for this document type.

When setting, conditions may be either a String or a Hash. Using a Hash is recommended as it will allow subclasses to specify further conditions by using:

default_conditions.merge!({ :override => 'conditions' })


60
61
62
63
64
65
66
67
# File 'lib/recliner/views.rb', line 60

def default_conditions(conditions=nil)
  if conditions
    write_inheritable_attribute(:default_conditions, conditions)
    reset_views!
  end
  
  read_inheritable_attribute(:default_conditions)
end

#default_order(attribute = nil) ⇒ Object

Sets or gets the default view order for this document type.

When setting, attribute should be the property name.



43
44
45
46
47
48
49
50
51
52
# File 'lib/recliner/views.rb', line 43

def default_order(attribute=nil)
  if attribute
    property = properties[attribute.to_sym]
    
    write_inheritable_attribute(:default_order, property ? property.as : attribute)
    reset_views!
  end
  
  read_inheritable_attribute(:default_order)
end

#firstObject

Fetch the first object of this type from the database.



100
101
102
# File 'lib/recliner/views.rb', line 100

def first
  all(:limit => 1).first
end

#initialize_views!Object

Initializes the views for this document type by synchronizing with the CouchDB view document.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/recliner/views.rb', line 84

def initialize_views!
  return if views_initialized?
  
  views = self.views.inject({}) do |result, (name, options)|
    options = { :order => default_order, :conditions! => default_conditions }.merge(options)
    
    result[name] = View.new(interpolate_hash_values(options))
    result
  end
  
  view_document.update_views(views)
  
  views_initialized!
end

#lastObject

Fetch the last object of this type from the database.



105
106
107
# File 'lib/recliner/views.rb', line 105

def last
  all(:limit => 1, :descending => true).first
end

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

Defines a view.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/recliner/views.rb', line 28

def view(name, options={})
  views[name] = options
  reset_views!

  class_eval <<-END_RUBY
    def self.#{name}(*args)                     # def self.by_name(*args)
      initialize_views!                         #   initialize_views!
      view_document.invoke('#{name}', *args)    #   view_document.invoke('by_name', *args)
    end                                         # end
  END_RUBY
end

#view_documentObject

Returns the view design document for this document type. If it doesn’t already exist, a new view document will be created.



71
72
73
74
75
76
# File 'lib/recliner/views.rb', line 71

def view_document
  @_view_document ||=
    ViewDocument.with_database(database) do
      ViewDocument.load(view_document_id) || ViewDocument.new(:id => view_document_id)
    end
end

#viewsObject

Returns hash of all the views that have been defined for this document type and parent document types.



23
24
25
# File 'lib/recliner/views.rb', line 23

def views
  read_inheritable_attribute(:views) || write_inheritable_attribute(:views, {})
end

#views_initialized?Boolean

Returns true if the views for this document type have been initialized; otherwise returns false.

Returns:



79
80
81
# File 'lib/recliner/views.rb', line 79

def views_initialized?
  @_views_initialized
end