Created
May 8, 2013 04:46
-
-
Save elskwid/5538254 to your computer and use it in GitHub Desktop.
Protomodel
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
# Example of prototyping models with a mix of ActiveRecord and Virtus | |
# | |
# Useful with large models where some fields are in flux during spiking/dev | |
# and you don't want or need to run migrations to try ideas out. | |
class A < ActiveRecord::Base | |
# ... | |
# belongs_to | |
# has_many | |
# ... | |
class Proto | |
include Virtus | |
attribute :amount, Float | |
attribute :name, String | |
attribute :foo, String | |
end | |
# helper | |
def proto | |
@proto ||= Proto.new | |
end | |
# delegate messages to the proto instance | |
def method_missing(method, *args, &block) | |
proto.respond_to?(method) ? proto.send(method, *args, &block) : super | |
end | |
# ensure the model quacks | |
def respond_to_missing?(method, include_private = false) | |
proto.respond_to?(method, include_private) || super | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment