Module: Shanty::Mixins::AttrCombinedAccessor

Included in:
ProjectTemplate
Defined in:
lib/shanty/mixins/attr_combined_accessor.rb

Overview

Additional accessor utility

Instance Method Summary collapse

Instance Method Details

#attr_combined_accessor(*syms) ⇒ 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) or when trying to create a nice looking DSL.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/shanty/mixins/attr_combined_accessor.rb', line 11

def attr_combined_accessor(*syms)
  syms.each do |sym|
    define_method(sym) do |*args|
      if args.empty?
        instance_variable_get(:"@#{sym}")
      else
        instance_variable_set(:"@#{sym}", *args)
      end
    end
  end
end