Files

CouchRest::Attributes

Public Class Methods

new(attrs = nil) click to toggle source

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.

# File lib/couchrest/attributes.rb, line 22
def initialize(attrs = nil)
  attrs.each{|k,v| self[k] = v} unless attrs.nil?
end

Public Instance Methods

[](key) click to toggle source
# File lib/couchrest/attributes.rb, line 34
def [](key)
  _attributes[key.to_s]
end
[]=(key, value) click to toggle source
# File lib/couchrest/attributes.rb, line 31
def []=(key, value)
  _attributes[key.to_s] = value
end
as_couch_json() click to toggle source

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.

# 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
clone() click to toggle source
# File lib/couchrest/attributes.rb, line 48
def clone
  new = super
  @_attributes = @_attributes.dup
  new
end
delete(key) click to toggle source
# File lib/couchrest/attributes.rb, line 40
def delete(key)
  _attributes.delete(key.to_s)
end
dup() click to toggle source
# File lib/couchrest/attributes.rb, line 43
def dup
  new = super
  @_attributes = @_attributes.dup
  new
end
freeze() click to toggle source

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.

# File lib/couchrest/attributes.rb, line 67
def freeze
  _attributes.freeze; self
end
has_key?(key) click to toggle source
# File lib/couchrest/attributes.rb, line 37
def has_key?(key)
  _attributes.has_key?(key.to_s)
end
inspect() click to toggle source

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

# 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
to_hash() click to toggle source
# File lib/couchrest/attributes.rb, line 53
def to_hash
  _attributes
end

Protected Instance Methods

_attributes() click to toggle source

Define accessor for _attributes hash that will instantiate the model if this has not already been done.

# File lib/couchrest/attributes.rb, line 83
def _attributes
  @_attributes ||= {}
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.