Module: Webhookdb::MethodUtilities

Overview

A collection of methods for declaring other methods.

class MyClass
    extend Webhookdb::MethodUtilities

    singleton_attr_accessor :types
    singleton_method_alias :kinds, :types
end

MyClass.types = [ :pheno, :proto, :stereo ]
MyClass.kinds # => [:pheno, :proto, :stereo]

Instance Method Summary collapse

Instance Method Details

#attr_predicate(attrname) ⇒ Object

Create a reader in the form of a predicate for the given attrname.



67
68
69
70
71
72
# File 'lib/webhookdb/method_utilities.rb', line 67

def attr_predicate(attrname)
  attrname = attrname.to_s.chomp("?")
  define_method(:"#{attrname}?") do
    instance_variable_get(:"@#{attrname}") ? true : false
  end
end

#attr_predicate_accessor(attrname) ⇒ Object

Create a reader in the form of a predicate for the given attrname as well as a regular writer method.



76
77
78
79
80
81
# File 'lib/webhookdb/method_utilities.rb', line 76

def attr_predicate_accessor(attrname)
  attrname = attrname.to_s.chomp("?")
  attr_writer(attrname)

  attr_predicate(attrname)
end

#singleton_attr_accessor(*symbols) ⇒ Object

Creates readers and writers that allow assignment to the attributes of the singleton of the declaring object that correspond to the specified symbols.



47
48
49
50
51
# File 'lib/webhookdb/method_utilities.rb', line 47

def singleton_attr_accessor(*symbols)
  symbols.each do |sym|
    singleton_class.__send__(:attr_accessor, sym)
  end
end

#singleton_attr_reader(*symbols) ⇒ Object

Creates instance variables and corresponding methods that return their values for each of the specified symbols in the singleton of the declaring object (e.g., class instance variables and methods if declared in a Class).



22
23
24
25
26
# File 'lib/webhookdb/method_utilities.rb', line 22

def singleton_attr_reader(*symbols)
  singleton_class.instance_exec(symbols) do |attrs|
    attr_reader(*attrs)
  end
end

#singleton_attr_writer(*symbols) ⇒ Object

Creates methods that allow assignment to the attributes of the singleton of the declaring object that correspond to the specified symbols.



38
39
40
41
42
# File 'lib/webhookdb/method_utilities.rb', line 38

def singleton_attr_writer(*symbols)
  singleton_class.instance_exec(symbols) do |attrs|
    attr_writer(*attrs)
  end
end

#singleton_method_alias(newname, original) ⇒ Object

Creates an alias for the original method named newname.



62
63
64
# File 'lib/webhookdb/method_utilities.rb', line 62

def singleton_method_alias(newname, original)
  singleton_class.__send__(:alias_method, newname, original)
end

#singleton_predicate_accessor(*symbols) ⇒ Object

Create predicate methods and writers that allow assignment to the attributes of the singleton of the declaring object that correspond to the specified symbols.



56
57
58
59
# File 'lib/webhookdb/method_utilities.rb', line 56

def singleton_predicate_accessor(*symbols)
  singleton_class.extend(Webhookdb::MethodUtilities)
  singleton_class.attr_predicate_accessor(*symbols)
end

#singleton_predicate_reader(*symbols) ⇒ Object

Create instance variables and corresponding methods that return true or false values for each of the specified symbols in the singleton of the declaring object.



31
32
33
34
# File 'lib/webhookdb/method_utilities.rb', line 31

def singleton_predicate_reader(*symbols)
  singleton_class.extend(Webhookdb::MethodUtilities)
  singleton_class.attr_predicate(*symbols)
end