Skip to content

Instantly share code, notes, and snippets.

@steveh
Created December 3, 2010 01:14
Show Gist options
  • Select an option

  • Save steveh/726420 to your computer and use it in GitHub Desktop.

Select an option

Save steveh/726420 to your computer and use it in GitHub Desktop.
module Decorators
class Base < BasicObject
# perhaps make this include ActiveModel and behave like the underlying ActiveRecord
attr_reader :decorated
def initialize(decorated)
@decorated = decorated
end
def method_missing(method, *args)
args.empty? ? decorated.send(method) : decorated.send(method, args)
end
end
module Api
class User < Decorators::Base
def attributes=(attrs)
# filter out disallowed attributes
end
def as_json
{ :id => decorated.id }
end
end
end
end
user = User.find(123)
@user = Decorators::Api::User.new(user)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment