Class: CouchCrumbs::View

Inherits:
Object
  • Object
show all
Includes:
Query
Defined in:
lib/couch_crumbs/view.rb

Overview

Based on the raw JSON that make up each view in a design doc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Query

#query_docs, #query_values

Constructor Details

#initialize(design, name, json) ⇒ View

Return or create a new view object



13
14
15
16
17
# File 'lib/couch_crumbs/view.rb', line 13

def initialize(design, name, json)
  self.name = name
  self.uri = File.join(design.uri, "_view", name)
  self.raw = JSON.parse(json)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/couch_crumbs/view.rb', line 9

def name
  @name
end

#rawObject

Returns the value of attribute raw.



9
10
11
# File 'lib/couch_crumbs/view.rb', line 9

def raw
  @raw
end

#uriObject

Returns the value of attribute uri.



9
10
11
# File 'lib/couch_crumbs/view.rb', line 9

def uri
  @uri
end

Class Method Details

.advanced_json(template, opts = {}) ⇒ Object

Return an advanced view as a JSON hash template => path to a .json template opts => options to gsub into the template



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/couch_crumbs/view.rb', line 47

def self.advanced_json(template, opts = {})
  # Read the given template (strip newlines to avoid JSON parser errors)
  template = File.read(template).gsub(/(\n|\r|\t|\s{2,})/, '')
  
  # Sub in any opts
  opts.each do |key, value|
    template.gsub!(/\##{ key }/, value.to_s)
  end
  
  template
end

.create!(design, name, json) ⇒ Object

Create a new view and save the containing design doc



20
21
22
23
24
25
26
27
28
# File 'lib/couch_crumbs/view.rb', line 20

def self.create!(design, name, json)
  view = new(design, name, json)
  
  design.add_view(view)
  
  design.save!
  
  view
end

.simple_json(type, property) ⇒ Object

Return a view as a JSON hash



32
33
34
35
36
37
38
39
40
41
# File 'lib/couch_crumbs/view.rb', line 32

def self.simple_json(type, property)
  # Read the 'simple' template (stripping newlines and tabs)
  template = File.read(File.join(File.dirname(__FILE__), "json", "simple.json")).gsub!(/(\n|\r|\t)/, '')

  template.gsub!(/\#name/, property.to_s.downcase)
  template.gsub!(/\#crumb_type/, type.to_s)
  template.gsub!(/\#property/, property.to_s.downcase)
  
  template
end

Instance Method Details

#has_reduce?Boolean

Return true if this view will reduce values

Returns:

  • (Boolean)


67
68
69
# File 'lib/couch_crumbs/view.rb', line 67

def has_reduce?      
  raw[raw.keys.first].has_key?("reduce")
end

#hashObject

Return a unique hash of the raw json



61
62
63
# File 'lib/couch_crumbs/view.rb', line 61

def hash
  raw.hash
end