Sha256: 752304b9f58e6cb0cc59caab4850d6165de86748e76c8feb9e3af014b7b9d19a
Contents?: true
Size: 1.05 KB
Versions: 34
Compression:
Stored size: 1.05 KB
Contents
module Inch module Utils # 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" # module ReadWriteMethods # Implements a read method that acts as writer if called with a value # # @return [void] def rw_method(name) class_eval ''" def #{name}(value = nil) if value.nil? @#{name} else @#{name} = value end end "'' end # Implements multiple read(write) methods with the given +names+ # # @param names [Array<String,Symbol>] # @return [void] def rw_methods(*names) [names].flatten.each { |name| rw_method(name) } end end end end
Version data entries
34 entries across 34 versions & 1 rubygems