Module: Friend

Defined in:
lib/friend.rb

Instance Method Summary collapse

Instance Method Details

#friend(meth, *classes) ⇒ Object Also known as: export



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/friend.rb', line 8

def friend(meth, *classes)
  meth, expmeth = meth.to_s, "__friends__#{meth}"
  (friend_list[meth] ||= []).push(*classes)
  alias_method expmeth, meth
  private expmeth
  public meth
  define_method(meth) do |*args, &block|
    cclass, classes = caller_class, self.class.friend_list[meth]
    if cclass == false
      raise NoMethodError, "`#{meth}' is not accessible outside #{self.class}"
    elsif cclass != self.class && classes && !classes.any? {|k| cclass <= k }
      raise NoMethodError, 
        "`#{meth}' is not accessible to #{cclass.inspect}:#{cclass.class.inspect}",
        caller
    end
    send(expmeth, *args, &block)
  end
end

#friend_listObject



4
5
6
# File 'lib/friend.rb', line 4

def friend_list
  @__friends__ ||= {}
end