Class: WingedCouch::ViewsLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/winged_couch/views_loader.rb

Overview

Class for fetching and uploading views to CouchDB

Constant Summary collapse

STRINGIFY_JS =
File.read(File.join(JAVASCRIPTS_PATH, "stringify_object.js"))

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.filepathObject

Returns the value of attribute filepath


11
12
13
# File 'lib/winged_couch/views_loader.rb', line 11

def filepath
  @filepath
end

Class Method Details

.fetch(klass) ⇒ Hash

Returns hash with all defined views for specified class

Parameters:

  • klass (Class)

    WingedCouch model

Returns:

  • (Hash)

    in format { “view name” => { “map” => “function() { some(javascript); }” } }


22
23
24
25
26
27
28
29
30
31
# File 'lib/winged_couch/views_loader.rb', line 22

def fetch(klass)
  js_klass = "#{klass.name.delete('::')}Views"
  begin
    json = v8_context.eval("JSON.stringify(stringifyObject(#{js_klass}))")
    JSON.parse(json)
  rescue => e
    Exceptions::ViewsMissing.raise(js_klass) if e.message == "#{js_klass} is not defined"
    raise e
  end
end

.upload_views_for(klass) ⇒ Object

Uploads views for specified class to CouchDB

Parameters:

  • klass (Class)

    WingedCouch::Model


37
38
39
40
41
42
43
44
45
# File 'lib/winged_couch/views_loader.rb', line 37

def upload_views_for(klass)
  design_document = klass.design_document

  fetch(klass).each do |view_name, functions|
    functions.each do |function_name, function_code|
      Design::View.create(design_document, view_name, function_name, function_code)
    end
  end
end