Module: Toy::Couch::Views::ClassMethods

Defined in:
lib/toy/couch/views.rb

Instance Method Summary collapse

Instance Method Details

#can_reduce_view?(name) ⇒ Boolean

Check if the view can be reduced by checking to see if it has a reduce function.

Returns:

  • (Boolean)


93
94
95
# File 'lib/toy/couch/views.rb', line 93

def can_reduce_view?(name)
  design_doc && design_doc.can_reduce_view?(name)
end

#first_from_view(name, *args) ⇒ Object

Find the first entry in the view. If the second parameter is a string it will be used as the key for the request, for example:

Course.first_from_view('by_teacher', 'Fred')

More advanced requests can be performed by providing a hash:

Course.first_from_view('by_teacher', :startkey => 'bbb', :endkey => 'eee')


115
116
117
118
119
120
121
122
123
124
125
# File 'lib/toy/couch/views.rb', line 115

def first_from_view(name, *args)
  query = {:limit => 1}
  case args.first
  when String, Array
    query.update(args[1]) unless args[1].nil?
    query[:key] = args.first
  when Hash
    query.update(args.first)
  end
  view(name, query).first
end

#has_view?(name) ⇒ Boolean

returns stored defaults if there is a view named this in the design doc

Returns:

  • (Boolean)


87
88
89
# File 'lib/toy/couch/views.rb', line 87

def has_view?(name)
  design_doc && design_doc.has_view?(name)
end

#view(name, query = {}, &block) ⇒ Object

Dispatches to any named view.



98
99
100
101
102
103
104
# File 'lib/toy/couch/views.rb', line 98

def view(name, query={}, &block)
  query = query.dup # Modifications made on copy!
  query[:raw] = true if query[:reduce]
  raw = query.delete(:raw)
  save_design_doc(store.client)
  fetch_view_with_docs( name, query, raw, &block)
end

#view_by(*keys) ⇒ Object

This was pretty much stolen whole sale from CouchRest Model github.com/couchrest/couchrest_model/blob/master/lib/couchrest/model/views.rb

Define a Couchstore.client view. The name of the view will be the concatenation of by and the keys joined by and

Example views:

class Post
  include Toy::Store
  store :couch, CouchRest.database!("http://127.0.0.1:5984/agree2-development")
  # view with default options
  # query with Post.by_date
  view_by :date, :descending => true

  # view with compound sort-keys
  # query with Post.by_user_id_and_date
  view_by :user_id, :date

  # view with custom map/reduce functions
  # query with Post.by_tags :reduce => true
  view_by :tags,                                                
    :map =>                                                     
      "function(doc) {                                          
        if (doc['model'] == 'Post' && doc.tags) {                   
          doc.tags.forEach(function(tag){                       
            emit(doc.tag, 1);                                   
          });                                                   
        }                                                       
      }",                                                       
    :reduce =>                                                  
      "function(keys, values, rereduce) {                       
        return sum(values);                                     
      }"                                                        
end

view_by :date will create a view defined by this Javascript function:

function(doc) {
  if (doc['model'] == 'Post' && doc.date) {
    emit(doc.date, null);
  }
}

It can be queried by calling Post.by_date which accepts all valid options for CouchRest::Database#view. In addition, calling with the :raw => true option will return the view rows themselves. By default Post.by_date will return the documents included in the generated view.

Toy::Stor#view options can be applied at view definition time as defaults, and they will be curried and used at view query time. Or they can be overridden at query time.

Custom views can be queried with :reduce => true to return reduce results. The default for custom views is to query with :reduce => false.

Views are generated (on a per-model basis) lazily on first-access. This means that if you are deploying changes to a view, the views for that model won’t be available until generation is complete. This can take some time with large databases. Strategies are in the works.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/toy/couch/views.rb', line 73

def view_by(*keys)
  include Type
  opts = keys.pop if keys.last.is_a?(Hash)
  opts ||= {}
  ducktype = opts.delete(:ducktype)
  unless ducktype || opts[:map]
    opts[:guards] ||= []
    opts[:guards].push "(doc['type'] == '#{self.to_s}')"
  end
  keys.push opts
  design_doc.view_by(*keys)
end