Skip to content

Instantly share code, notes, and snippets.

@gbuesing
Created June 14, 2010 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbuesing/438433 to your computer and use it in GitHub Desktop.
Save gbuesing/438433 to your computer and use it in GitHub Desktop.
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