Class: CouchPotato::View::ModelViewSpec::JavascriptGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_potato/view/model_view_spec.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, klass) ⇒ JavascriptGenerator

Returns a new instance of JavascriptGenerator.



77
78
79
80
# File 'lib/couch_potato/view/model_view_spec.rb', line 77

def initialize(options, klass)
  @options = options
  @klass = klass
end

Instance Method Details

#formatted_key(_key = nil) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/couch_potato/view/model_view_spec.rb', line 98

def formatted_key(_key = nil)
  _key ||= @options[:key]
  if _key.is_a? Array
    '[' + _key.map{|key_part| formatted_key(key_part)}.join(', ') + ']'
  else
    "doc['#{_key}']"
  end
end

#map_body(&block) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/couch_potato/view/model_view_spec.rb', line 82

def map_body(&block)
  <<-JS
  function(doc) {
    if(doc.#{JSON.create_id} && doc.#{JSON.create_id} == '#{@klass.name}'#{conditions}) {
      #{yield}
    }
  }
  JS
end

#map_functionObject



92
93
94
95
96
# File 'lib/couch_potato/view/model_view_spec.rb', line 92

def map_function
  map_body do
    "emit(#{formatted_key}, #{emit_value});"
  end
end