module AttrOptional::ClassMethods

Public Instance Methods

attr_optional(*keys) click to toggle source
# File lib/attr_optional.rb, line 16
def attr_optional(*keys)
  if defined? undef_required_attributes
    undef_required_attributes(*keys)
  end
  optional_attributes.concat(keys)
  attr_accessor(*keys)
end
attr_optional?(key) click to toggle source
# File lib/attr_optional.rb, line 24
def attr_optional?(key)
  optional_attributes.include?(key)
end
inherited(klass) click to toggle source
Calls superclass method
# File lib/attr_optional.rb, line 9
def inherited(klass)
  super
  unless optional_attributes.empty?
    klass.attr_optional(*optional_attributes)
  end
end
optional_attributes() click to toggle source
# File lib/attr_optional.rb, line 28
def optional_attributes
  @optional_attributes ||= []
end
undef_optional_attributes(*keys) click to toggle source
# File lib/attr_optional.rb, line 32
def undef_optional_attributes(*keys)
  keys.each do |key|
    if attr_optional?(key)
      undef_method key, :"#{key}="
      optional_attributes.delete key
    end
  end
end