Created
May 6, 2012 18:19
-
-
Save ifesdjeen/2623624 to your computer and use it in GitHub Desktop.
metaprogramming magic
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 SafeConstDefinition | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def const_missing(name) | |
puts "Warning: #{name} was not defined, assuming further definition. Keep calm tho." | |
# Since Parent is defined after Child both in root and in SomeModule, | |
# i need to have a mechanism to define a future const, but simple: | |
# const_set(name, Class.new) | |
# doesn't do the thing, since it defines the constant within the class | |
# rather than enclosing module | |
end | |
end | |
included do | |
extend SafeConstDefinition::ClassMethods | |
end | |
end | |
module SomeModule | |
class Child | |
include SafeConstDefinition | |
attribute :parent, Parent | |
end | |
class Parent | |
include SafeConstDefinition | |
attribute :child, Child | |
end | |
end | |
class Child | |
include SafeConstDefinition | |
attribute :parent, Parent | |
end | |
class Parent | |
include SafeConstDefinition | |
attribute :child, Child | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems that
did the trick!