Class: Katte::Recipe::NodeFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/katte/recipe/node_factory.rb

Constant Summary collapse

@@after_create_hook =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNodeFactory

Returns a new instance of NodeFactory.



10
11
12
13
14
15
# File 'lib/katte/recipe/node_factory.rb', line 10

def initialize
  pattern_regexp = File.join(Katte.app.config.recipes_root, '(?<name>.+?)\.(?<ext>\w+)')
  @path_pattern = /^#{pattern_regexp}$/

  @nodes = {}
end

Class Method Details

.after_create(&proc) ⇒ Object



6
7
8
# File 'lib/katte/recipe/node_factory.rb', line 6

def self.after_create(&proc)
  @@after_create_hook = proc
end

Instance Method Details

#create(params) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/katte/recipe/node_factory.rb', line 49

def create(params)
  node = Katte::Recipe::Node.new params

  @@after_create_hook.call(node, params) if @@after_create_hook

  node
end

#load(path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/katte/recipe/node_factory.rb', line 17

def load(path)
  return unless FileTest.file? path
  return unless m = @path_pattern.match(path)

  name, ext = m[:name], m[:ext]

  file_type = Katte::Recipe::FileType.find(ext)

  directive = file_type.parse(path)

  requires   = directive['require']
  output     = directive['output'].map {|o| Katte::Plugins.output[o.first.to_sym]}
  period     = (directive['period'].empty? ? 'day' : directive['period'].last)
  options    = Hash[directive['option'].map {|k, v| [k, v || true]}]

  params = {
    :name         => name,
    :path         => path,
    :require      => requires,
    :file_type    => file_type,
    :output       => output,
    :period       => period,
    :options      => options,
  }

  if Katte.app.config.mode == 'test'
    params[:output] = [Katte::Plugins::Output.find(:debug)]
  end

  create(params)
end