Class: Igor::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/igor/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Builder

Returns a new instance of Builder.



23
24
25
26
# File 'lib/igor/builder.rb', line 23

def initialize(&block)
  @ins = []
  instance_eval(&block) if block_given?
end

Class Method Details

.igor(&block) ⇒ Object



28
29
30
# File 'lib/igor/builder.rb', line 28

def self.igor(&block)
  self.new(&block).to_igor
end

.parse_file(config, opts = {}) ⇒ Object



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

def self.parse_file(config, opts = {})
  options = {}

  if config =~ /\.iu$/
    cfgfile = ::File.read(config)

    if cfgfile[/^#\\(.*)/] && opts
      options = opts.parse! $1
    end

    cfgfile.sub!(/^__END__\n.*/, '')
    igor = eval "Igor::Builder.new {( " + cfgfile + "\n )}.to_igor", TOPLEVEL_BINDING, config
  else
    require config
    igor = Object.const_get(::File.basename(config, '.rb').capitalize)
  end
  return igor, options
end

Instance Method Details

#call(env) ⇒ Object



45
46
47
# File 'lib/igor/builder.rb', line 45

def call(env)
  to_igor.call(env)
end

#run(igor) ⇒ Object



36
37
38
# File 'lib/igor/builder.rb', line 36

def run(igor)
  @ins << igor #lambda { |nothing| igor }
end

#to_igorObject



40
41
42
43
# File 'lib/igor/builder.rb', line 40

def to_igor
  inner_igor = @ins.last
  @ins[0...-1].reverse.inject(inner_igor) { |a, e| e.call(a) }
end

#use(igor, *args, &block) ⇒ Object



32
33
34
# File 'lib/igor/builder.rb', line 32

def use(igor, *args, &block)
  @ins << lambda { |inner_igor| igor.new(inner_igor, *args, &block) }
end