Last active
December 23, 2015 08:19
-
-
Save spickermann/6606858 to your computer and use it in GitHub Desktop.
A decorator that can return faked data for defined fields
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
# depends on: https://github.com/masover/blankslate | |
# | |
# user = User.new(:name => 'foo') | |
# user.name #=> 'foo' | |
# Decorator.new(user).name #=> 'foo' | |
# | |
# but | |
# Decorator.new(user, :name => 'bar').name #=> 'bar' | |
class Decorator < BlankSlate | |
def initialize(record, fields = {}) | |
@record = record | |
@fields = (fields || {}).symbolize_keys | |
end | |
# restore default send method | |
def send(*args) | |
__send__(*args) | |
end | |
private | |
def method_missing(sym, *args, &block) | |
@fields[sym] || @record.send(sym, *args, &block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment