Created
June 14, 2010 22:45
-
-
Save gbuesing/438433 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_model' | |
class ActiveCouchModel | |
extend ActiveModel::Callbacks | |
include ActiveModel::Conversion | |
include ActiveModel::Naming | |
include ActiveModel::Validations | |
define_model_callbacks :save, :create | |
attr_accessor :attributes | |
def initialize(attrs = {}) | |
@attributes = {} | |
attrs.each do |k, v| | |
@attributes[k.to_s] = v | |
end | |
end | |
def [](key) | |
@attributes[key.to_s] | |
end | |
def []=(key, value) | |
@attributes[key.to_s] = value | |
end | |
def id | |
self['_id'] | |
end | |
def rev | |
self['_rev'] | |
end | |
def new? | |
!rev | |
end | |
alias_method :new_record?, :new? | |
def persisted? | |
!new? | |
end | |
def save | |
_run_save_callbacks do | |
if valid? | |
_run_create_callbacks do | |
puts "saving!" | |
"OK!" | |
end | |
else | |
false | |
end | |
end | |
end | |
private | |
def read_attribute_for_validation(key) | |
@attributes[key.to_s] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment