module Shared

Constants

Code
VERSION

Public Class Methods

description() click to toggle source
# File lib/shared.rb, line 44
def Shared.description
  'a clean way to factor class/instance mixins in ruby'
end

Public Instance Methods

for(name, options = {})
Alias for: shared
key_for(name) click to toggle source
# File lib/shared.rb, line 99
def key_for name
  name.to_s.strip.downcase
end
load(key) click to toggle source
# File lib/shared.rb, line 50
def load key
  key = key_for(key)
  unless Code.has_key?(key)
    ::Kernel.load("shared/#{ key }.rb")
  end
end
share(name, options = {})
Alias for: shared
shared(name, options = {}) click to toggle source
# File lib/shared.rb, line 57
def shared name, options = {}, &block
  key = key_for name
  via = (options[:via]||options['via']||:eval).to_s.to_sym

  if block.nil?
    Shared.load(key)
    return Code[key] 
  end

  m = (Code[key] || Module.new)

  case via
    when :eval
      singleton_class(m) do
        unless m.respond_to?(:blocks)
          blocks = []

          define_method(:blocks){ blocks }

          define_method(:included) do |other|
            blocks.each{|b| other.send(:module_eval, &b)}
          end

          define_method(:extend_object) do |other|
            Shared.singleton_class(other) do
              m.blocks.each{|b| module_eval &b}
            end
          end
        end
      end
      m.blocks << block

    when :module
      m.send(:module_eval, &block)
  end

  Code[key] ||= m
end
Also aliased as: share, for
singleton_class(object, &block) click to toggle source
# File lib/shared.rb, line 103
def singleton_class object, &block
  singleton_class =
    class << object
      self
    end
  block ? singleton_class.module_eval(&block) : singleton_class
end
version() click to toggle source
# File lib/shared.rb, line 42
def version() Shared::VERSION end