Class: CouchCrumbs::Design
- Inherits:
-
Object
- Object
- CouchCrumbs::Design
- Defined in:
- lib/couch_crumbs/design.rb
Overview
Represents “special” design documents
Instance Attribute Summary collapse
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#uri ⇒ Object
Returns the value of attribute uri.
Class Method Summary collapse
-
.get!(database, opts = {}) ⇒ Object
Return a single design doc (from a specific database).
Instance Method Summary collapse
-
#add_view(view) ⇒ Object
Append a view to the view list.
-
#destroy! ⇒ Object
Remove a design from the database.
-
#id ⇒ Object
Return design doc id (typically “_design/resource”).
-
#initialize(database, opts = {}) ⇒ Design
constructor
Instantiate a new design document.
-
#name ⇒ Object
Return the design name (id - prefix, i.e. “resource”).
-
#rev ⇒ Object
Return the revision.
-
#save! ⇒ Object
Save the design to the database.
-
#views(opts = {}) ⇒ Object
Return all views of this design doc opts => supply a name to select a specific view.
Constructor Details
#initialize(database, opts = {}) ⇒ Design
Instantiate a new design document
Parameters
- database<String>
-
database instance
- opts => json<String>
-
raw json to init from
- opts => name<String>
-
design doc name (i.e. “person”)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/couch_crumbs/design.rb', line 44 def initialize(database, opts = {}) if opts.has_key?(:json) self.raw = JSON.parse(opts[:json]) elsif opts.has_key?(:name) # Read the design doc template template = File.read(File.join(File.dirname(__FILE__), "json", "design.json")) # Make our substitutions template.gsub!(/\#design_id/, "_design/#{ opts[:name] }") # Init the raw hash self.raw = JSON.parse(template) else raise "#new must have :json or a :name supplied" end # Set out unique URI self.uri = File.join(database.uri, id) end |
Instance Attribute Details
#raw ⇒ Object
Returns the value of attribute raw.
7 8 9 |
# File 'lib/couch_crumbs/design.rb', line 7 def raw @raw end |
#uri ⇒ Object
Returns the value of attribute uri.
7 8 9 |
# File 'lib/couch_crumbs/design.rb', line 7 def uri @uri end |
Class Method Details
.get!(database, opts = {}) ⇒ Object
Return a single design doc (from a specific database)
Parameters
- database<String>
-
database instance
- opts => name<String>
-
design doc name (i.e. “person”)
- opts => id<String
-
id of an existing design doc (i.e. “_design/person”)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/couch_crumbs/design.rb', line 16 def self.get!(database, opts = {}) raise "opts must contain an :id or :name" unless (opts.has_key?(:id) || opts.has_key?(:name)) # Munge the URI from an :id or :name uri = File.join(database.uri, (opts[:id] || "_design/#{ opts[:name] }")) begin # Try for an existing doc result = RestClient.get(uri) design = Design.new(database, :json => result) rescue RestClient::ResourceNotFound # Or create a new one design = Design.new(database, :name => File.basename(uri)) design.save! end design end |
Instance Method Details
#add_view(view) ⇒ Object
Append a view to the view list
119 120 121 122 123 124 125 |
# File 'lib/couch_crumbs/design.rb', line 119 def add_view(view) raise ArgumentError.new("view must be of kind View: #{ view }") unless view.kind_of?(View) raw["views"].merge!(view.raw) save! end |
#destroy! ⇒ Object
Remove a design from the database
96 97 98 99 100 101 102 |
# File 'lib/couch_crumbs/design.rb', line 96 def destroy! freeze result = JSON.parse(RestClient.delete(File.join(uri, "?rev=#{ rev }"))) result["ok"] end |
#id ⇒ Object
Return design doc id (typically “_design/resource”)
66 67 68 |
# File 'lib/couch_crumbs/design.rb', line 66 def id raw["_id"] end |
#name ⇒ Object
Return the design name (id - prefix, i.e. “resource”)
78 79 80 |
# File 'lib/couch_crumbs/design.rb', line 78 def name raw["_id"].split("/").last end |
#rev ⇒ Object
Return the revision
72 73 74 |
# File 'lib/couch_crumbs/design.rb', line 72 def rev raw["_rev"] end |
#save! ⇒ Object
Save the design to the database
84 85 86 87 88 89 90 91 92 |
# File 'lib/couch_crumbs/design.rb', line 84 def save! result = JSON.parse(RestClient.put(uri, raw.to_json)) # update our stats raw["_id"] = result["id"] raw["_rev"] = result["rev"] result["ok"] end |
#views(opts = {}) ⇒ Object
Return all views of this design doc opts => supply a name to select a specific view
107 108 109 110 111 112 113 114 115 |
# File 'lib/couch_crumbs/design.rb', line 107 def views(opts = {}) if opts.has_key?(:name) View.new(self, opts[:name].to_s, {:name => raw["views"][opts[:name].to_s]}.to_json) else raw["views"].collect do |key, value| View.new(self, key.to_s, {key => value}.to_json) end end end |