Class: Flow::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/flow-lite.rb

Overview

base class for flow class (auto)-constructed/build from flowfile

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_step(name_or_names, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/flow-lite.rb', line 47

def self.define_step( name_or_names, &block )
  names =  if name_or_names.is_a?( Array )
             name_or_names
           else
             [name_or_names]   ## assume single symbol (name); wrap in array

           end
  names = names.map {|name| name.to_sym }   ## make sure we always use symbols



  name = names[0]
  puts "[flow]    adding step  >#{name}<..."
  define_method( :"step_#{name}", &block )

  alt_names = names[1..-1]
  alt_names.each do |alt_name|
    puts "[flow]      adding alias >#{alt_name}< for >#{name}<..."
    alias_method( :"step_#{alt_name}", :"step_#{name}" )
  end
end

.step_methodsObject



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

def self.step_methods
  names = instance_methods.reduce([]) do |names, name|
                                        names << $1.to_sym  if name =~ /^step_(.+)/
                                        names
                                      end
  names
end

Instance Method Details

#step(name) ⇒ Object

run step by symbol/name (e.g. step :hello - etc.)



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
95
96
# File 'lib/flow-lite.rb', line 70

def step( name )
  step_name = :"step_#{name}"  ## note: make sure we always use symbols

  if respond_to?( step_name )
    #######

    ## check: track (and report) call stack - why? why not?

    ## e.g.

    ##   [flow >(1) first_step)] step >first_step< - starting...

    ##   [flow >(2) ..first_step > second_step)] step >second_step< - starting...

    ##   [flow >(3) ....first_step > second_step > third_step)] step >third_step< - starting...

    @stack ||= []   ## use a call stack of step names

    @stack.push( name )

    puts "[flow >(#{@stack.size}) #{'..'*(@stack.size-1)}#{@stack.join(' > ')})] step >#{name}< - starting..."
    start_time = Time.now   ## todo: use Timer? t = Timer.start / stop / diff etc. - why? why not?


    __send__( step_name )

    end_time = Time.now
    diff_time = end_time - start_time
    puts "[flow <(#{@stack.size}) #{'..'*(@stack.size-1)}#{@stack.join(' < ')})] step >#{name}< - done in #{diff_time} sec(s)"
    @stack.pop
  else
    puts "!! ERROR: step definition >#{name}< not found; cannot run/execute - known (defined) steps include:"
    pp self.class.step_methods  #=> e.g. [:hello, ...]

    exit 1
  end
end