Last active
November 5, 2017 04:22
-
-
Save neohunter/1304e3ebde7c937a987c8bb9fee83830 to your computer and use it in GitHub Desktop.
instance class variables
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 Validator | |
def self.included(klass) | |
klass.extend ClassMethods | |
end | |
module ClassMethods | |
def inherited(child_class) | |
child_class.instance_variable_set :@required_fields, self.required_fields | |
end | |
def add_required_fields(*fields) | |
@required_fields = required_fields + fields | |
end | |
def required_fields | |
@required_fields || [] | |
end | |
end | |
end | |
# so I can do something like | |
class MyBase | |
add_required_fields *%i(a b c) | |
end | |
class MyChild < MyBase | |
add_required_fields *%i(d e f) | |
end | |
# for MyChald result would be a b c d e f | |
class TheOtherChild < MyBase | |
add_required_fields *%i(g h i) | |
end | |
# for MyChald result would be a b c g h i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment