Module: Inch::Utils::ReadWriteMethods

Included in:
Evaluation::Grade, Evaluation::ObjectSchema, Evaluation::PriorityRange
Defined in:
lib/inch/utils/read_write_methods.rb

Overview

Extend a class with ReadWriteMethods and you will gain the rw_methods macro.

class User
  extend ReadWriteMethods
  rw_methods :name, :admin
end

This implements read methods that act as writer methods when called with a value parameter:

user = User.new
user.name # => nil
user.name "Adam"
user.name # => "Adam"

Instance Method Summary collapse

Instance Method Details

#rw_method(name) ⇒ void

This method returns an undefined value.

Implements a read method that acts as writer if called with a value



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/inch/utils/read_write_methods.rb', line 23

def rw_method(name)
  class_eval """
    def #{name}(value = nil)
      if value.nil?
        @#{name}
      else
        @#{name} = value
      end
    end
  """
end

#rw_methods(*names) ⇒ void

This method returns an undefined value.

Implements multiple read(write) methods with the given names

Parameters:

  • names (Array<String,Symbol>)


39
40
41
# File 'lib/inch/utils/read_write_methods.rb', line 39

def rw_methods(*names)
  [names].flatten.each { |name| rw_method(name) }
end