From 41ebb5564d0ea5511c48910863814627acf91f94 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 17 Jan 2016 15:20:35 +0100 Subject: Make ref return the internal symbol. We delegate to_sym to the internal symbol, which we've already called to_sym on in initialize, so we don't need to do that. We also know to_sym will never return a falsy value, so we'll never hit to_s. Just return the symbolized symbol. --- actionview/lib/action_view/template/types.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index be45fcf742..bc805de348 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -32,7 +32,7 @@ module ActionView alias to_str to_s def ref - to_sym || to_s + @symbol end def ==(type) -- cgit v1.2.3 From f48098bd80cb93fcc670f2d0a016a64171dfad65 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 17 Jan 2016 15:26:31 +0100 Subject: Spare to_sym call in `==`. The @symbol has already been converted to a symbol in initialize, so no need to call to_sym when comparing it. Ditch early return for a simple unless statement. --- actionview/lib/action_view/template/types.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index bc805de348..2bd8bcee02 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -36,8 +36,7 @@ module ActionView end def ==(type) - return false if type.blank? - symbol.to_sym == type.to_sym + @symbol == type.to_sym unless type.blank? end end -- cgit v1.2.3 From bb295be314e7c6d2c26b370209e29eba4b4553c8 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 17 Jan 2016 15:31:09 +0100 Subject: Replace delegate calls with standard method defs. Spares a to_sym call by aliasing to_sym to ref. Then the delegate felt meager for one method; ditch and define method ourselves. --- actionview/lib/action_view/template/types.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index 2bd8bcee02..8ba5a48aef 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -28,12 +28,15 @@ module ActionView @symbol = symbol.to_sym end - delegate :to_s, :to_sym, :to => :symbol + def to_s + @symbol.to_s + end alias to_str to_s def ref @symbol end + alias to_sym ref def ==(type) @symbol == type.to_sym unless type.blank? -- cgit v1.2.3 From c10bb2996a441f602ca264a84607692cb3e833c7 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 17 Jan 2016 15:40:36 +0100 Subject: Remove register abstraction. The template types is a private abstraction to fill in basic blanks from Action Dispatch's mime types. As such we can modify the data structure ourselves. --- actionview/lib/action_view/template/types.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index 8ba5a48aef..44a3da934f 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -6,13 +6,7 @@ module ActionView class Types class Type cattr_accessor :types - self.types = Set.new - - def self.register(*t) - types.merge(t.map(&:to_s)) - end - - register :html, :text, :js, :css, :xml, :json + self.types = Set.new([ :html, :text, :js, :css, :xml, :json ]) def self.[](type) return type if type.is_a?(self) -- cgit v1.2.3 From b1dcfa782e64859f9d03b6cf9474506ac5129337 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 17 Jan 2016 15:54:16 +0100 Subject: Replace class attribute with SET constant. We'll be using this to map over to Action Dispatch's Mime::Set. --- actionview/lib/action_view/template/types.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index 44a3da934f..4a64467ce9 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -5,13 +5,12 @@ module ActionView class Template class Types class Type - cattr_accessor :types - self.types = Set.new([ :html, :text, :js, :css, :xml, :json ]) + SET = Set.new([ :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) + if type.is_a?(Symbol) || SET.member?(type.to_s) new(type) end end -- cgit v1.2.3 From eb4ec6648d313586fd801582f20d845b6c2b7e20 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 17 Jan 2016 16:10:43 +0100 Subject: Don't bother looking up the types. If they aren't symbols, then they aren't likely to be in the set anyway. --- actionview/lib/action_view/template/types.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index 4a64467ce9..650023f6c5 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -8,9 +8,9 @@ module ActionView SET = Set.new([ :html, :text, :js, :css, :xml, :json ]) def self.[](type) - return type if type.is_a?(self) - - if type.is_a?(Symbol) || SET.member?(type.to_s) + if type.is_a?(self) + type + else new(type) end end -- cgit v1.2.3 From 85146f6de0a361b98b77c38c589e5208521571b9 Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 17 Jan 2016 16:12:00 +0100 Subject: Enrich the SET constant to respond to symbols. Match `Mime::SET.symbols`. --- actionview/lib/action_view/template/types.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index 650023f6c5..f423399c30 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -5,7 +5,7 @@ module ActionView class Template class Types class Type - SET = Set.new([ :html, :text, :js, :css, :xml, :json ]) + SET = Struct.new(:symbols).new(Set.new([ :html, :text, :js, :css, :xml, :json ])) def self.[](type) if type.is_a?(self) @@ -47,6 +47,10 @@ module ActionView def self.[](type) type_klass[type] end + + def symbols + type_klass::SET.symbols + end end end end -- cgit v1.2.3 From 91f2ad36821cc01d2084dd1690fecc0913fc541d Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Sun, 17 Jan 2016 16:38:54 +0100 Subject: Store the symbols as an array. A Set can't be implicitly converted into an Array: ``` irb(main):012:0> formats = [ :rss ] => [:rss] irb(main):013:0> formats &= SET.symbols TypeError: no implicit conversion of Set into Array from (irb):13:in `&' from (irb):13 from /Users/kasperhansen/.rbenv/versions/2.2.3/bin/irb:11:in `
' ``` Besides `Mime::SET.symbols` returns an Array, so we're closer to that. --- actionview/lib/action_view/template/types.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/template/types.rb b/actionview/lib/action_view/template/types.rb index f423399c30..ddc5b25831 100644 --- a/actionview/lib/action_view/template/types.rb +++ b/actionview/lib/action_view/template/types.rb @@ -5,7 +5,7 @@ module ActionView class Template class Types class Type - SET = Struct.new(:symbols).new(Set.new([ :html, :text, :js, :css, :xml, :json ])) + SET = Struct.new(:symbols).new([ :html, :text, :js, :css, :xml, :json ]) def self.[](type) if type.is_a?(self) -- cgit v1.2.3