lib/couchpillow/document.rb in couchpillow-0.3.0 vs lib/couchpillow/document.rb in couchpillow-0.3.1
- old
+ new
@@ -11,20 +11,26 @@
@type = "default"
PRESENCE_LAMBDA = lambda do |value| !value.nil? end
+ RESERVED_KEYS = %i[_id _type created_at updated_at]
+
def initialize hash = {}, id = SecureRandom.hex
@data = self.class.symbolize(hash)
@id = id
@data[:created_at] and
+ @data[:created_at].is_a? String and
@data[:created_at] = Time.parse(@data[:created_at]) or
@data[:created_at] = Time.now.utc
- @data[:updated_at] = Time.parse(@data[:updated_at]) if @data[:updated_at]
+ @data[:updated_at] and
+ @data[:updated_at].is_a? String and
+ @data[:updated_at] = Time.parse(@data[:updated_at])
+
raise TypeError if @data[:_type] && @data[:_type] != self.class._type
@data[:_type] = self.class._type
rename!
end
@@ -139,9 +145,12 @@
# Rename an existing key to a new key. This is invoked right after initialize.
#
def self.rename from, to
+ raise ArgumentError, "Cannot rename reserved keys" if
+ RESERVED_KEYS.include?(from) || RESERVED_KEYS.include?(to)
+
rename_keys << [from.to_s.to_sym, to.to_s.to_sym]
end
# Validate the presence of a particular key, and the value of that key