Module: Shared
Constant Summary collapse
- VERSION =
'1.1.1'- Code =
{}
Class Method Summary collapse
Instance Method Summary collapse
- #key_for(name) ⇒ Object
- #load(key) ⇒ Object
- #shared(name, options = {}, &block) ⇒ Object (also: #share, #for)
- #singleton_class(object, &block) ⇒ Object
- #version ⇒ Object
Class Method Details
.description ⇒ Object
44 45 46 |
# File 'lib/shared.rb', line 44 def Shared.description 'a clean way to factor class/instance mixins in ruby' end |
Instance Method Details
#key_for(name) ⇒ Object
99 100 101 |
# File 'lib/shared.rb', line 99 def key_for name name.to_s.strip.downcase end |
#load(key) ⇒ Object
50 51 52 53 54 55 |
# 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 |
#shared(name, options = {}, &block) ⇒ Object Also known as: , for
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 91 92 93 94 |
# File 'lib/shared.rb', line 57 def shared name, = {}, &block key = key_for name via = ([:via]||['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
103 104 105 106 107 108 109 |
# 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 ⇒ Object
42 |
# File 'lib/shared.rb', line 42 def version() Shared::VERSION end |