Module: CouchRestViews

Defined in:
lib/glue_envs/couchrest_glue_env.rb

Constant Summary collapse

ClassViewAllName =

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

"all"
ClassNamespaceKey =

view name stored in the couch db design doc

"all_this_class"
@@log =

Set Logger

TinkitLog.set(self.name, :warn)

Class Method Summary collapse

Class Method Details

.by_my_category(moab_data, user_datastore_location, match_key) ⇒ Object

TODO: Tied to datastructure



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object

TODO: Tied to datastructure



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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) ⇒ Object

begin



73
74
75
76
77
78
79
80
81
82
# 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 = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# 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