module CouchRestViews

Constants

ClassNamespaceKey
ClassViewAllName

Constants (pulling out magic text embedded in program) Changing these will break compatibility with earlier records

Public Class Methods

by_my_category(moab_data, user_datastore_location, match_key) click to toggle source

end

TODO: Tied to datastructure

# File lib/glue_envs/couchrest_glue_env.rb, line 85
def self.by_my_category(moab_data, user_datastore_location, match_key)
  db = moab_data[:db]
  design_doc = moab_data[:design_doc]
  map_str = "function(doc) {
                 if (doc.bufs_namespace =='#{user_datastore_location}' && doc.my_category ){
                   emit(doc.my_category, doc);
                }
             }"
  map_fn = { :map => map_str }
  self.set_view(db, design_doc, :my_category, map_fn)
  raw_res = design_doc.view :by_my_category, :key => match_key
  rows = raw_res["rows"]
  records = rows.map{|r| r["value"]}
end
by_parent_categories(moab_data, user_datastore_location, match_keys) click to toggle source

TODO: Tied to datastructure

# File lib/glue_envs/couchrest_glue_env.rb, line 101
def self.by_parent_categories(moab_data, user_datastore_location, match_keys)
  db = moab_data[:db]
  design_doc = moab_data[:design_doc]
  map_str = "function(doc) {
              if (doc.bufs_namespace == '#{user_datastore_location}' && doc.parent_categories) {
                     emit(doc.parent_categories, doc);
                  };
              };"
        #   }"
  map_fn = { :map => map_str }

  self.set_view(db, design_doc, :parent_categories, map_fn)
  raw_res = design_doc.view :by_parent_categories
  rows = raw_res["rows"]
  records = rows.map{|r| r["value"] if r["value"]["parent_categories"].include? match_keys}
end
set_my_cat_view(db, design_doc, user_datastore_location) click to toggle source

Set static views.

begin

# File lib/glue_envs/couchrest_glue_env.rb, line 73
def self.set_my_cat_view(db, design_doc, user_datastore_location)
  map_str = "function(doc) {
                 if (doc.#{ClassNamespaceKey} =='#{user_datastore_location}' && doc.my_category ){
                   emit(doc.my_category, doc);
                }
             }"
  map_fn = { :map => map_str }
  #TODO: Tied to datastructure
  self.set_view(db, design_doc, :my_category, map_fn)
end
set_view(db, design_doc, view_name, opts={}) click to toggle source
# File lib/glue_envs/couchrest_glue_env.rb, line 21
def self.set_view(db, design_doc, view_name, opts={})
  #raise view_name if view_name == :parent_categories
  #TODO: Add options for custom maps, etc
  #creating view in design_doc
  #puts "setting design_doc #{design_doc['_id']} with view: #{view_name.inspect} with map:\n #{opts.inspect}"
  design_doc.view_by view_name.to_sym, opts
  db_view_name = "by_#{view_name}"
  views = design_doc['views'] || {}
  view_keys = views.keys || []
  unless view_keys.include? db_view_name
    design_doc['_rev'] = nil
  end
  begin
    view_rev_in_db = db.get(design_doc['_id'])['_rev']
    #TODO: See if this can be simplified, I had forgotten the underscore for rev and added a bunch of other stuff
    #I also think I'm saving when it's not needed because I can't figure out how to detect if the saved view matches the
    #current view I want to run yet
    design_doc_uptodate = (design_doc['_rev'] == view_rev_in_db) && 
                                     (design_doc['views'].keys.include? db_view_name)
    design_doc['_rev'] = view_rev_in_db #unless design_doc_uptodate
    res = design_doc.save #unless design_doc_uptodate
    @@log.debug { "Save Design Doc Response: #{res.inspect}"} if @@log.debug?
    res
  rescue RestClient::RequestFailed
    if @@log.warn?
      @@log.warn { "Warning: Request Failed, assuming because the design doc was already saved?"}
    end
    if @@log.info?
      @@log.info { "Design doc_id: #{design_doc['_id'].inspect}"}
      @@log.info { "doc_rev: #{design_doc['_rev'].inspect}" }
      @@log.info { "db_rev: #{view_rev_in_db}" }
      @@log.info {"Code thinks doc is up to date? #{design_doc_uptodate.inspect}" }
    end
  end
end
set_view_all(db, design_doc, model_name, datastore_location) click to toggle source
# File lib/glue_envs/couchrest_glue_env.rb, line 58
def self.set_view_all(db, design_doc, model_name, datastore_location)
  view_name = "#{ClassViewAllName}_#{model_name}"
  namespace_id = ClassNamespaceKey
  record_namespace = "#{datastore_location}_#{model_name}"
  map_str = "function(doc) {
                if (doc['#{namespace_id}'] == '#{record_namespace}') {
                   emit(doc['_id'], doc);
                }
             }"
  map_fn = { :map => map_str }
  self.set_view(db, design_doc, view_name, map_fn)
end