Module: Loom::CoreExt::ModuleExt

Defined in:
lib/loom/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#loom_accessor(*names) ⇒ Object

A method like :attr_accessor, except the setter and getter methods are combined into one. It acts as a setter if an argument is passed, and always returns the currnet value.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/loom/core_ext.rb', line 21

def loom_accessor(*names)
  names.each do |name|
    instance_variable_set "@#{name}", nil

    define_method name do |*args|
      raise 'expected 0..1 arguments' if args.size > 1
      instance_variable_set("@#{name}", args.first) if args.size > 0
      instance_variable_get "@#{name}"
    end
  end
end