Method: Module#mbool_attr_reader

Defined in:
lib/quality_extensions/module/bool_attr_accessor.rb

#mbool_attr_reader(*args) ⇒ Object

This creates a reader method for a boolean (flag) class/module variable.

mbool_attr_reader :a

is equivalent to

def self.a?
  @@a ? true : @@a
end

Works for both classes and modules.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/quality_extensions/module/bool_attr_accessor.rb', line 149

def mbool_attr_reader(*args)
  options = (if args.last.is_a?(Hash) then args.pop else {} end)    # These options aren't used here, per se, but it allows us to have bool_attr_accessor pass along same args to both bool_attr_reader and bool_attr_setter.

  make = {}
  args.each { |a|
    make["#{a}?".to_sym] = %{
      def self.#{a}?(true_value=true)
        @@#{a} ? true_value : @@#{a}
      end
    }
  }
  module_eval make.values.join("\n"), __FILE__, __LINE__
  make.keys
end