Class: Orange::Parser

Inherits:
Resource show all
Defined in:
lib/orange-core/resources/parser.rb

Instance Method Summary collapse

Methods inherited from Resource

call_me, #do_view, #find_extras, #init, #initialize, #options, #orange, #orange_name, #routable, #set_orange, set_orange, #view, #view_opts

Methods included from ClassInheritableAttributes

#cattr_accessor, #cattr_reader, #cattr_writer, eval_in_accessor_module, fetch_value, store_value

Constructor Details

This class inherits a constructor from Orange::Resource

Instance Method Details

#afterLoadObject



8
9
10
11
12
13
14
# File 'lib/orange-core/resources/parser.rb', line 8

def afterLoad
  orange.add_pulp Orange::Pulp::ParserPulp
  @template_dirs = [File.join(orange.core_dir, 'templates')]
  @view_dirs = [File.join(orange.core_dir, 'views')]
  Orange.plugins.each{|p| @template_dirs << p.templates if p.has_templates? }
  Orange.plugins.each{|p| @view_dirs << p.views if p.has_views? }
end

#haml(file, packet_binding, *vars, &block) ⇒ Object



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/orange-core/resources/parser.rb', line 23

def haml(file, packet_binding, *vars, &block)
  
  opts = vars.extract_options!
  # Initial info
  temp = opts.delete(:template)
  opts[:resource_name] = opts[:resource].orange_name.to_s if 
      opts[:resource] && opts[:resource].respond_to?(:orange_name)
  resource = (opts[:resource_name] || '').downcase
  
  if packet_binding.is_a? Orange::Packet
    context = packet_binding['route.context'].to_s
    unless temp
      packet_binding['parser.haml-templates'] ||= {}
      haml_engine = packet_binding['parser.haml-templates']["#{context}-#{resource}-#{file}"] || false
    end
  end
  unless haml_engine
    opts.merge :orange => orange
  
    string = false
    if temp
      string ||= read_if_exists('templates', file) 
      @template_dirs.each do |templates_dir|
        string ||= read_if_exists(templates_dir, file)
      end unless string
    end
  
    if context
      #Check for context specific overrides
      string ||= read_if_exists('views', resource, context+"."+file) if resource
      string ||= read_if_exists('views', context+"."+file)
      @view_dirs.each do |views_dir|
        string ||= read_if_exists(views_dir, resource, context+"."+file) if resource
        string ||= read_if_exists(views_dir, context+"."+file)
      end unless string
    end
  
    # Check for standard views
    string ||= read_if_exists('views', resource, file) if resource
    string ||= read_if_exists('views', file)
    @view_dirs.each do |views_dir|
      string ||= read_if_exists(views_dir, resource, file) if resource
      string ||= read_if_exists(views_dir, file)
    end unless string
  
    # Check for default resource views
    string ||= read_if_exists('views', 'default_resource', file)
    @view_dirs.each do |views_dir|
      string ||= read_if_exists(views_dir, 'default_resource', file) if resource
    end unless string
    raise LoadError, "Couldn't find haml file '#{file}'" unless string
    
    haml_engine = Haml::Engine.new(string)
    if packet_binding.is_a? Orange::Packet
      packet_binding['parser.haml-templates']["#{context}-#{resource}-#{file}"] = haml_engine
    end
  end
  out = haml_engine.render(packet_binding, opts, &block)
end

#hpricot(text) ⇒ Object



88
89
90
91
# File 'lib/orange-core/resources/parser.rb', line 88

def hpricot(text)
  require 'hpricot'
  Hpricot(text)
end

#json(text) ⇒ Object



97
98
99
# File 'lib/orange-core/resources/parser.rb', line 97

def json(text)
  Crack::JSON.parse(text)
end

#read_if_exists(*args) ⇒ Object



83
84
85
86
# File 'lib/orange-core/resources/parser.rb', line 83

def read_if_exists(*args)
  return File.read(File.join(*args)) if File.exists?(File.join(*args))
  false
end

#xml(text) ⇒ Object



93
94
95
# File 'lib/orange-core/resources/parser.rb', line 93

def xml(text)
  Crack::XML.parse(text)
end

#yaml(file) ⇒ Object



16
17
18
19
20
21
# File 'lib/orange-core/resources/parser.rb', line 16

def yaml(file)
  return nil unless File.exists?(file)
  string = File.read(file) 
  string.gsub!('__ORANGE__', orange.app_dir)
  out = YAML::load(string)
end