Class Web::Typesettings
In: lib/web/typesettings.rb
Parent: Object

Typesettings for portlets. Used in Web::Layout.

Methods

columns=   include?   method_missing   new   parse   read   to_s  

Attributes

portlets  [RW] 
template_id  [RW] 

Public Class methods

Takes either existing raw typesettings string (to be parsed) or a Hash, when clean Typesettings is created.

[Source]

    # File lib/web/typesettings.rb, line 9
 9:     def initialize(params={})
10:       @portlets = {}
11:       if params.is_a?(String)
12:         self.read(params)
13:       else
14:         self.columns = (params[:columns] || 2)
15:       end
16:     end

Public Instance methods

Choose the number of columns in the Layout

[Source]

    # File lib/web/typesettings.rb, line 35
35:     def columns=(nr)
36:       @template_id = (
37:         case nr
38:         when 1
39:           '1_column'
40:         when 2
41:           '2_columns_ii'
42:         else
43:           raise 'Invalid column number'
44:         end
45:       )
46:     end

Does this Typesettings include this portlet? Params:

 - portlet (either name as String, or Web::PortletName)

[Source]

     # File lib/web/typesettings.rb, line 92
 92:     def include?(args)
 93:       if args.is_a?(String)
 94:         p = Web::PortletName.find_by_name args
 95:       elsif args.is_a?(Web::PortletName)
 96:         p = args
 97:       else
 98:         raise 'Invalid input class: %s' % args.class
 99:       end
100: 
101:       #!@portlets_s[/[=,]#{p.portletid},/].nil?
102: 
103:       # this may be handy if columns are needed
104:       #_x = @portlets.find{|k,v| v.include?(p.portletid)}
105:       #return false if _x.nil?
106:       #column = _x.first[0] 
107:       #return true
108: 
109:       @portlets.any?{|k,v| v.include?(p.portletid)}
110:     end

Accepts a PortletName as the first parameter, the second parameter acceps a Hash {:column => nr} where nr is the column to place the portlet.

[Source]

    # File lib/web/typesettings.rb, line 58
58:     def method_missing(method, *args, &block)
59:       begin
60:         portlet = Web::PortletName.find_by_name(method.to_s)
61:         return nil unless portlet
62: 
63:         column = 1
64:         if args.any?
65:           if args.first[:column]
66:             column = args.first[:column]
67:           end
68:         end
69: 
70:         @portlets.update(column => [portlet.portletid])
71: 
72:       rescue
73:         STDERR.puts $!.message
74:         STDERR.puts 'Have you installed Caterpillar?'
75:         return nil
76:       end
77:       self
78:     end

Formulates the typesettings columns string for portlets

[Source]

    # File lib/web/typesettings.rb, line 81
81:     def parse
82:       columns = []
83:       @portlets.each_pair do |column,portlets|
84:         columns << "column-%i=%s," % [column, portlets.join(",")]
85:       end
86:       columns.join("\n")
87:     end

Parses raw typesettings String into internal format.

[Source]

    # File lib/web/typesettings.rb, line 19
19:     def read(raw)
20:       _x = raw.split(/\n/)
21:       #puts _x.inspect
22: 
23:       template_id = _x.find{|z| z[/layout-template-id.*/]}
24:       if template_id
25:         _x.delete_at(_x.index(template_id))
26:         @template_id = template_id[/=(.*)/,1]
27:       end
28: 
29:       _x.each do |col|
30:         @portlets[col[/-(.)=/,1].to_i] = col[/=(.*)/,1].split(/,/)
31:       end
32:     end

Output String

[Source]

    # File lib/web/typesettings.rb, line 49
49:     def to_s
50:       s = ''
51:       s << "layout-template-id=%s\n" % @template_id if @template_id
52:       s << self.parse
53:       return s
54:     end

[Validate]