Sha256: 1bb1c03e2a4948c3822a4596c872495ad240dc8af6413195209731f3507c1e43

Contents?: true

Size: 890 Bytes

Versions: 2

Compression:

Stored size: 890 Bytes

Contents

# Implementable interfaces in ruby
module Interface
  autoload :Abstract, 'interface/abstract'
  autoload :Version, 'interface/version'

  # Takes a module (or multiple in reverse order), extends it with <tt>Interface::Abstract</tt>, then includes it into the current object
  #
  # Example
  #
  #   module Remote
  #     # turns the device on
  #     def on
  #     end
  #   end
  #
  #   class Device
  #     implements Remote
  #
  #     def on
  #       @power = true
  #     end
  #   end
  def implements(*modules)
    modules.flatten.reverse!.each { |mod| include mod.extend(Abstract) }
  end

  # Conforms with naming conventions for include and extend
  alias_method :implement, :implements

  # Returns an array of interfaces implemented by the current object
  def interfaces
    included_modules.select { |mod| mod.is_a?(Abstract) }
  end
end

Object.send(:include, Interface)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shuber-interface-0.0.1 lib/interface.rb
shuber-interface-0.0.0 lib/interface.rb