Class: CouchModel::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_model/row.rb

Overview

The Row class acts as a wrapper for a CouchDB view result row.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = { }) ⇒ Row

Returns a new instance of Row.



13
14
15
# File 'lib/couch_model/row.rb', line 13

def initialize(attributes = { })
  @id, @key, @value, @document = attributes.values_at "id", "key", "value", "doc"
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



11
12
13
# File 'lib/couch_model/row.rb', line 11

def document
  @document
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/couch_model/row.rb', line 8

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/couch_model/row.rb', line 9

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/couch_model/row.rb', line 10

def value
  @value
end

Instance Method Details

#instanciate_model(model_class_name) ⇒ Object



25
26
27
28
29
30
# File 'lib/couch_model/row.rb', line 25

def instanciate_model(model_class_name)
  model_class = Object.const_get model_class_name
  model = model_class.new
  model.instance_variable_set :@attributes, @document
  model
end

#modelObject

Raises:

  • (StandardError)


17
18
19
20
21
22
23
# File 'lib/couch_model/row.rb', line 17

def model
  return nil unless @document && @document.has_key?(Configuration::CLASS_KEY)

  model_class_name = document[Configuration::CLASS_KEY]
  raise StandardError, "no class defined with name [#{model_class_name}]" unless Object.const_defined?(model_class_name)
  instanciate_model model_class_name
end