Skip to content

Instantly share code, notes, and snippets.

@Yorkshireman
Last active May 30, 2016 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yorkshireman/6459bd8473a7e86bfa0e90a8f38fc4fd to your computer and use it in GitHub Desktop.
Save Yorkshireman/6459bd8473a7e86bfa0e90a8f38fc4fd to your computer and use it in GitHub Desktop.
Neat Class.all pattern
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.
@Yorkshireman
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment