Module: Hanswurst::Delegates

Defined in:
lib/hanswurst/delegates.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &code) ⇒ Object

shortcuts view_for_, list_for_



25
26
27
28
29
30
31
32
33
# File 'lib/hanswurst/delegates.rb', line 25

def method_missing(meth, *args, &code)
  meth.to_s =~ /^([^=]+)=?$/
  unified_meth = $1
  if role = self.class.delegations[unified_meth.to_sym]
    self.send(role).send(meth, *args, &code)
  else
    super
  end
end

Class Method Details

.included(mod) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hanswurst/delegates.rb', line 3

def self.included(mod)
  mod.instance_eval do
    def delegates(hsh)
      @delegations ||= {}
      hsh.each do |prop,role|
        if prop.is_a? Array
          prop.each do |pr|
            @delegations.update pr => role
          end
        else
          @delegations.update prop => role
        end
      end
    end

    def delegations
      @delegations || {}
    end
  end
end