Skip to content

Instantly share code, notes, and snippets.

@tdg5
Last active August 29, 2015 13:55
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 tdg5/8710022 to your computer and use it in GitHub Desktop.
Save tdg5/8710022 to your computer and use it in GitHub Desktop.
Mildly insane examples of overriding the reader method of the ActiveSupport provided :class_attribute directly and as part of an ActiveSupport::Concern.
class Feline
class_attribute :meow
self.meow = '<feline-silence/>'
def self.meow
raise NotImplementedError
end
end
class MetaCat < Feline
end
metacat = MetaCat.new
class Cat < Feline
self.meow = 'meow!'
end
class Lion < Cat
self.meow = 'roar!'
end
simba = Lion.new
simba.meow = 'Hakuna Matata'
module FelineMutagen
extend ActiveSupport::Concern
included do
class_attribute :meow
self.meow = 'The inner thoughts of cats'
def self.meow
'^^30\/\/'
end
end
module ClassMethods
def meow
# Class methods are added before included block is executed so...
raise ErrorThatDoesNotExistBecauseThisCodeWillNotBeCalled
end
end
end
class ComicbookFeline
include FelineMutagen
end
class Catwoman < ComicbookFeline
self.meow = 'DC Comics is better than Marvel.'
end
anne_hathaway = Catwoman.new
anne_hathaway.meow = 'Seriously.'
[Feline, MetaCat, metacat].each_with_index do |kitty, index|
case index
when 0
print "Felines are an abstract class, so their meow says \""
when 1
print "The same goes for MetaCat: \""
when 2
print "And an instance of MetaCat: \""
end
begin
kitty.meow
rescue NotImplementedError => e
puts "#{e.message}\""
end
end
puts "but the Cat class says \"#{Cat.meow}\""
puts "so Cat instances say \"#{Cat.new.meow}\""
puts "a Lion is kind of like a Cat, but it says \"#{Lion.meow}\""
puts "Except for Simba, he says \"#{simba.meow}\""
puts "\nBut sometimes in the comic books some kind of mutagen agent makes a feline of a non-feline. What then?"
puts "Feline mutants as a class might say \"#{ComicbookFeline.meow}\""
puts "So an instance of a generic feline mutant would say \"#{ComicbookFeline.new.meow}\""
puts "But Catwoman is not generic, so her class says \"#{Catwoman.meow}\""
puts "And a Catwoman instance (I'm thinking Anne Hathaway) says \"#{anne_hathaway.meow}\""
puts "\nBut the inner thoughts of Comicbook Felines? We may never know."
# =>
# Felines are an abstract class, so their meow says "NotImplementedError"
# The same goes for MetaCat: "NotImplementedError"
# And an instance of MetaCat: "NotImplementedError"
# but the Cat class says "meow!"
# so Cat instances say "meow!"
# a Lion is kind of like a Cat, but it says "roar!"
# Except for Simba, he says "Hakuna Matata"
#
# But sometimes in the comic books some kind of mutagen agent makes a feline of a non-feline. What then?
# Feline mutants as a class might say "^^30\/\/"
# So an instance of a generic feline mutant would say "^^30\/\/"
# But Catwoman is not generic, so her class says "DC Comics is better than Marvel."
# And a Catwoman instance (I'm thinking Anne Hathaway) says "Seriously."
#
# But the inner thoughts of Comicbook Felines? We may never know.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment