Class: DbAgile::Core::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/dbagile/core/chain.rb

Defined Under Namespace

Modules: Participant

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*chain) ⇒ Chain

Creates an empty chain



22
23
24
25
26
# File 'lib/dbagile/core/chain.rb', line 22

def initialize(*chain)
  @delegate_chain = []
  plug(*chain) unless chain.empty?
  __install_chain__
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Handles the magic of delegation through _getobj_.



29
30
31
32
33
34
35
# File 'lib/dbagile/core/chain.rb', line 29

def method_missing(m, *args, &block)
  target = self.__getobj__
  unless target.respond_to?(m)
    super
  end
  target.__send__(m, *args, &block)
end

Instance Attribute Details

#delegate_chainObject (readonly)

Returns the delegator chain



14
15
16
# File 'lib/dbagile/core/chain.rb', line 14

def delegate_chain
  @delegate_chain
end

Class Method Details

.[](*args) ⇒ Object

Creates a chain instance. Shortcut for Chain.new(*args)



17
18
19
# File 'lib/dbagile/core/chain.rb', line 17

def self.[](*args)
  Chain.new(*args)
end

Instance Method Details

#__build_participants__(args) ⇒ Object

Builds some participants



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dbagile/core/chain.rb', line 38

def __build_participants__(args)
  delegates = []
  until args.empty?
    case arg = args.shift
      when Class
        mod_args = []
        until args.empty? or args[0].kind_of?(Module)
          mod_args << args.shift
        end
        delegates << arg.new(*mod_args)
      else
        delegates << arg
    end
  end
  delegates
end

#__install_chain__Object

Installs the chain



56
57
58
59
60
61
# File 'lib/dbagile/core/chain.rb', line 56

def __install_chain__
  delegate_chain.each_cons(2) do |part, its_del|
    part.extend(Participant)
    part.instance_eval{ @__delegate__ = its_del }
  end
end

#connect(last) ⇒ Object

Returns a connected version of self.



76
77
78
79
# File 'lib/dbagile/core/chain.rb', line 76

def connect(last)
  chain = delegate_chain + [ last ]
  Chain.new(*chain)
end

#inspectObject

Inspects this chain



82
83
84
85
86
87
# File 'lib/dbagile/core/chain.rb', line 82

def inspect
  debug = delegate_chain.collect{|c| 
    c.kind_of?(Chain) ? c.inspect : c.class.name
  }.join(', ')
  "[#{debug}]"
end

#plug(*args) ⇒ Object

Plugs some chain participants



69
70
71
72
73
# File 'lib/dbagile/core/chain.rb', line 69

def plug(*args)
  @delegate_chain = __build_participants__(args) + delegate_chain
  __install_chain__
  self
end