Module: Shared

Extended by:
Shared
Included in:
Shared
Defined in:
lib/shared.rb

Constant Summary collapse

VERSION =
'1.1.0'
Code =
{}

Instance Method Summary collapse

Instance Method Details

#key_for(name) ⇒ Object



95
96
97
# File 'lib/shared.rb', line 95

def key_for name
  name.to_s.strip.downcase
end

#load(key) ⇒ Object



46
47
48
49
50
51
# File 'lib/shared.rb', line 46

def load key
  key = key_for(key)
  unless Code.has_key?(key)
    ::Kernel.load("shared/#{ key }.rb")
  end
end

#shared(name, options = {}, &block) ⇒ Object Also known as: share, for



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/shared.rb', line 53

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

#singleton_class(object, &block) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/shared.rb', line 99

def singleton_class object, &block
  singleton_class =
    class << object
      self
    end
  block ? singleton_class.module_eval(&block) : singleton_class
end

#versionObject



42
# File 'lib/shared.rb', line 42

def version() Shared::VERSION end