Module: Wool::ModuleExtensions

Included in:
SexpAnalysis, SexpAnalysis::ProtocolRegistry, SexpAnalysis::Sexp, SexpAnalysis::WoolMethod, Warning
Defined in:
lib/wool/support/module_extensions.rb

Overview

These are extensions to Wool modules. This module should be extended by any Wool 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

#attr_accessor_with_default(name, val) ⇒ Object

Creates an attr_accessor that defaults to a certain value.



8
9
10
11
12
13
14
15
16
17
# File 'lib/wool/support/module_extensions.rb', line 8

def attr_accessor_with_default(name, val)
  ivar_sym = ":@#{name}".intern
  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

#cattr_accessor(*attrs) ⇒ Object

Creates readers and writers for the given instance variables.



33
34
35
36
# File 'lib/wool/support/module_extensions.rb', line 33

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

#cattr_accessor_with_default(attr, default) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wool/support/module_extensions.rb', line 38

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
        instance_variable_set(varname, default)
        default
      end
    end
  end
  cattr_writer(attr)
end

#cattr_get_and_setter(*attrs) ⇒ Object

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


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wool/support/module_extensions.rb', line 66

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

#cattr_reader(*attrs) ⇒ Object

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



19
20
21
22
23
# File 'lib/wool/support/module_extensions.rb', line 19

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

#cattr_writer(*attrs) ⇒ Object

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



26
27
28
29
30
# File 'lib/wool/support/module_extensions.rb', line 26

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