Created
April 23, 2015 12:20
-
-
Save jacoyutorius/584a03f3a4f87ed76068 to your computer and use it in GitHub Desktop.
~valid?メソッドが存在したら、それを反転したinvalid?メソッドを用意する
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 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