Class: Caterpillar::Parser

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

Overview

Portlet configuration and route parser.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Parser

include ActionController::Assertions::RoutingAssertions



14
15
16
17
# File 'lib/caterpillar/parser.rb', line 14

def initialize(config)
  @config = config
  @routes = config.routes
end

Instance Method Details

#portlets(routes = @routes) ⇒ Object

Updates the portlets hash from the routes and configuration options. Changes the path variables to a format supported by the Rails-portlet.



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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/caterpillar/parser.rb', line 21

def portlets(routes=@routes)
  raise 'No configuration' unless @config
  portlets = []

  @config.instances.flatten.each do |portlet|

    ### route to path
    if portlet[:path]
      # take user-given path & do not parse routes
      path = portlet[:path]
      #
      # parse the requirements - controller & action
      # ( this is too difficult -- no navigation for user-given paths )
      #
#           begin
#             #recognized_request_for(path)
#             #builder = ActionController::Routing::RouteBuilder.new
#             #r = ActionController::Routing::Routes.recognize_path(path, { :method => :get })
#             #puts r.inspect
#             #req_path = builder.segments_for_route_path(r)
#             #STDERR.puts req_path.inspect
#           rescue
#             STDERR.puts $!.message
#           end

      portlet.update( :reqs => {} )
      portlet.update( :vars => [] )

    else # parse path from routes
      begin
        _r = routes.select{
          |route| route[:name]==portlet[:name].to_sym
        }
        path = _r.first[:path] # take only the first segments
        raise if path.nil?
      rescue
        $stderr.puts ' !! no route for %s' % portlet[:name]
        next
      end
                                  
      # getting de default values from wildcards (:controller, :action, :other)
      portlet.update(:defaults => _r.first[:defaults])
      
      ### requirements - controller & action
      portlet.update( :reqs => _r.first[:reqs] )

      ### variables
      # take just the ones that are required in the path!
      vars = []
      _r.first[:vars].each do |var|
        # variables that are not defined in reqs are required to be inserted by the rails-portlet
        vars << var unless _r.first[:reqs][var]
      end
      portlet.update( :vars => vars )

      # delete the route from routes
	  if routes
        _r.each do |r|
          routes.delete(r)
        end
	  end
    end
    portlet.update( :path => path )

    ### javascripts
    # append portlet's javascripts to global javascripts
    javascripts = (portlet[:javascripts].nil? ?
      @config.javascripts : @config.javascripts + portlet[:javascripts].to_a)
    portlet.update( :javascripts => javascripts.flatten )

    portlets << portlet
  end

  # leftover named routes
  if @config.include_all_named_routes==true
    portlets << routes
  end

  # sanity check
  portlets.flatten!
  portlets.compact!
  portlets.each do |portlet|
    ### hostname
    portlet[:host] ||= @config.host

    ### servlet
    portlet[:servlet] ||= @config.servlet

    ### category
    portlet[:category] ||= @config.category

    ### title
    _title = portlet[:title] || portlet[:name].to_s.gsub('_',' ').capitalize
    # strip illegal characters
    title = _title.gsub(/ä/,'a').gsub(/ö/,'o').gsub(/Ä/,'A').gsub(/Ö/,'O')
    portlet.update( :title => title )

    portlet[:edit_mode] ||= nil
    portlet[:instanceable] ||= false

    ### unless defined, use default javascripts
    portlet[:javascripts] ||= @config.javascripts

    path = portlet[:path]
    portlet.update( :path => path )
  end

  return portlets
end