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.



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

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.



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

def output
  @output
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#dependencies(*pupus) ⇒ Object



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

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

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



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

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

#evaluateObject



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

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



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

def files
  @page.files
end

#get_dependenciesObject



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

def get_dependencies
  @dependencies
end

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



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

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



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

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



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

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



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

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



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

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