Module: CouchRest::Attributes

Extended by:
Forwardable
Included in:
Document
Defined in:
lib/couchrest/attributes.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) [](key)



34
35
36
# File 'lib/couchrest/attributes.rb', line 34

def [](key)
  _attributes[key.to_s]
end

- (Object) []=(key, value)



31
32
33
# File 'lib/couchrest/attributes.rb', line 31

def []=(key, value)
  _attributes[key.to_s] = value
end

- (Object) as_couch_json

Provide JSON data hash that can be stored in the database. Will go through each attribute value and request the `as_couch_json` method on each if available, or return the value as-is.



60
61
62
# File 'lib/couchrest/attributes.rb', line 60

def as_couch_json
  _attributes.inject({}) {|h, (k,v)| h[k] = v.respond_to?(:as_couch_json) ? v.as_couch_json : v; h}
end

- (Object) clone



48
49
50
51
52
# File 'lib/couchrest/attributes.rb', line 48

def clone
  new = super
  @_attributes = @_attributes.dup
  new
end

- (Object) delete(key)



40
41
42
# File 'lib/couchrest/attributes.rb', line 40

def delete(key)
  _attributes.delete(key.to_s)
end

- (Object) dup



43
44
45
46
47
# File 'lib/couchrest/attributes.rb', line 43

def dup
  new = super
  @_attributes = @_attributes.dup
  new
end

- (Object) freeze

Freeze the object's attributes instead of the actual document. This prevents further modifications to stored data, but does allow access to local variables useful for callbacks or cached data.



67
68
69
# File 'lib/couchrest/attributes.rb', line 67

def freeze
  _attributes.freeze; self
end

- (Boolean) has_key?(key)

Returns:

  • (Boolean)


37
38
39
# File 'lib/couchrest/attributes.rb', line 37

def has_key?(key)
  _attributes.has_key?(key.to_s)
end

- (Object) initialize(attrs = nil)

Initialize a new CouchRest Document and prepare a hidden attributes hash.

When inherting a Document, it is essential that the super method is called before you own changes to ensure that the attributes hash has been initialized before you attempt to use it.



22
23
24
# File 'lib/couchrest/attributes.rb', line 22

def initialize(attrs = nil)
  attrs.each{|k,v| self[k] = v} unless attrs.nil?
end

- (Object) inspect

Provide details of the current keys in the reponse. Based on ActiveRecord::Base.



72
73
74
75
76
77
# File 'lib/couchrest/attributes.rb', line 72

def inspect
  attributes_as_nice_string = self.keys.collect { |key|
    "#{key}: #{self[key].inspect}"
  }.compact.join(", ")
  "#<#{self.class} #{attributes_as_nice_string}>"
end

- (Object) to_hash



53
54
55
# File 'lib/couchrest/attributes.rb', line 53

def to_hash
  _attributes
end