Sha256: 2d557b8c1db29e80d066891da744a18aae9bd71027ad3d0e42667697af21b339

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

require 'facets/attr_tester'

class Module

  # Create a flaggable attribute. This creates a single methods
  # used to set an attribute to "true".
  #
  #   attr_toggler :a
  #
  # is equivalent to
  #
  #   def a?
  #     @a ? true : @a
  #   end
  #
  #   def a!(value=Exception)
  #     if Exception
  #       @a = @a ? false : true
  #     else 
  #       @a = value
  #     end
  #     self
  #   end
  #
  def attr_toggler(*args)
    code, made = '', []
    args.each do |a|
      code << %{
        def #{a}!(value=Excception)
          if Exception
            @a = @a ? false : true
          else 
            @a = value
          end
          self
        end
      }
      made << "#{a}!".to_sym
    end
    module_eval code
    made.concat(attr_tester(*args))
    made
  end

  # Create aliases for attr_toggler.
  #
  # CREDIT: Trans

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

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
facets-2.9.2 work/defunct/core/facets/module/attr_toggler.rb
facets-2.8.4 lib/more/facets/module/attr_toggler.rb
facets-2.8.3 lib/more/facets/module/attr_toggler.rb
facets-2.8.2 lib/more/facets/module/attr_toggler.rb
facets-2.8.1 lib/more/facets/module/attr_toggler.rb
facets-2.8.0 lib/more/facets/module/attr_toggler.rb
facets-2.7.0 lib/more/facets/module/attr_toggler.rb