Skip to content

Instantly share code, notes, and snippets.

@neohunter
Last active November 5, 2017 04:22
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 neohunter/1304e3ebde7c937a987c8bb9fee83830 to your computer and use it in GitHub Desktop.
Save neohunter/1304e3ebde7c937a987c8bb9fee83830 to your computer and use it in GitHub Desktop.
instance class variables
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