How to extend DataMapper::Resource with custom method

心不动则不痛 提交于 2019-12-04 13:14:26

Andrew,

You can think of DataMapper::Resource as the instance (a row) and of DataMapper::Model as the class (a table). Now to alter the default capabilities at either the resource or the model level, you can either append inclusions or extensions to your model.

First you will need to wrap your #paginate method in a module. I've also added a probably useless #page method to show how to append to a resource in case you ever need to.

module Pagination
  module ClassMethods
    def paginate(page)
      # ...
    end
  end
  module InstanceMethods
    def page
      # ...
    end
  end
end

In your case, you want #paginate to be available on the model, so you would do:

DataMapper::Model.append_extensions(Pagination::ClassMethods)

If you find yourself in need of adding default capabilities to every resource, do:

DataMapper::Model.append_inclusions(Pagination::InstanceMethods)

Hope that helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!