Class: WingedCouch::Design::View

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

Overview

Class for working with design views

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(design_document, view_name) ⇒ View

Returns a new instance of View


13
14
15
# File 'lib/winged_couch/design/view.rb', line 13

def initialize(design_document, view_name)
  @design_document, @view_name = design_document, view_name
end

Instance Attribute Details

#design_documentObject (readonly)

Returns the value of attribute design_document


10
11
12
# File 'lib/winged_couch/design/view.rb', line 10

def design_document
  @design_document
end

#view_nameObject (readonly)

Returns the value of attribute view_name


10
11
12
# File 'lib/winged_couch/design/view.rb', line 10

def view_name
  @view_name
end

Class Method Details

.create(design_document, view_name, view_type, text) ⇒ WingedCouch::Design::View

Creates design view

Parameters:

  • design_document (WingedCouch::Design::Document)
  • view_name (String)

    name of name

  • view_type (String)

    “map” or “reduce”

  • text (String)

    source code of view

Returns:


45
46
47
48
49
50
# File 'lib/winged_couch/design/view.rb', line 45

def create(design_document, view_name, view_type, text)
  data = { views: { view_name =>  { view_type => text } } }
  design_document.data.deep_merge!(data)
  design_document.save
  self.new(design_document, view_name)
end

Instance Method Details

#==(other) ⇒ Object


61
62
63
64
65
# File 'lib/winged_couch/design/view.rb', line 61

def ==(other)
  other.is_a?(self.class) &&
    other.view_name == view_name &&
    other.design_document == design_document
end

#inspectObject


56
57
58
# File 'lib/winged_couch/design/view.rb', line 56

def inspect
  %Q{#<#{self.class} name="#{view_name}", database="#{database.name}">}
end

#sourceHash

Returns source code of view

Returns:

  • (Hash)

    in format { “map” => “function(doc) { … }”, “reduce” => “…” }


21
22
23
# File 'lib/winged_couch/design/view.rb', line 21

def source
  design_document.data.fetch(:views, {})[view_name]
end

#strategyObject


26
27
28
29
30
31
32
# File 'lib/winged_couch/design/view.rb', line 26

def strategy
  case source.keys
  when ["map"]           then "view:map"
  when ["map", "reduce"] then "view:map:reduce"
  else "unknown"
  end
end