Last active
May 30, 2016 18:50
-
-
Save Yorkshireman/6459bd8473a7e86bfa0e90a8f38fc4fd to your computer and use it in GitHub Desktop.
Neat Class.all pattern
This file contains 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
class Team | |
TEAMS = [] | |
attr_reader :name | |
def initialize(params) | |
@name = params[:team][:name] | |
TEAMS << self | |
end | |
def self.all | |
TEAMS | |
end | |
end | |
# allows you to create multiple instances of the model and do Team.all in a rails-y way with no database. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm having trouble in my RSpec tests with this so have abandoned it for now; the constant was remaining populated throughout multiple examples and not 'resetting'. I couldn't reset it manually because Ruby won't do dynamic assignment (there is a hacky way around this but I don't like 'hacky'!), and I couldn't work out how to make the file reload (a 'require' in the before/each block didn't work), thinking that might have sorted it.