Module: Laser::ModuleExtensions

Included in:
Analysis, Analysis::Annotations, Analysis::BasicAnnotation, Analysis::LaserMethod, Analysis::LaserObject, Analysis::ProtocolRegistry, Analysis::Sexp, Analysis::Visitor, Analysis::Visitor::ClassMethods
Defined in:
lib/laser/support/module_extensions.rb

Overview

These are extensions to Laser modules. This module should be extended by any Laser modules seeking to take advantage of them. This prevents conflicts with other libraries defining extensions of the same name.

Instance Method Summary (collapse)

Instance Method Details

- (Object) attr_accessor_with_default(name, val)

Creates an attr_accessor that defaults to a certain value.



16
17
18
19
20
21
22
23
24
25
# File 'lib/laser/support/module_extensions.rb', line 16

def attr_accessor_with_default(name, val)
  ivar_sym = "@#{name}"
  define_method name do
    unless instance_variable_defined?(ivar_sym)
      instance_variable_set(ivar_sym, val)
    end
    instance_variable_get ivar_sym
  end
  attr_writer name
end

- (Object) cattr_accessor(*attrs)

Creates readers and writers for the given instance variables.



42
43
44
45
# File 'lib/laser/support/module_extensions.rb', line 42

def cattr_accessor(*attrs)
  cattr_reader(*attrs)
  cattr_writer(*attrs)
end

- (Object) cattr_accessor_with_default(attr, default)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/laser/support/module_extensions.rb', line 47

def cattr_accessor_with_default(attr, default)
  varname = "@#{attr}".to_sym
  singleton_class.instance_eval do
    define_method attr do
      if instance_variable_defined?(varname)
        instance_variable_get(varname)
      else
        default = default.dup unless Fixnum === default || default.singleton_class == default.class
        instance_variable_set(varname, default)
        instance_variable_get(varname)
      end
    end
  end
  cattr_writer(attr)
end

- (Object) cattr_get_and_setter(*attrs)

Creates a DSL-friendly set-and-getter method. The method, when called with no arguments, acts as a getter. When called with arguments, it acts as a setter. Uses class instance variables - this is not for generating instance methods.

Examples:

class A
  cattr_get_and_setter :type
end
class B < A
  type :silly
end
p B.type  # => :silly


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/laser/support/module_extensions.rb', line 76

def cattr_get_and_setter(*attrs)
  attrs.each do |attr|
    cattr_accessor attr
    singleton_class.instance_eval do
      alias_method "#{attr}_old_get".to_sym, attr
      define_method attr do |*args, &blk|
        if args.size > 0
          send("#{attr}=", *args)
        elsif blk != nil
          send("#{attr}=", blk)
        else
          send("#{attr}_old_get")
        end
      end
    end
  end
end

- (Object) cattr_reader(*attrs)

Creates a reader for the given instance variables on the class object.



28
29
30
31
32
# File 'lib/laser/support/module_extensions.rb', line 28

def cattr_reader(*attrs)
  attrs.each do |attr|
    instance_eval("def #{attr}; @#{attr}; end")
  end
end

- (Object) cattr_writer(*attrs)

Creates a writer for the given instance variables on the class object.



35
36
37
38
39
# File 'lib/laser/support/module_extensions.rb', line 35

def cattr_writer(*attrs)
  attrs.each do |attr|
    instance_eval("def #{attr}=(val); @#{attr} = val; end")
  end
end

- (Object) opposite_method(new_name, old_name)

Creates a new method that returns the boolean negation of the specified method.



9
10
11
12
13
# File 'lib/laser/support/module_extensions.rb', line 9

def opposite_method(new_name, old_name)
  define_method new_name do |*args, &blk|
    !send(old_name, *args, &blk)
  end
end