#-- # Author:: Tyler Rick # Copyright:: Copyright (c) Thomas Sawyer, probably, since this is a derivative work # License:: Ruby License # Submit to Facets?:: Yes # Developer notes:: # * Based on /usr/lib/ruby/gems/1.8/gems/facets-1.8.54/lib/facets/core/module/attr_tester.rb # * Hey Thomas, don't you think Module#attr_tester should only create the read-only a? method and have another method that creates the writer (like there how we have attr_reader, _writer, and _accessor?) ? "tester" does not imply "setter" in my mind... # * I would rename this one to bool_attr_accessor, which calls both bool_attr_reader and bool_attr_writer, but also lets you just create a reader or writer if you so desire. #++ class Module # This creates two methods for each given variable name. One is used to test # the attribute and the other is used to set or toggle it. # # bool_attr_accessor :a # # is equivalent to # # def self.a? # @@a ? true : @@a # end # # def self.a!(switch=Exception) # if switch == Exception # @@a = !@@a # else # @@a = switch ? true : false # self # end # end # # Works for both classes and modules. # def mbool_attr_accessor(*args) make = {} args.each { |a| # Initialize it first so that we won't have any NameErrors. module_eval %{ @@#{a} = nil if !defined?(@@#{a}) }, __FILE__, __LINE__ make["#{a}?".to_sym] = %{ def self.#{a}?(true_value=true) @@#{a} ? true_value : @@#{a} end } make["#{a}!".to_sym] = %{ def self.#{a}!(switch=Exception) if switch == Exception @@#{a} = !@@#{a} else @@#{a} = switch ? true : false self end end } } module_eval make.values.join("\n"), __FILE__, __LINE__ make.keys end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' require 'rubygems' require 'qualitysmith_extensions/object/ignore_access' require 'set' require 'qualitysmith_extensions/module/bool_attr_accessor' # Simply to show a comparison of mbool_attr_accessor and using class<