Class: Module
- Inherits:
-
Object
- Object
- Module
- Defined in:
- lib/nxt/patches/module.rb
Overview
Some patches that extend the default Ruby Module with some useful methods.
Instance Method Summary collapse
-
#attr_combined_accessor(sym, default = nil) ⇒ Object
Creates an invariant accessor that allows getting and setting from the same endpoint.
Instance Method Details
#attr_combined_accessor(sym, default = nil) ⇒ Object
Creates an invariant accessor that allows getting and setting from the same endpoint. It will operate in getter mode if you don’t pass any arguments when calling it, otherwise it will work in setter mode. Useful when needing to chain methods (you can’t chain standard attr_writer methods because of the ‘= something` part).
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/nxt/patches/module.rb', line 10 def attr_combined_accessor(sym, default = nil) define_method(sym) do |*args| if args.empty? instance_var = :"@#{sym}" if (value = instance_variable_get(instance_var)) value else instance_variable_set(instance_var, default) default end else send(:"#{sym}=", *args) end end end |