Sha256: db16aa775d29121293c48ddf4afa7b5f6b376f047b4ad6fb149cfb363d6c009a

Contents?: true

Size: 941 Bytes

Versions: 6

Compression:

Stored size: 941 Bytes

Contents

class Module

  # Create an attribute method for both getting
  # and setting an instance variable.
  #
  #   attr_setter :a
  #
  # _is equivalent to_
  #
  #   def a(*args)
  #     if args.size > 0
  #       @a = args[0]
  #       self
  #     else
  #       @a
  #     end
  #   end
  #
  # CREDIT: Trans

  def attr_setter(*args)
    code, made = '', []
    args.each do |a|
      code << %{
        def #{a}(*args)
          args.size > 0 ? ( @#{a}=args[0] ; self ) : @#{a}
        end
      }
      made << "#{a}".to_sym
    end
    module_eval code
    made
  end

  # Alias an accessor. This create an alias for
  # both a reader and a writer.
  #
  #   class X
  #     attr_accessor :a
  #     alias_accessor :b, :a
  #   end
  #
  #   x = X.new
  #   x.b = 1
  #   x.a        #=> 1
  #
  # CREDIT: Trans

  def alias_setter(*args)
    args = args - [orig]
    args.each do |name|
      alias_method(name, orig)
    end
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/module/attr_setter.rb
facets-2.8.3 lib/core/facets/module/attr_setter.rb
facets-2.8.2 lib/core/facets/module/attr_setter.rb
facets-2.8.1 lib/core/facets/module/attr_setter.rb
facets-2.8.0 lib/core/facets/module/attr_setter.rb
facets-2.7.0 lib/core/facets/module/attr_setter.rb