Class: ExtensionsProject

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

Constant Summary collapse

@@extension_methods =
[]

Class Method Summary collapse

Class Method Details

._defined?(_module, _method, _type) ⇒ Boolean

See whether the given module implements the given method, taking account of the type (class/instance) required.

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
# File 'lib/extensions/_base.rb', line 124

def _defined?(_module, _method, _type)
  case _type
  when :instance
    _module.instance_method_defined?(_method) # See definition above.
  when :class, :module
    _module.module_method_defined?(_method)   # See definition above.
  end
end

.extension_methodsObject

The list of methods implemented in this project.



76
77
78
# File 'lib/extensions/_base.rb', line 76

def extension_methods
  @@extension_methods
end

.implement(_module, _method, _type = :instance) ⇒ Object

Wraps around the implementation of a method, emitting a warning if the method is already defined. Returns true to indicate - false to indicate failure (i.e. method is already defined). Raises an error if the specified method is not actually implemented by the block.



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
# File 'lib/extensions/_base.rb', line 94

def implement(_module, _method, _type=:instance)
  raise "Internal error: #{__FILE__}:#{__LINE__}" unless
    _module.is_a? Module and
    _method.is_a? Symbol and
    _type == :instance or _type == :class or _type == :module

  fullname = _module.to_s + string_rep(_type) + _method.to_s

  if _defined?(_module, _method, _type)
    STDERR.puts "#{project_name}: #{fullname} is already defined; not overwriting"
    return false
  else
    yield # Perform the block; presumably a method implementation.
    if _method == :initialize and _type == :instance
      # Special case; we can't verify this.
      @@extension_methods<< "#{_module}::new"
    else
      unless _defined?(_module, _method, _type)
        raise "#{project_name}: internal error: was supposed to implement " +
          "#{fullname}, but it didn't!"
      end
      @@extension_methods << fullname
    end
    return true
  end
end

.project_nameObject

Return the name of the project. To be used in error messages, etc., for consistency.



84
85
86
# File 'lib/extensions/_base.rb', line 84

def project_name
  "Ruby/Extensions"
end

.string_rep(method_type) ⇒ Object

Return the string representation of the given method type.



136
137
138
139
140
141
142
143
144
# File 'lib/extensions/_base.rb', line 136

def string_rep(method_type)
  case method_type
  when :instance then "#"
  when :class    then "."
  when :module   then "."
  else
    nil
  end
end