File: bool_attr_accessor.rb

package info (click to toggle)
ruby-facets 2.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,824 kB
  • sloc: ruby: 25,483; xml: 90; makefile: 20
file content (414 lines) | stat: -rw-r--r-- 10,923 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#--
# Author::    Thomas Sawyer, 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'm going to rename this one to bool_attr_accessor, which calls both bool_attr_reader and bool_attr_writer
# * Then you also have the option to use bool_attr_reader and bool_attr_writer separately if you so desire.
# * Other changes made:
#   * Changed it so that if you call a!(false) it would actually set @a to false rather than leaving it unchanged. (I assume that was a bug.)
#++

# bool_attr_accessor

class Module
  # This creates a reader method for a boolean (flag) attribute (instance variable).
  #
  #   bool_attr_reader :a
  #
  # is equivalent to
  #
  #   def a?
  #     @a ? true : @a
  #   end
  #
  # Example:
  #   class Foo
  #     def set_it
  #       @a = true
  #     end
  #   end
  #   x = Foo.new
  #   x.a?        # => false
  #   x.set_it
  #   x.a?        # => true
  #
  def bool_attr_reader(*args)
    make = {}
    args.each { |a|
      make["#{a}?".to_sym] = %{
        def #{a}?(true_value=true)
          @#{a} ? true_value : @#{a}
        end
      }
    }
    module_eval make.values.join("\n"), __FILE__, __LINE__

    make.keys
  end

  # This creates a setter method for a boolean (flag) attribute (instance variable).
  #
  #   bool_attr_setter :a
  #
  # is equivalent to
  #
  #   def a!(switch=Exception)
  #     if switch == Exception
  #       @a = !@a
  #     else
  #       @a = switch ? true : false
  #       self
  #     end
  #   end
  #
  # This setter method can either be used to set it directly to true or false or to toggle it.
  #
  # Examples:
  #   x = Klass.new
  #   x.a! true   # sets @a to true
  #   x.a!        # toggles @a, so that it ends up being false
  #   x.a!        # toggles @a, so that it ends up being true
  #   x.a! false  # sets @a to false
  #
  def bool_attr_setter(*args)
    make = {}
    args.each { |a|
      make["#{a}!".to_sym] = %{
        def #{a}!(switch=Exception)
          if switch == Exception
            @#{a} = !@#{a}
          else
            @#{a} = switch ? true : false
                    # used to be @#{a} instead of false in Facets version
            self
          end
        end
      }
    }
    module_eval make.values.join("\n"), __FILE__, __LINE__
    make.keys
  end


  # This creates both a reader and a setter for a boolean (flag) attribute (instance variable).
  #
  #   bool_attr_accessor :a
  #
  # is equivalent to
  #
  #   bool_attr_reader :a
  #   bool_attr_setter :a
  #
  # Examples:
  #   x = Klass.new
  #   x.a! true   # sets @a to true
  #   x.a?        # => true
  #   x.a!        # toggles @a, so that it ends up being false
  #   x.a!        # toggles @a, so that it ends up being true
  #   x.a! false  # sets @a to false
  #
  def bool_attr_accessor(*args)
    bool_attr_reader *args
    bool_attr_setter *args
  end

end

# mbool_attr_accessor

class Module
  # 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.
  #
  def mbool_attr_reader(*args)
    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

  # This creates a setter method for a boolean (flag) class/module variable.
  #
  #   mbool_attr_setter :a
  #
  # is equivalent to
  #
  #   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_setter(*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}!(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

  # This creates both a reader and a setter for a boolean (flag) class/module variable.
  #
  #   mbool_attr_accessor :a
  #
  # is equivalent to
  #
  #   mbool_attr_reader :a
  #   mbool_attr_setter :a
  #
  # Works for both classes and modules.
  #
  def mbool_attr_accessor(*args)
    mbool_attr_reader :a
    mbool_attr_setter :a
  end
end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test
require 'test/unit'
require 'rubygems'
require 'qualitysmith_extensions/object/ignore_access'

# Test that it works for *instances*
class TestBoolAttr < Test::Unit::TestCase
  def setup
    @x = C.new
  end

  class C_for_default_is_nil
    bool_attr_accessor :a, :b
  end

  # Also tests that it can create multiple accessors at once.
  def test1_default_is_nil
    assert_equal nil, C_for_default_is_nil.new.a? 
    assert_equal nil, C_for_default_is_nil.new.b? 
  end

  class C
    bool_attr_accessor :a
  end

  def test2_toggle
    assert_equal nil, @x.a? 
    @x.a!
    assert_equal true, @x.a? 
    @x.a!
    assert_equal false, @x.a? 
  end

  def test3_setter
    @x.a! true
    @x.a! true
    assert_equal true, @x.a? 

    @x.a! false 
    assert_equal false, @x.a? 
  end
  def test4_sets_to_boolean_even_if_try_to_set_to_other_type
    @x.a! "whatever" 
    assert_equal true, @x.a?     # Still returns a boolean even though we tried to set it to a string.

    @x.a! nil
    assert_equal false, @x.a?    # Still returns a boolean even though we tried to set it to nil.
  end
