aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/template/types.rb
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2012-07-05 22:02:11 +0200
committerPiotr Sarnacki <drogus@gmail.com>2012-08-28 11:19:36 +0200
commit67f55e282236eef53adc6036e735190b1dda5a47 (patch)
tree3fb6712eaaa98871caace81f6b4d1ce6b7d1a965 /actionpack/lib/action_view/template/types.rb
parentdc663dd52c99ab6e6c633b54d1a0e836f379bf9f (diff)
downloadrails-67f55e282236eef53adc6036e735190b1dda5a47.tar.gz
rails-67f55e282236eef53adc6036e735190b1dda5a47.tar.bz2
rails-67f55e282236eef53adc6036e735190b1dda5a47.zip
Implement ActionView::Template::Types
AV::Template::Types is a small abstraction to allow to specify template types that can be used in ActionView. When Action Pack is loaded it's replaced with Mime::Type.
Diffstat (limited to 'actionpack/lib/action_view/template/types.rb')
-rw-r--r--actionpack/lib/action_view/template/types.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/template/types.rb b/actionpack/lib/action_view/template/types.rb
new file mode 100644
index 0000000000..7611c9e708
--- /dev/null
+++ b/actionpack/lib/action_view/template/types.rb
@@ -0,0 +1,58 @@
+require 'set'
+require 'active_support/core_ext/class/attribute_accessors'
+require 'active_support/core_ext/object/blank'
+
+module ActionView
+ class Template
+ class Types
+ class Type
+ cattr_accessor :types
+ self.types = Set.new
+
+ def self.register(*t)
+ types.merge(t.map { |type| type.to_s })
+ end
+
+ register :html, :text, :js, :css, :xml, :json
+
+ def self.[](type)
+ return type if type.is_a?(self)
+
+ if type.is_a?(Symbol) || types.member?(type.to_s)
+ new(type)
+ end
+ end
+
+ attr_reader :symbol
+
+ def initialize(symbol)
+ @symbol = symbol.to_sym
+ end
+
+ delegate :to_s, :to_sym, :to => :symbol
+ alias to_str to_s
+
+ def ref
+ to_sym || to_s
+ end
+
+ def ==(type)
+ return false if type.blank?
+ symbol.to_sym == type.to_sym
+ end
+ end
+
+ cattr_accessor :type_klass
+
+ def self.delegate_to(klass)
+ self.type_klass = klass
+ end
+
+ delegate_to Type
+
+ def self.[](type)
+ type_klass[type]
+ end
+ end
+ end
+end