Class: Couchup::View
- Inherits:
-
Object
- Object
- Couchup::View
- Defined in:
- lib/couchup/view.rb
Constant Summary collapse
- MAP_TEMPLATE =
"function(doc){\n}\n"- REDUCE_TEMPLATE =
""
Class Method Summary collapse
- .create(name, map, reduce) ⇒ Object
- .create_from_file(name, file_contents) ⇒ Object
- .parse(file_contents) ⇒ Object
Instance Method Summary collapse
- #delete! ⇒ Object
-
#initialize(name) ⇒ View
constructor
A new instance of View.
- #map ⇒ Object
- #map=(fun) ⇒ Object
- #map? ⇒ Boolean
- #reduce ⇒ Object
- #reduce=(fun) ⇒ Object
- #reduce? ⇒ Boolean
- #save ⇒ Object
Constructor Details
#initialize(name) ⇒ View
Returns a new instance of View.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/couchup/view.rb', line 6 def initialize(name) @design, @name = name.split "/" begin @doc = Couchup.database.get("_design/#{@design}") rescue ap "Design #{@design} not found creating a new one" @doc = {"_id" => "_design/#{@design}", :language => 'javascript', :views => {}} save @doc = Couchup.database.get("_design/#{@design}") end end |
Class Method Details
.create(name, map, reduce) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/couchup/view.rb', line 65 def self.create(name, map, reduce) v = new(name) v.map = map v.reduce = reduce unless reduce.blank? ap v v.save end |
.create_from_file(name, file_contents) ⇒ Object
60 61 62 63 |
# File 'lib/couchup/view.rb', line 60 def self.create_from_file(name, file_contents) map, reduce = parse(file_contents) create(name, map, reduce) end |
.parse(file_contents) ⇒ Object
73 74 75 76 |
# File 'lib/couchup/view.rb', line 73 def self.parse(file_contents) file_contents =~ /-+BEGIN Map-+(.*)^-+END-+.*BEGIN Reduce.*-+(.*)^-+END-*/m [$1.strip, $2.strip] end |
Instance Method Details
#delete! ⇒ Object
54 55 56 57 |
# File 'lib/couchup/view.rb', line 54 def delete! @doc["views"].delete(@name) save end |
#map ⇒ Object
22 23 24 |
# File 'lib/couchup/view.rb', line 22 def map @doc["views"][@name]["map"] end |
#map=(fun) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/couchup/view.rb', line 26 def map=(fun) if map? @doc["views"][@name]["map"] = fun else @doc["views"][@name] = {:map => fun} end end |
#map? ⇒ Boolean
46 47 48 |
# File 'lib/couchup/view.rb', line 46 def map? @doc["views"][@name] && !@doc["views"][@name]["map"].blank? end |
#reduce ⇒ Object
34 35 36 |
# File 'lib/couchup/view.rb', line 34 def reduce @doc["views"][@name]["reduce"] end |
#reduce=(fun) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/couchup/view.rb', line 38 def reduce=(fun) if reduce? @doc["views"][@name]["reduce"] = fun else @doc["views"][@name] ={:reduce => fun} end end |
#reduce? ⇒ Boolean
50 51 52 |
# File 'lib/couchup/view.rb', line 50 def reduce? @doc["views"][@name] && !@doc["views"][@name]["reduce"].blank? end |
#save ⇒ Object
18 19 20 |
# File 'lib/couchup/view.rb', line 18 def save Couchup.database.save_doc(@doc) end |