Last active
August 29, 2015 14:02
-
-
Save gabrieljoelc/ef2510172c7114149400 to your computer and use it in GitHub Desktop.
Overkill but interesting learning experience for me about metaprogramming in Ruby.
This file contains hidden or 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 ActsAsOwned | |
def acts_as_owned(options = {}) | |
raise 'Must define owner lambda' unless options[:owner] && options[:owner].class == Proc | |
define_method(:owner, &options[:owner]) | |
end | |
end | |
ActiveRecord::Base.send :extend, ActsAsOwned |
This file contains hidden or 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
require 'test_helper' | |
describe 'ActsAsOwned' do | |
before do | |
class ActsAsOwned::Mocker | |
extend ActsAsOwned | |
end | |
end | |
it 'when owner not specified must throw' do | |
#User.methods(true).any? { |m| m == :owner }.must_equal false | |
#user = FactoryGirl.build(:user) | |
mocker = ActsAsOwned::Mocker | |
proc { mocker.acts_as_owned }.must_raise RuntimeError | |
end | |
it 'when owner not Proc must throw' do | |
#User.methods(true).any? { |m| m == :owner }.must_equal false | |
#user = FactoryGirl.build(:user) | |
mocker = ActsAsOwned::Mocker | |
proc { mocker.acts_as_owned }.must_raise RuntimeError | |
end | |
describe 'when the owner is specified' do | |
before do | |
class ActsAsOwned::Mocker | |
TEST_VALUE = SecureRandom.base64 | |
acts_as_owned owner: -> { TEST_VALUE } | |
end | |
end | |
it 'owner must return value specified to acts_as_owned' do | |
ActsAsOwned::Mocker.new.owner.must_equal ActsAsOwned::Mocker::TEST_VALUE | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment