Module: CouchRest::Attributes
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#as_couch_json ⇒ Object
Provide JSON data hash that can be stored in the database.
- #clone ⇒ Object
- #delete(key) ⇒ Object
- #dup ⇒ Object
-
#freeze ⇒ Object
Freeze the object’s attributes instead of the actual document.
-
#has_key?(key) ⇒ Boolean
has_key? is deprecated for Hash, key? replaces it github.com/bbatsov/ruby-style-guide#hash-key.
-
#initialize(attrs = nil) ⇒ Object
Initialize a new CouchRest Document and prepare a hidden attributes hash.
-
#inspect ⇒ Object
Provide details of the current keys in the reponse.
-
#key?(key) ⇒ Boolean
key? is preferred over has_key?.
- #to_hash ⇒ Object
Instance Method Details
#[](key) ⇒ Object
34 35 36 |
# File 'lib/couchrest/attributes.rb', line 34 def [](key) _attributes[key.to_s] end |
#[]=(key, value) ⇒ Object
31 32 33 |
# File 'lib/couchrest/attributes.rb', line 31 def []=(key, value) _attributes[key.to_s] = value end |
#as_couch_json ⇒ Object
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.
66 67 68 |
# File 'lib/couchrest/attributes.rb', line 66 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 ⇒ Object
54 55 56 57 58 |
# File 'lib/couchrest/attributes.rb', line 54 def clone new = super @_attributes = @_attributes.dup new end |
#delete(key) ⇒ Object
46 47 48 |
# File 'lib/couchrest/attributes.rb', line 46 def delete(key) _attributes.delete(key.to_s) end |
#dup ⇒ Object
49 50 51 52 53 |
# File 'lib/couchrest/attributes.rb', line 49 def dup new = super @_attributes = @_attributes.dup new end |
#freeze ⇒ Object
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.
73 74 75 |
# File 'lib/couchrest/attributes.rb', line 73 def freeze _attributes.freeze; self end |
#has_key?(key) ⇒ Boolean
has_key? is deprecated for Hash, key? replaces it github.com/bbatsov/ruby-style-guide#hash-key
43 44 45 |
# File 'lib/couchrest/attributes.rb', line 43 def has_key?(key) key?(key) end |
#initialize(attrs = nil) ⇒ Object
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 |
#inspect ⇒ Object
Provide details of the current keys in the reponse. Based on ActiveRecord::Base.
78 79 80 81 82 83 |
# File 'lib/couchrest/attributes.rb', line 78 def inspect attributes_as_nice_string = self.keys.collect { |key| "#{key}: #{self[key].inspect}" }.compact.join(", ") "#<#{self.class} #{attributes_as_nice_string}>" end |
#key?(key) ⇒ Boolean
key? is preferred over has_key?
38 39 40 |
# File 'lib/couchrest/attributes.rb', line 38 def key?(key) _attributes.has_key?(key.to_s) end |
#to_hash ⇒ Object
59 60 61 |
# File 'lib/couchrest/attributes.rb', line 59 def to_hash _attributes end |