Skip to content
Newer
Older
100644 58 lines (43 sloc) 1.07 KB
67f55e2 @drogus Implement ActionView::Template::Types
drogus authored Jul 5, 2012
1 require 'set'
2 require 'active_support/core_ext/class/attribute_accessors'
3
4 module ActionView
5 class Template
6 class Types
7 class Type
8 cattr_accessor :types
9 self.types = Set.new
10
11 def self.register(*t)
12 types.merge(t.map { |type| type.to_s })
13 end
14
15 register :html, :text, :js, :css, :xml, :json
16
17 def self.[](type)
18 return type if type.is_a?(self)
19
20 if type.is_a?(Symbol) || types.member?(type.to_s)
21 new(type)
22 end
23 end
24
25 attr_reader :symbol
26
27 def initialize(symbol)
28 @symbol = symbol.to_sym
29 end
30
31 delegate :to_s, :to_sym, :to => :symbol
32 alias to_str to_s
33
34 def ref
35 to_sym || to_s
36 end
37
38 def ==(type)
39 return false if type.blank?
40 symbol.to_sym == type.to_sym
41 end
42 end
43
44 cattr_accessor :type_klass
45
46 def self.delegate_to(klass)
47 self.type_klass = klass
48 end
49
50 delegate_to Type
51
52 def self.[](type)
53 type_klass[type]
54 end
55 end
56 end
57 end
Something went wrong with that request. Please try again.