Module: Resultify::ClassMethods
- Defined in:
- lib/resultify.rb
Instance Method Summary collapse
- #method_added(name) ⇒ Object
- #optionify(*method_names) ⇒ Object
- #overwrite_method_for_option(name) ⇒ Object
- #overwrite_method_for_result(name) ⇒ Object
- #resultify(*method_names) ⇒ Object
Instance Method Details
#method_added(name) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/resultify.rb', line 107 def method_added(name) return if defined?(@@overwriting_method) && @@overwriting_method if defined?(@@resultify_method_names) && @@resultify_method_names.include?(name) self.overwrite_method_for_result(name) end if defined?(@@optionify_method_names) && @@optionify_method_names.include?(name) self.overwrite_method_for_option(name) end end |
#optionify(*method_names) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/resultify.rb', line 99 def optionify(*method_names) @@optionify_method_names = method_names self.instance_methods(false).each do |mname| self.overwrite_method_for_option(mname) if @@optionify_method_names.include?(mname) end @@overwriting_method = false end |
#overwrite_method_for_option(name) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/resultify.rb', line 76 def overwrite_method_for_option(name) @@overwriting_method = true alias_method "old_#{name}".to_sym, name.to_sym define_method name do |*args, &block| old_method = self.class.instance_method("old_#{name}") ret_val = old_method.bind(self).call(*args, &block) if ret_val == nil || ret_val == "" return Option.new(ret_val) else return Option.new(nil) end end @@overwriting_method = false end |
#overwrite_method_for_result(name) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/resultify.rb', line 61 def overwrite_method_for_result(name) @@overwriting_method = true alias_method "old_#{name}".to_sym, name.to_sym define_method name do |*args, &block| begin old_method = self.class.instance_method("old_#{name}") ret_val = old_method.bind(self).call(*args, &block) return Result.new(ret_val, nil) rescue Exception => e return Result.new(nil, e) end end @@overwriting_method = false end |
#resultify(*method_names) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/resultify.rb', line 91 def resultify(*method_names) @@resultify_method_names = method_names self.instance_methods(false).each do |mname| self.overwrite_method_for_result(mname) if @@resultify_method_names.include?(mname) end @@overwriting_method = false end |