Class: Pupu::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/pupu/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(plugin_name, plugin_params, page = Page.new) ⇒ Parser

Returns a new instance of Parser.



19
20
21
22
23
24
25
26
27
# File 'lib/pupu/parser.rb', line 19

def initialize(plugin_name, plugin_params, page = Page.new)
  @plugin    = Pupu[plugin_name, plugin_params]
  @output    = Array.new
  # puts "Parser: #{page.inspect}"
  @page = page
  @dsl       = DSL.new(@plugin, page)
  @@loaded ||= Hash.new
  @@loaded[@plugin.name] = Array.new
end

Instance Method Details

#add_dependenciesObject



46
47
48
49
50
51
# File 'lib/pupu/parser.rb', line 46

def add_dependencies
  @dsl.get_dependencies.each do |dependency|
    parser = Parser.new(dependency.name, dependency.params, @page)
    @output.push(parser.parse!) unless parser.loaded?
  end
end

#add_initializersObject



39
40
41
42
43
44
# File 'lib/pupu/parser.rb', line 39

def add_initializers
  js_initializer  = @plugin.initializer(:javascript)
  css_initializer = @plugin.initializer(:stylesheet)
  @output.push("\n<link href='#{css_initializer.url}' media='screen' rel='stylesheet' type='text/css' />") if css_initializer
  @output.push("\n<script src='#{js_initializer.url}' type='text/javascript'></script>") if js_initializer
end

#loaded?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
# File 'lib/pupu/parser.rb', line 29

def loaded?
  # The reason why just array with plugin names isn't enough is that every time it can be called
  # with different parameters. For example pupu :mootools and then pupu :mootools, more: true
  @@loaded[@plugin.name] && @dsl.files.all? do |file|
    @@loaded[@plugin.name].include?(file)
  end

  false # FIXME: there is a bug in this method, it returns true in every case (not so important)
end

#parse!Object



53
54
55
56
57
58
59
60
# File 'lib/pupu/parser.rb', line 53

def parse!
  @dsl.instance_eval(File.read(@plugin.file("config.rb").path))
  self.add_dependencies
  @@loaded[@plugin.name].push(*@dsl.files)
  @output.push(@dsl.output)
  self.add_initializers
  return @output.join("\n")
end