Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/sixarm_ruby_defining.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) defining(*method_names)

To protect our code, we call #defining before we define a new method.

Example

class C
  defining :foo
  def foo
    ...
  end
end

The #defining method asks if the method #foo is safe for us to define:

* If #foo is not yet defined, then return true, i.e. we're safe.
* If #foo is already definied, then raise an error.

This is helpful for meta-programming because it lets us know that we're not overriding a method that already exists.

We can call this on many methods at once:

class C
   defining :foo, :goo, :hoo
   ...
end

The #defining method asks if each method is safe for us to define:

* If each method is not yet definied, then return true.
* If a method is already definied, then raise an error.

See #defining_success See #defining_failure Cf. #redefining



46
47
48
49
50
51
52
53
54
# File 'lib/sixarm_ruby_defining.rb', line 46

def defining(*method_names)
  aok = true
  [*method_names].each{|m| 
    aok &= !method_defined?(m) \
    ? defining_success(m) \
    : defining_failure(m)
  }
  return aok
end

- (Object) defining_failure(method_name)

Called when #defining sees that you are creating a method that already exists.

This method raises an error with a diagnostic message. You can override it if you want to provide features like logging, coverage analysis, reporting, etc.

If you override this and do not choose to raise an error, be sure to return true if we're safe, or false otherwise.

See #defining See #defining_success



85
86
87
# File 'lib/sixarm_ruby_defining.rb', line 85

def defining_failure(method_name)
  raise "Class#defining expects '#{method_name}' to be a new method, but it already exists: #{self}##{method_name}"
end

- (Object) defining_success(method_name)

Called when #defining sees that you are creating a new method.

This method is a default and always returns true. you can override it if you want to provide features like logging, coverage analysis, reporting, etc.

See #defining See #defining_failure



68
69
70
# File 'lib/sixarm_ruby_defining.rb', line 68

def defining_success(method_name)
  return true
end

- (Object) redefining(*method_names)

To protect our code, we call #redefining before we redefine a new method.

Example

class C
  defining :hash
  def hash
    ...
  end
end

The #redefining method asks if the method #hash already exists:

* If #hash is already defined, then return true, i.e. we're safe.
* If #hash is not yet defined, then raise an error.

This is helpful for meta-programming because it lets us know that we're intentially overriding a method that already exists.

We can call this on many methods at once:

class C
   redefining :hash, :inspect, "equals?"
   ...
end

The #redefining method asks if each method is safe for us to define:

* If each method is already definied, then return true.
* If a method is not yet definied, then raise an error.

See #redefining_success See #redefining_failure Cf. #defining



127
128
129
130
131
132
133
134
135
# File 'lib/sixarm_ruby_defining.rb', line 127

def redefining(*method_names)
  aok = true
  [*method_names].each{|m|
    aok &= method_defined?(m) \
    ? redefining_success(m)
    : redefining_failure(m)
  }
  return aok
end

- (Object) redefining_failure(method_name)

Called when #redefining sees that you are creating a method that doesn't exist.

This method raises an error with a diagnostic message. You can override it if you want to provide features like logging, coverage analysis, reporting, etc.

If you override this and do not choose to raise an error, be sure to return true if we're safe, or false otherwise.

See #redefining See #redefining_success



166
167
168
# File 'lib/sixarm_ruby_defining.rb', line 166

def redefining_failure(method_name)
  raise "Class#redefining expects '#{method_name}' to be overriding an existing method, but it doesn't exist"
end

- (Object) redefining_success(method_name)

Called when #redefining sees that you are overriding an existing method.

This method is a default and always returns true. You can override it if you want to provide features like logging, coverage analysis, reporting, etc.

See #redefining See #redefining_failure



149
150
151
# File 'lib/sixarm_ruby_defining.rb', line 149

def redefining_success(method_name)
  true
end