Class: Caterpillar::Util
- Inherits:
-
Object
- Object
- Caterpillar::Util
- Defined in:
- lib/caterpillar/util.rb
Overview
Common utility methods
Class Method Summary collapse
-
.categorize(portlets) ⇒ Object
Reorganizes the portlets hash by category.
-
.eval_configuration(conf_file = nil) ⇒ Object
Reads and evaluates the configuration.
-
.parse_routes(config) ⇒ Object
Collects Rails’ named routes.
- .xml_to_s(doc) ⇒ Object
Class Method Details
.categorize(portlets) ⇒ Object
Reorganizes the portlets hash by category.
{'Category 1' => [portlets], 'Category 2' => [portlets]}
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 |
# File 'lib/caterpillar/util.rb', line 90 def categorize(portlets) ret = {} # organize into main categories categories = portlets.collect{|p| p[:category]} categories << portlets.collect{|p| p[:categories].first if p[:categories]} categories.flatten!.uniq! categories.each do |category| next if category.nil? # skip nil categories # does this category have subcategories? # skip them. TODO: parse internal categories if (portlets.map{|p| ( !p[:categories].nil? && \ p[:categories].first==category)} & [true] ).any? STDERR.puts '%s has subcategories, skipping' % category.inspect next end # select the portlets in this category _portlets = portlets.select do |p| p[:category]==category or (!p[:categories].nil? and p[:categories].include?(category)) end ret.update(category => _portlets) end # add portlets without category uncategorized = portlets.select {|p| p[:category].nil?} if uncategorized.any? ret.update('undefined' => uncategorized) end return ret end |
.eval_configuration(conf_file = nil) ⇒ Object
Reads and evaluates the configuration. If parameter is not given, read from default location (RAILS_ROOT/config/portlets.rb)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/caterpillar/util.rb', line 15 def eval_configuration(conf_file=nil) return Config.new if not (conf_file or defined?(RAILS_ROOT)) # else . . . conf_file ||= File.join([RAILS_ROOT,Caterpillar::Config::FILE]) if File.exists?(conf_file) #$stdout.puts "Reading configuration from #{conf_file}" config = eval(File.open(conf_file) {|f| f.read}) end unless config.kind_of? Config $stderr.puts "Configuration was not parsed properly" config = Config.new end return config end |
.parse_routes(config) ⇒ Object
Collects Rails’ named routes
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 |
# File 'lib/caterpillar/util.rb', line 31 def parse_routes(config) require 'action_controller' require File.join(CATERPILLAR_LIBS, '..','portlet_test_bench', 'routing') ActionController::Routing::RouteSet::Mapper.send :include, Caterpillar::Routing::MapperExtensions routes = [] config.instances.each do |portlet| # clear old routes from memory and reload ActionController ActionController::Routing::Routes.clear! # prefer portlet rails_root if portlet[:rails_root] rails_root = portlet[:rails_root] elsif config.rails_root rails_root = config.rails_root else next end # load routes f = File.open( File.join( rails_root,'config','routes.rb' )) eval(f.read()) f.close() routes << ActionController::Routing::Routes.named_routes.collect do |route| # Ruby 1.9 if route.class == Symbol name = route _route = ActionController::Routing::Routes.named_routes.routes[route] defaults = {} #TODO: Get default values in ruby 1.9 # Ruby 1.8 elsif route.class == Array name = route[0] _route = route[1] # 'ActionController::Routing::Route' defaults = route[-1].defaults end # segments; the path segs = _route.segments.inject("") { |str,s| str << s.to_s } segs.chop! if segs.length > 1 # controller and action reqs = _route.requirements # extra variables keys = _route.significant_keys vars = keys - [:action, :controller] {:name => name, :path => segs, :reqs => reqs, :vars => vars, :defaults => defaults} end end return routes.flatten end |
.xml_to_s(doc) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/caterpillar/util.rb', line 127 def xml_to_s(doc) # Serializes the REXML::Document to String. # It has to pass Ruby unit test validation and Liferay runtime validation. # The XML requires strict ordering of the child nodes, # and also tags and values have to be on a single line. require 'rexml/formatters/pretty' str = String.new fmt = REXML::Formatters::Pretty.new(4) fmt.compact = true fmt.write(doc,str) return str.gsub('\'', '"') # fix rexml attribute single quotes to double quotes end |