Class: ShyCouch::Data::View
- Inherits:
-
Object
- Object
- ShyCouch::Data::View
- Defined in:
- lib/ShyCouch/data.rb
Constant Summary collapse
- JS_MAP_FUNCTION_HEADER =
"function ( doc ) { \n "
- JS_REDUCE_FUNCTION_HEADER =
"function(key, values, rereduce) { \n "
- JS_FUNCTION_FOOTER =
"}"
Instance Attribute Summary collapse
-
#map ⇒ Object
Returns the value of attribute map.
-
#name ⇒ Object
Returns the value of attribute name.
-
#reduce ⇒ Object
Returns the value of attribute reduce.
Instance Method Summary collapse
- #functions ⇒ Object
-
#initialize(view_name, &block) ⇒ View
constructor
A new instance of View.
Constructor Details
#initialize(view_name, &block) ⇒ View
Returns a new instance of View.
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/ShyCouch/data.rb', line 290 def initialize(view_name, &block) #O TODO - oh dear this is a nightmare @parser = ShyRubyJS::ShySexpParser.new sexp_check = block.to_sexp sexp = block.to_sexp(:strip_enclosure=>true) # make sure the two blocks inside are calls to "map" and "reduce" @name = view_name.to_s if sexp[0] == :block unless sexp_check[3][1][1][2] == :map and sexp_check[3][2][1][2] == :reduce raise ShyCouchError, "view must be called with map block and optional reduce block" end [1,2].each { |num| 2.times { sexp[num].delete_at(1) } } @map = JS_MAP_FUNCTION_HEADER + @parser.parse(sexp[1])[0] + JS_FUNCTION_FOOTER @reduce = JS_REDUCE_FUNCTION_HEADER + @parser.parse(sexp[2])[0] + JS_FUNCTION_FOOTER elsif sexp[0] == :iter raise ShyCouchError, "view must be called with map block and optional reduce block" unless sexp[1][2] == :map @map = JS_MAP_FUNCTION_HEADER + @parser.parse(sexp[3]) + JS_FUNCTION_FOOTER end end |
Instance Attribute Details
#map ⇒ Object
Returns the value of attribute map.
285 286 287 |
# File 'lib/ShyCouch/data.rb', line 285 def map @map end |
#name ⇒ Object
Returns the value of attribute name.
285 286 287 |
# File 'lib/ShyCouch/data.rb', line 285 def name @name end |
#reduce ⇒ Object
Returns the value of attribute reduce.
285 286 287 |
# File 'lib/ShyCouch/data.rb', line 285 def reduce @reduce end |
Instance Method Details
#functions ⇒ Object
314 315 316 |
# File 'lib/ShyCouch/data.rb', line 314 def functions return {"map" => @map, "reduce" => @reduce} end |