Created
March 6, 2017 18:42
-
-
Save joecorcoran/63605bde9e3c88ff59f401a02e2651d1 to your computer and use it in GitHub Desktop.
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
# Simple user class | |
# We're pretending this is an ActiveRecord::Base model | |
class User | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
end | |
# Controller class | |
# We're pretending this is an ActionController::Base | |
class Controller | |
def self.before_action(lmb = ->{}) | |
@@before_actions ||= [] | |
@@before_actions << lmb | |
end | |
def run_before_actions!(context) | |
@@before_actions.each do |a| | |
context.instance_exec(&a) | |
end | |
end | |
end | |
# Our own controller | |
# This is like when we make a new controller in a Rails app | |
class UsersController < Controller | |
before_action -> { puts @user.name } | |
def create | |
@user = User.new('Joe') | |
run_before_actions!(self) | |
end | |
end | |
# We make a new instance of our controller and call the #create action | |
controller = UsersController.new | |
controller.create |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment