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

Version Path
inch-0.9.0.rc1 lib/inch/utils/read_write_methods.rb
inch-0.8.0 lib/inch/utils/read_write_methods.rb
inch-0.8.0.rc2 lib/inch/utils/read_write_methods.rb
inch-0.8.0.rc1 lib/inch/utils/read_write_methods.rb
inch-0.7.1 lib/inch/utils/read_write_methods.rb
inch-0.7.0 lib/inch/utils/read_write_methods.rb
inch-0.6.4 lib/inch/utils/read_write_methods.rb
inch-0.6.3 lib/inch/utils/read_write_methods.rb
inch-0.6.2 lib/inch/utils/read_write_methods.rb
inch-0.6.1 lib/inch/utils/read_write_methods.rb
inch-0.6.0 lib/inch/utils/read_write_methods.rb
inch-0.6.0.rc6 lib/inch/utils/read_write_methods.rb
inch-0.6.0.rc5 lib/inch/utils/read_write_methods.rb
inch-0.6.0.rc4 lib/inch/utils/read_write_methods.rb
inch-0.6.0.rc3 lib/inch/utils/read_write_methods.rb
inch-0.6.0.rc2 lib/inch/utils/read_write_methods.rb
inch-0.6.0.rc1 lib/inch/utils/read_write_methods.rb
inch-0.5.10 lib/inch/utils/read_write_methods.rb
inch-0.5.9 lib/inch/utils/read_write_methods.rb
inch-0.5.8 lib/inch/utils/read_write_methods.rb