Class: CouchCrumbs::View
- Inherits:
-
Object
- Object
- CouchCrumbs::View
- 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
-
#name ⇒ Object
Returns the value of attribute name.
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
-
.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.
-
.create!(design, name, json) ⇒ Object
Create a new view and save the containing design doc.
-
.simple_json(type, property) ⇒ Object
Return a view as a JSON hash.
Instance Method Summary collapse
-
#has_reduce? ⇒ Boolean
Return true if this view will reduce values.
-
#hash ⇒ Object
Return a unique hash of the raw json.
-
#initialize(design, name, json) ⇒ View
constructor
Return or create a new view object.
Methods included from Query
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
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/couch_crumbs/view.rb', line 9 def name @name end |
#raw ⇒ Object
Returns the value of attribute raw.
9 10 11 |
# File 'lib/couch_crumbs/view.rb', line 9 def raw @raw end |
#uri ⇒ Object
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
67 68 69 |
# File 'lib/couch_crumbs/view.rb', line 67 def has_reduce? raw[raw.keys.first].has_key?("reduce") end |
#hash ⇒ Object
Return a unique hash of the raw json
61 62 63 |
# File 'lib/couch_crumbs/view.rb', line 61 def hash raw.hash end |