Created
January 28, 2015 04:33
-
-
Save bradstewart/0248ce91c494a8f1f88d to your computer and use it in GitHub Desktop.
Model Decorator
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
## | |
# Abstract class designed to decorate ActiveRecord models with methods for | |
# acting on instances which are stored in both local and remote databases. | |
# | |
# @note Decorator instances should be initialized with an instance of the ActiveRecord | |
# model to be acted on. This instance is available within decorators as {#object}. | |
# | |
# @abstract Subclass and override {#create}, {#update}, {#destroy} to implement. | |
# | |
class AbstractDecorator < SimpleDelegator | |
# | |
# Class-level methods which simply delegate to a new instance of the decorator. | |
# | |
class << self | |
def create(target) | |
new(target).create | |
end | |
def update(target) | |
new(target).update | |
end | |
def destroy(target) | |
new(target).destroy | |
end | |
end | |
# | |
# Allow use of the cleaner {#object} instead of {#__getobj__} to reference the base record. | |
# | |
alias_method :object, :__getobj__ | |
# | |
# Create the {#object}. | |
# | |
# @abstract | |
# | |
# @return [Boolean] True if operation succeeds, False otherwise | |
# | |
def create | |
end | |
# | |
# Update the {#object}. | |
# | |
# @abstract | |
# | |
# @return [Boolean] True if operation succeeds, False otherwise | |
# | |
def update | |
end | |
# | |
# Destroy the {#object}. | |
# | |
# @abstract | |
# | |
# @return [Boolean] True if operation succeeds, False otherwise | |
# | |
def destroy | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment