Skip to content

Instantly share code, notes, and snippets.

@leejarvis
Created October 7, 2014 15:21
Show Gist options
  • Save leejarvis/e87d93c636d592590058 to your computer and use it in GitHub Desktop.
Save leejarvis/e87d93c636d592590058 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'set'
class User
extend Enumerable
def self.all
@all ||= Set.new
end
def self.create(attrs = {})
all << new(attrs)
end
def self.each(&block)
all.each(&block)
end
attr_reader :attributes
def initialize(attributes = {})
@attributes = attributes
end
def method_missing(m, *args)
attributes[m] || super
end
end
User.create name: "Lee"
p User.find { |u| u.name == "Lee" } #<User:0x007ff7539b4518 @attributes={:name=>"Lee"}>
p User.find { |u| u.name == "Bob" } #=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment