Class: CouchPotato::View::FlexViewSpec
- Inherits:
-
Object
- Object
- CouchPotato::View::FlexViewSpec
- Defined in:
- lib/couch_potato/view/flex_view_spec.rb
Overview
A flexible view spec. It allows to either just define a key and option conditions like the model view spec or custom map/reduce functions. In addition, it returns a result object that allows convenient access to either the raw result, the keys, values, ids or docs. The result object can be extended with custom module, too. Examples: class Thing
module ResultsExt
def average_time
keys.sum / keys.size # can access other result methods
end
end
property :time
view :by_time, type: :flex, key: :time, extend_results: ResultsExt
view :by_custom_time, type: :flex,
reduce: '_sum'
map: <<~JS
function(doc) {
emit(doc.time, 1);
}
JS
end
usage: irb> result = db.view Thing.by_time irb> result.raw # raw CouchDB results irb> result.ids # ids of rows irb> result.keys # keys emitted in map function irb> result.values # values emitted in map function irb> result.average_time # custom method from ResultsExt module irb> db.view(Thing.by_time(include_docs: true)).docs # documents irb> db.view(Thing.by_time(reduce: true)).reduce_value # value of first row, i.e. result of the reduce function (without grouping)
Defined Under Namespace
Classes: Results
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
-
#initialize(klass, view_name, options, view_parameters) ⇒ FlexViewSpec
constructor
A new instance of FlexViewSpec.
- #process_results(results) ⇒ Object
Constructor Details
#initialize(klass, view_name, options, view_parameters) ⇒ FlexViewSpec
Returns a new instance of FlexViewSpec.
90 91 92 93 94 95 96 |
# File 'lib/couch_potato/view/flex_view_spec.rb', line 90 def initialize(klass, view_name, , view_parameters) @extend_results_module = [:extend_results] @klass = klass @view_name = view_name @options = .except(:extend_results) @view_parameters = view_parameters end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
39 40 41 |
# File 'lib/couch_potato/view/flex_view_spec.rb', line 39 def klass @klass end |