Class: Couch::Document

Inherits:
Object show all
Extended by:
DocExtensions
Includes:
Log
Defined in:
lib/couch/document.rb

Direct Known Subclasses

View

Constant Summary

Constants included from Log

Log::QUIET

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DocExtensions

condition_keys, count, view

Constructor Details

#initialize(data = {}, dont_change = true) ⇒ Document

Returns a new instance of Document.



42
43
44
45
46
47
# File 'lib/couch/document.rb', line 42

def initialize(data = {}, dont_change = true)
  @attributes = data.dup
  self.id = @attributes.delete(:id) if @attributes[:id]

  @attributes[:type] = self.class.name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object (private)



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/couch/document.rb', line 129

def method_missing(sym, *args)
  if block_given?
    super
  elsif args.length == 0 && attributes.key?(sym)
    attributes[sym]
  elsif args.length == 1 && sym.to_s =~ /(.+)=$/
    attributes[$1.to_sym] = args.first
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



40
41
42
# File 'lib/couch/document.rb', line 40

def attributes
  @attributes
end

Class Method Details

.async(&block) ⇒ Object



12
13
14
# File 'lib/couch/document.rb', line 12

def self.async(&block)
  database.async(&block)
end

.create(*args) ⇒ Object



16
17
18
# File 'lib/couch/document.rb', line 16

def self.create(*args)
  new(*args).save
end

.databaseObject



4
5
6
# File 'lib/couch/document.rb', line 4

def self.database
  Couch.database
end

.find(id) ⇒ Object



34
35
36
37
38
# File 'lib/couch/document.rb', line 34

def self.find(id)
  database.read(id)
rescue Couch::Error404
  nil
end

.instantiate(data, target = nil) ⇒ Object

instantiate a Couch::Document from a Hash.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/couch/document.rb', line 21

def self.instantiate(data, target = nil)
  target_type_name = data[:type] || raise("Missing document type in #{data.inspect}")

  if target.nil?
    target_klass = target_type_name.constantize
    target = target_klass.new(data)
  elsif target.class.name != target_type_name
    raise "Type mismatch #{target.class.name} vs #{target_type_name}"
    target.update data
  end
  target
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/couch/document.rb', line 98

def changed?
  @original != @attributes
end

#databaseObject



8
9
10
# File 'lib/couch/document.rb', line 8

def database
  self.class.database
end

#destroyObject



102
103
104
105
# File 'lib/couch/document.rb', line 102

def destroy
  database.destroy self
  self
end

#dupObject



49
50
51
52
53
54
# File 'lib/couch/document.rb', line 49

def dup
  other = super
  other.instance_variable_set "@attributes", attributes.dup
  other.instance_variable_set "@original", @original.dup
  other
end

#idObject



58
# File 'lib/couch/document.rb', line 58

def id; @attributes[:_id]; end

#id=(id) ⇒ Object



59
# File 'lib/couch/document.rb', line 59

def id=(id); @attributes[:_id] = id; end

#inspectObject



107
108
109
# File 'lib/couch/document.rb', line 107

def inspect
  "<#{self.class.name}[#{self.id}] #{attributes.inspect}>"
end

#new_record?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/couch/document.rb', line 75

def new_record?
  rev.nil?
end

#reloadObject



79
80
81
82
83
84
85
# File 'lib/couch/document.rb', line 79

def reload
  raise if new_record?

  @attributes = database.read(self.id).attributes
  @original = attributes.dup
  self
end

#revObject



61
# File 'lib/couch/document.rb', line 61

def rev; @attributes[:_rev]; end

#saveObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/couch/document.rb', line 87

def save
  if new_record?
    database.create self
  elsif changed?
    database.update self
  end
  
  @original = attributes.dup
  self
end

#to_hashObject



63
64
65
66
67
68
69
70
# File 'lib/couch/document.rb', line 63

def to_hash
  r = @attributes.dup
  
  r.delete :_id
  r.delete :_rev
  r.delete :type
  r
end

#to_jsonObject



119
120
121
# File 'lib/couch/document.rb', line 119

def to_json
  attributes.to_json
end

#typeObject



56
# File 'lib/couch/document.rb', line 56

def type; attributes[:type]; end

#update(attributes) ⇒ Object



123
124
125
# File 'lib/couch/document.rb', line 123

def update(attributes)
  self.attributes.update attributes
end