Skip to content

Instantly share code, notes, and snippets.

@jacoyutorius
Created April 23, 2015 12:20
Show Gist options
  • Select an option

  • Save jacoyutorius/584a03f3a4f87ed76068 to your computer and use it in GitHub Desktop.

Select an option

Save jacoyutorius/584a03f3a4f87ed76068 to your computer and use it in GitHub Desktop.
~valid?メソッドが存在したら、それを反転したinvalid?メソッドを用意する
module ValidInvalid
extend ActiveSupport::Concern
included do
# validメソッドが存在したら、その結果を反転させて返す
def method_missing(method, *args)
if method =~ /invalid?/
name_str = method.to_s
name_str.slice!("invalid?")
target_method = "#{name_str}valid?"
return nil unless self.class.method_defined? target_method
ret = eval("self.#{target_method}")
return nil if ret.nil?
!ret
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment