couchrest_model/lib/couchrest/model/designs.rb
2011-02-06 20:02:44 +01:00

63 lines
1.3 KiB
Ruby

#### NOTE Work in progress! Not yet used!
module CouchRest
module Model
# A design block in CouchRest Model groups together the functionality of CouchDB's
# design documents in a simple block definition.
#
# class Person < CouchRest::Model::Base
# property :name
# timestamps!
#
# design do
# view :by_name
# end
# end
#
module Designs
extend ActiveSupport::Concern
module ClassMethods
def design(*args, &block)
mapper = DesignMapper.new(self)
mapper.create_view_method(:all)
mapper.instance_eval(&block)
req_design_doc_refresh
end
end
#
class DesignMapper
attr_accessor :model
def initialize(model)
self.model = model
end
# Define a view and generate a method that will provide a new
# View instance when requested.
def view(name, opts = {})
View.create(model, name, opts)
create_view_method(name)
end
def create_view_method(name)
model.class_eval <<-EOS, __FILE__, __LINE__ + 1
def self.#{name}(opts = {})
CouchRest::Model::Designs::View.new(self, opts, '#{name}')
end
EOS
end
end
end
end
end