Class: Pupu::DSL

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pupu, page = Page.new) ⇒ DSL

Returns a new instance of DSL.



21
22
23
24
25
26
27
28
29
# File 'lib/pupu/dsl.rb', line 21

def initialize(pupu, page = Page.new)
  @pupu   = pupu
  @page   = page
  @output = Array.new
  @files  = files
  @dependencies = Array.new
  puts "DSL: #{page.inspect}"
  @path = pupu.file("config.rb")
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



15
16
17
# File 'lib/pupu/dsl.rb', line 15

def output
  @output
end

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/pupu/dsl.rb', line 15

def path
  @path
end

Instance Method Details

#dependencies(*pupus) ⇒ Object



49
50
51
52
53
# File 'lib/pupu/dsl.rb', line 49

def dependencies(*pupus)
  pupus.each do |pupu|
    self.dependency(pupu)
  end
end

#dependency(pupu, params = Hash.new) ⇒ Object



42
43
44
45
46
47
# File 'lib/pupu/dsl.rb', line 42

def dependency(pupu, params = Hash.new)
  struct = OpenStruct.new
  struct.name = pupu
  struct.params = params
  @dependencies.push(struct)
end

#evaluateObject



31
32
33
34
35
36
# File 'lib/pupu/dsl.rb', line 31

def evaluate
  content = File.read(self.path.to_s)
  self.instance_eval(content)
rescue Exception => exception
  abort "Exception during parsing #{self.path} in #{pupu.inspect}:\n#{exception.inspect}\n#{exception.backtrace.join("\n")}"
end

#filesObject



17
18
19
# File 'lib/pupu/dsl.rb', line 17

def files
  @page.files
end

#get_dependenciesObject



55
56
57
# File 'lib/pupu/dsl.rb', line 55

def get_dependencies
  @dependencies
end

#javascript(basename, params = Hash.new) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pupu/dsl.rb', line 59

def javascript(basename, params = Hash.new)
  path = @pupu.javascript(basename).url
  tag  = "<script src='#{path}' type='text/javascript'></script>"
  if params[:if]
    tag = "<!--[if #{params[:if]}]>" + tag + "<![endif]-->"
  end
  unless files.include?(path)
    files.push(path)
    @output.push(tag)
  end
end

#javascripts(*names) ⇒ Object



86
87
88
89
90
# File 'lib/pupu/dsl.rb', line 86

def javascripts(*names)
  names.each do |name|
    self.javascript(name)
  end
end

#parameter(name, params = Hash.new, &block) ⇒ Object

parameter :more do |boolean|

javascript "mootools-1.2-more" if boolean

end



105
106
107
108
109
110
111
112
113
114
# File 'lib/pupu/dsl.rb', line 105

def parameter(name, params = Hash.new, &block)
  # pupu :autocompleter, type: "request"
  # @pupu.params: { type: "request" }

  # pupu :mootools, more: true
  # @pupu.params: { more: true }
  if @pupu.params.key?(name)
    block.call(@pupu.params[name])
  end
end

#stylesheet(basename, params = Hash.new) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pupu/dsl.rb', line 71

def stylesheet(basename, params = Hash.new)
  path = @pupu.stylesheet(basename).url
  condition = params.delete(:if)
  default = {media: 'screen', rel: 'stylesheet', type: 'text/css'}
  params = default.merge(params)
  tag  = "<link href='#{path}' #{params.to_html_attrs} />"
  if condition
    tag = "<!--[if #{condition}]>" + tag + "<![endif]-->"
  end
  unless files.include?(path)
    files.push(path)
    @output.push(tag)
  end
end

#stylesheets(*names) ⇒ Object



92
93
94
95
96
# File 'lib/pupu/dsl.rb', line 92

def stylesheets(*names)
  names.each do |name|
    self.stylesheet(name)
  end
end