Sha256: 838b79807322e5a5fcc643ad4235b7c0f0f523004322ffb0e86cd1420af607b0

Contents?: true

Size: 779 Bytes

Versions: 6

Compression:

Stored size: 779 Bytes

Contents

class Module

  # Create an tester attribute. This creates a single method
  # used to test the attribute for truth.
  #
  #   attr_tester :a
  #
  # is equivalent to
  #
  #   def a?
  #     @a ? true : @a
  #   end
  #
  def attr_tester(*args)
    code, made = '', []
    args.each do |a|
      code << %{
        def #{a}?(truth=nil)
          @#{a} ? truth || @#{a} : @#{a}
        end
      }
      made << "#{a}?".to_sym
    end
    module_eval code
    made
  end

  alias_method :attr_reader?, :attr_tester

  # Create aliases for flag reader.
  #
  # CREDIT: Trans

  def alias_tester(*args)
    orig = args.last
    args = args - [orig]
    args.each do |name|
      alias_method("#{name}?", "#{orig}?")
    end
  end
  alias_method :alias_reader?, :alias_tester

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-2.8.4 lib/more/facets/module/attr_tester.rb
facets-2.8.3 lib/more/facets/module/attr_tester.rb
facets-2.8.2 lib/more/facets/module/attr_tester.rb
facets-2.8.1 lib/more/facets/module/attr_tester.rb
facets-2.8.0 lib/more/facets/module/attr_tester.rb
facets-2.7.0 lib/more/facets/module/attr_tester.rb