Created
November 13, 2018 21:00
-
-
Save blairanderson/6c87b500412fe28e8772a3ea80044534 to your computer and use it in GitHub Desktop.
Ruby Module with Class and Instance methods extended
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
module Persistence | |
def self.included(klass) | |
klass.extend(ClassMethods) | |
end | |
module ClassMethods | |
def all | |
puts 'all' | |
end | |
def find(id) | |
puts "looking for entity with id=#{id}" | |
end | |
end | |
def save | |
puts 'saving' | |
end | |
def update | |
puts 'updating entity' | |
end | |
end | |
class User | |
include Persistence | |
end | |
u = User.new | |
# instance methods | |
u.save #=> saving | |
u.update #=> updating entity | |
# class methods | |
User.all #=> all | |
User.find(1) #=> looking for entity with id=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment