Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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