end

# Test that it works for *modules*
class TestForModules < Test::Unit::TestCase

  class M_for_default_is_nil
    mbool_attr_accessor :a
  end
  def test1_default_is_nil
    assert_equal nil, M_for_default_is_nil.a? 
  end

  module M
    mbool_attr_accessor :a
  end
  def test2_toggle
    assert_equal nil, M.a? 
    M.a!
    assert_equal true, M.a? 
    M.a!
    assert_equal false, M.a? 
  end
  def test3_setter
    M.a! true
    M.a! true
    assert_equal true, M.a? 

    M.a! false 
    assert_equal false, M.a? 
  end
  def test4_sets_to_boolean_even_if_try_to_set_to_other_type
    M.a! "whatever" 
    assert_equal true, M.a?     # Still returns a boolean even though we tried to set it to a string.

    M.a! nil
    assert_equal false, M.a?    # Still returns a boolean even though we tried to set it to nil.
  end
end

# Test that it also works for *classes*
class TestForClasses < Test::Unit::TestCase

  class C_for_default_is_nil
    mbool_attr_accessor :a
  end
  def test1_default_is_nil
    assert_equal nil, C_for_default_is_nil.a? 
  end

  class C
    mbool_attr_accessor :a
  end

  def test2_toggle
    C.access.class_variable_set :@@a, false 
    assert_equal false, C.a?    # otherwise would have been nil
    C.a!
    assert_equal true, C.a? 
    C.a!
    assert_equal false, C.a? 

    assert_equal ["@_ignore_access_functor"], C.instance_variables
    assert_equal ["@@a"], C.class_variables
  end

  def test3_setter
    C.a! true
    C.a! true
    assert_equal true, C.a? 

    C.a! false 
    assert_equal false, C.a? 
  end
  def test4_sets_to_boolean_even_if_try_to_set_to_other_type
    C.a! "whatever" 
    assert_equal true, C.a?     # Still returns a boolean even though we tried to set it to a string.

    C.a! nil
    assert_equal false, C.a?    # Still returns a boolean even though we tried to set it to nil.
  end
end


# Observation: You can also the normal bool_attr_accessor in conjunction with class << self.
# The methods generated seem to *behave* in the same way. Both techniques allow you to query the class, C.a?
# The only difference is in which *variables* are use to store the state:
# * class << self and the normal bool_attr_accessor:
#   Stores the state in an instance variable for the *class*: @a
# * mbool_attr_accessor:
#   Stores the state in a *class* variable: @@a
#
# Can someone explain to me the difference between class variables and instance variables of a class?
# It seems silly (confusing even!) to have both of them!
#
# My best explanation is that the fact that we can even *have* instance variables of a class does *not* mean it's a good idea
# to use them. It simply means that Matz made the language with as few arbitrary restrictions as possible. He made it
# technically *possible* to do a lot of things that it is probably not good practice to do... And one of those things is
# using instance variables of classes to store state information for that class.
#
# What do you think class variables are for? Exactly that! There's a *reason* we have both @a and @@a variables -- to
# *differentiate* between the two kinds of variables and keep programmers sane. So please don't blur the distinction and
# use the @a-type variable to do the job that @@a-type variables are for.
#
# Or am I missing something here?
#
class TestWith_class_self_and_plain_bool_attr_accessor < Test::Unit::TestCase

  class C_for_default_is_nil
    class << self
      bool_attr_accessor :a
    end
  end
  def test1_default_is_nil
    assert_equal nil, C_for_default_is_nil.a? 
  end

  class C
    class << self
      bool_attr_accessor :a
    end
  end
  # This is where I spotted a class that uses this technique:
  # * Test::Unit::Assertions::AssertionMessage

  def test_2_toggle
    assert_equal false, C.a?  # Why isn't it nil like it is for the instance use case of bool_attr_accessor?
    C.a!
    assert_equal true, C.a? 
    C.a!
    assert_equal false, C.a? 

    assert_equal ["@a"], C.instance_variables
    assert_equal [], C.new.instance_variables  # @a is an instance variable of the *class*  *not* objects of the class  -- weird!
    assert_equal [], C.class_variables
  end

  def test3_setter
    C.a! true
    C.a! true
    assert_equal true, C.a? 

    C.a! false 
    assert_equal false, C.a? 
  end
  def test4_sets_to_boolean_even_if_try_to_set_to_other_type
    C.a! "whatever" 
    assert_equal true, C.a?     # Still returns a boolean even though we tried to set it to a string.

    C.a! nil
    assert_equal false, C.a?    # Still returns a boolean even though we tried to set it to nil.
  end

end
=end