diff --git a/History.md b/History.md index 69d4bd3..9083759 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,7 @@ ## 3.0.0 / not yet released / branch "master" * ... +* Freeze lots of string literals for new Ruby 2.1 optimization, see #297 [Florian Weingarten, fw42] * Allow newlines in tags and variables, see #324 [Dylan Thacker-Smith, dylanahsmith] * Tag#parse is called after initialize, which now takes options instead of tokens as the 3rd argument. See #321 [Dylan Thacker-Smith, dylanahsmith] * Raise `Liquid::ArgumentError` instead of `::ArgumentError` when filter has wrong number of arguments #309 [Bogdan Gusiev, bogdan] diff --git a/lib/liquid.rb b/lib/liquid.rb index 3b35e56..484f8b6 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -21,9 +21,9 @@ module Liquid FilterSeparator = /\|/ - ArgumentSeparator = ',' - FilterArgumentSeparator = ':' - VariableAttributeSeparator = '.' + ArgumentSeparator = ','.freeze + FilterArgumentSeparator = ':'.freeze + VariableAttributeSeparator = '.'.freeze TagStart = /\{\%/ TagEnd = /\%\}/ VariableSignature = /\(?[\w\-\.\[\]]\)?/ diff --git a/lib/liquid/block.rb b/lib/liquid/block.rb index c53c5c5..a5118f9 100644 --- a/lib/liquid/block.rb +++ b/lib/liquid/block.rb @@ -41,14 +41,14 @@ module Liquid unknown_tag($1, $2, tokens) end else - raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination", :token => token, :tag_end => TagEnd.inspect)) + raise SyntaxError.new(options[:locale].t("errors.syntax.tag_termination".freeze, :token => token, :tag_end => TagEnd.inspect)) end when IsVariable new_var = create_variable(token) @nodelist << new_var @children << new_var @blank = false - when '' + when ''.freeze # pass else @nodelist << token @@ -79,15 +79,15 @@ module Liquid def unknown_tag(tag, params, tokens) case tag - when 'else' - raise SyntaxError.new(options[:locale].t("errors.syntax.unexpected_else", + when 'else'.freeze + raise SyntaxError.new(options[:locale].t("errors.syntax.unexpected_else".freeze, :block_name => block_name)) - when 'end' - raise SyntaxError.new(options[:locale].t("errors.syntax.invalid_delimiter", - :block_name => block_name, + when 'end'.freeze + raise SyntaxError.new(options[:locale].t("errors.syntax.invalid_delimiter".freeze, + :block_name => block_name, :block_delimiter => block_delimiter)) else - raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag", :tag => tag)) + raise SyntaxError.new(options[:locale].t("errors.syntax.unknown_tag".freeze, :tag => tag)) end end @@ -103,7 +103,7 @@ module Liquid token.scan(ContentOfVariable) do |content| return Variable.new(content.first, @options) end - raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination", :token => token, :tag_end => VariableEnd.inspect)) + raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination".freeze, :token => token, :tag_end => VariableEnd.inspect)) end def render(context) @@ -113,7 +113,7 @@ module Liquid protected def assert_missing_delimitation! - raise SyntaxError.new(options[:locale].t("errors.syntax.tag_never_closed", :block_name => block_name)) + raise SyntaxError.new(options[:locale].t("errors.syntax.tag_never_closed".freeze, :block_name => block_name)) end def render_all(list, context) @@ -138,7 +138,7 @@ module Liquid context.increment_used_resources(:render_length_current, token_output) if context.resource_limits_reached? context.resource_limits[:reached] = true - raise MemoryError.new("Memory limits exceeded") + raise MemoryError.new("Memory limits exceeded".freeze) end unless token.is_a?(Block) && token.blank? output << token_output diff --git a/lib/liquid/condition.rb b/lib/liquid/condition.rb index ade7ac1..bf025d9 100644 --- a/lib/liquid/condition.rb +++ b/lib/liquid/condition.rb @@ -8,14 +8,14 @@ module Liquid # class Condition #:nodoc: @@operators = { - '==' => lambda { |cond, left, right| cond.send(:equal_variables, left, right) }, - '!=' => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) }, - '<>' => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) }, - '<' => :<, - '>' => :>, - '>=' => :>=, - '<=' => :<=, - 'contains' => lambda { |cond, left, right| left && right ? left.include?(right) : false } + '=='.freeze => lambda { |cond, left, right| cond.send(:equal_variables, left, right) }, + '!='.freeze => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) }, + '<>'.freeze => lambda { |cond, left, right| !cond.send(:equal_variables, left, right) }, + '<'.freeze => :<, + '>'.freeze => :>, + '>='.freeze => :>=, + '<='.freeze => :<=, + 'contains'.freeze => lambda { |cond, left, right| left && right ? left.include?(right) : false } } def self.operators @@ -61,7 +61,7 @@ module Liquid end def inspect - "#" + "#" end private diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index 1da66a9..0663bc5 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -103,7 +103,7 @@ module Liquid # Push new local scope on the stack. use Context#stack instead def push(new_scope={}) @scopes.unshift(new_scope) - raise StackLevelError, "Nesting too deep" if @scopes.length > 100 + raise StackLevelError, "Nesting too deep".freeze if @scopes.length > 100 end # Merge a hash of variables in the current local scope @@ -151,11 +151,11 @@ module Liquid private LITERALS = { - nil => nil, 'nil' => nil, 'null' => nil, '' => nil, - 'true' => true, - 'false' => false, - 'blank' => :blank?, - 'empty' => :empty? + nil => nil, 'nil'.freeze => nil, 'null'.freeze => nil, ''.freeze => nil, + 'true'.freeze => true, + 'false'.freeze => false, + 'blank'.freeze => :blank?, + 'empty'.freeze => :empty? } # Look up variable, either resolve directly after considering the name. We can directly handle @@ -244,7 +244,7 @@ module Liquid # Some special cases. If the part wasn't in square brackets and # no key with the same name was found we interpret following calls # as commands and call them on the current object - elsif !part_resolved and object.respond_to?(part) and ['size', 'first', 'last'].include?(part) + elsif !part_resolved and object.respond_to?(part) and ['size'.freeze, 'first'.freeze, 'last'.freeze].include?(part) object = object.send(part.intern).to_liquid diff --git a/lib/liquid/file_system.rb b/lib/liquid/file_system.rb index 3fb823a..f95f644 100644 --- a/lib/liquid/file_system.rb +++ b/lib/liquid/file_system.rb @@ -44,7 +44,7 @@ module Liquid class LocalFileSystem attr_accessor :root - def initialize(root, pattern = "_%s.liquid") + def initialize(root, pattern = "_%s.liquid".freeze) @root = root @pattern = pattern end @@ -59,7 +59,7 @@ module Liquid def full_path(template_path) raise FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ /\A[^.\/][a-zA-Z0-9_\/]+\z/ - full_path = if template_path.include?('/') + full_path = if template_path.include?('/'.freeze) File.join(root, File.dirname(template_path), @pattern % File.basename(template_path)) else File.join(root, @pattern % template_path) diff --git a/lib/liquid/i18n.rb b/lib/liquid/i18n.rb index 205b740..3a59b1f 100644 --- a/lib/liquid/i18n.rb +++ b/lib/liquid/i18n.rb @@ -6,7 +6,7 @@ module Liquid class TranslationError < StandardError end - + attr_reader :path def initialize(path = DEFAULT_LOCALE) @@ -31,7 +31,7 @@ module Liquid end def deep_fetch_translation(name) - name.split('.').reduce(locale) do |level, cur| + name.split('.'.freeze).reduce(locale) do |level, cur| level[cur] or raise TranslationError, "Translation for #{name} does not exist in locale #{path}" end end diff --git a/lib/liquid/interrupts.rb b/lib/liquid/interrupts.rb index d6bf61e..6ff01ac 100644 --- a/lib/liquid/interrupts.rb +++ b/lib/liquid/interrupts.rb @@ -5,7 +5,7 @@ module Liquid attr_reader :message def initialize(message=nil) - @message = message || "interrupt" + @message = message || "interrupt".freeze end end diff --git a/lib/liquid/lexer.rb b/lib/liquid/lexer.rb index 47a9fc4..0ecd8e0 100644 --- a/lib/liquid/lexer.rb +++ b/lib/liquid/lexer.rb @@ -2,14 +2,14 @@ require "strscan" module Liquid class Lexer SPECIALS = { - '|' => :pipe, - '.' => :dot, - ':' => :colon, - ',' => :comma, - '[' => :open_square, - ']' => :close_square, - '(' => :open_round, - ')' => :close_round + '|'.freeze => :pipe, + '.'.freeze => :dot, + ':'.freeze => :colon, + ','.freeze => :comma, + '['.freeze => :open_square, + ']'.freeze => :close_square, + '('.freeze => :open_round, + ')'.freeze => :close_round } IDENTIFIER = /[\w\-?!]+/ SINGLE_STRING_LITERAL = /'[^\']*'/ diff --git a/lib/liquid/parser.rb b/lib/liquid/parser.rb index 26f235f..d2006f3 100644 --- a/lib/liquid/parser.rb +++ b/lib/liquid/parser.rb @@ -66,10 +66,11 @@ module Liquid str = "" # might be a keyword argument (identifier: expression) if look(:id) && look(:colon, 1) - str << consume << consume << ' ' + str << consume << consume << ' '.freeze end str << expression + str end def variable_signature diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 6163b4c..3cc3cc8 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -4,12 +4,17 @@ require 'bigdecimal' module Liquid module StandardFilters - HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => ''' } + HTML_ESCAPE = { + '&'.freeze => '&'.freeze, + '>'.freeze => '>'.freeze, + '<'.freeze => '<'.freeze, + '"'.freeze => '"'.freeze, + "'".freeze => '''.freeze + } HTML_ESCAPE_ONCE_REGEXP = /["><']|&(?!([a-zA-Z]+|(#\d+));)/ # Return the size of an array or of an string def size(input) - input.respond_to?(:size) ? input.size : 0 end @@ -39,19 +44,19 @@ module Liquid alias_method :h, :escape # Truncate a string down to x characters - def truncate(input, length = 50, truncate_string = "...") + def truncate(input, length = 50, truncate_string = "...".freeze) if input.nil? then return end l = length.to_i - truncate_string.length l = 0 if l < 0 input.length > length.to_i ? input[0...l] + truncate_string : input end - def truncatewords(input, words = 15, truncate_string = "...") + def truncatewords(input, words = 15, truncate_string = "...".freeze) if input.nil? then return end wordlist = input.to_s.split l = words.to_i - 1 l = 0 if l < 0 - wordlist.length > l ? wordlist[0..l].join(" ") + truncate_string : input + wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string : input end # Split input string into an array of substrings separated by given pattern. @@ -76,16 +81,17 @@ module Liquid end def strip_html(input) - input.to_s.gsub(//m, '').gsub(//m, '').gsub(//m, '').gsub(/<.*?>/m, '') + empty = ''.freeze + input.to_s.gsub(//m, empty).gsub(//m, empty).gsub(//m, empty).gsub(/<.*?>/m, empty) end # Remove all newlines from the string def strip_newlines(input) - input.to_s.gsub(/\r?\n/, '') + input.to_s.gsub(/\r?\n/, ''.freeze) end # Join elements of the array with certain character between them - def join(input, glue = ' ') + def join(input, glue = ' '.freeze) [input].flatten.join(glue) end @@ -95,7 +101,7 @@ module Liquid ary = flatten_if_necessary(input) if property.nil? ary.sort - elsif ary.first.respond_to?('[]') and !ary.first[property].nil? + elsif ary.first.respond_to?('[]'.freeze) and !ary.first[property].nil? ary.sort {|a,b| a[property] <=> b[property] } elsif ary.first.respond_to?(property) ary.sort {|a,b| a.send(property) <=> b.send(property) } @@ -113,7 +119,7 @@ module Liquid flatten_if_necessary(input).map do |e| e = e.call if e.is_a?(Proc) - if property == "to_liquid" + if property == "to_liquid".freeze e elsif e.respond_to?(:[]) e[property] @@ -122,23 +128,23 @@ module Liquid end # Replace occurrences of a string with another - def replace(input, string, replacement = '') + def replace(input, string, replacement = ''.freeze) input.to_s.gsub(string, replacement.to_s) end # Replace the first occurrences of a string with another - def replace_first(input, string, replacement = '') + def replace_first(input, string, replacement = ''.freeze) input.to_s.sub(string, replacement.to_s) end # remove a substring def remove(input, string) - input.to_s.gsub(string, '') + input.to_s.gsub(string, ''.freeze) end # remove the first occurrences of a substring def remove_first(input, string) - input.to_s.sub(string, '') + input.to_s.sub(string, ''.freeze) end # add one string to another @@ -153,7 +159,7 @@ module Liquid # Add
tags in front of all newlines in input string def newline_to_br(input) - input.to_s.gsub(/\n/, "
\n") + input.to_s.gsub(/\n/, "
\n".freeze) end # Reformat a date @@ -196,7 +202,7 @@ module Liquid date = if input.is_a?(String) case input.downcase - when 'now', 'today' + when 'now'.freeze, 'today'.freeze Time.now else Time.parse(input) @@ -256,7 +262,7 @@ module Liquid apply_operation(input, operand, :%) end - def default(input, default_value = "") + def default(input, default_value = "".freeze) is_blank = input.respond_to?(:empty?) ? input.empty? : !input is_blank ? default_value : input end diff --git a/lib/liquid/tag.rb b/lib/liquid/tag.rb index 2daf084..3df1045 100644 --- a/lib/liquid/tag.rb +++ b/lib/liquid/tag.rb @@ -27,7 +27,7 @@ module Liquid end def render(context) - '' + ''.freeze end def blank? diff --git a/lib/liquid/tags/assign.rb b/lib/liquid/tags/assign.rb index 75301b1..fa852a0 100644 --- a/lib/liquid/tags/assign.rb +++ b/lib/liquid/tags/assign.rb @@ -17,7 +17,7 @@ module Liquid @to = $1 @from = Variable.new($2) else - raise SyntaxError.new options[:locale].t("errors.syntax.assign") + raise SyntaxError.new options[:locale].t("errors.syntax.assign".freeze) end end @@ -25,7 +25,7 @@ module Liquid val = @from.render(context) context.scopes.last[@to] = val context.increment_used_resources(:assign_score_current, val) - '' + ''.freeze end def blank? @@ -33,5 +33,5 @@ module Liquid end end - Template.register_tag('assign', Assign) + Template.register_tag('assign'.freeze, Assign) end diff --git a/lib/liquid/tags/break.rb b/lib/liquid/tags/break.rb index 0626338..411a3b1 100644 --- a/lib/liquid/tags/break.rb +++ b/lib/liquid/tags/break.rb @@ -17,5 +17,5 @@ module Liquid end - Template.register_tag('break', Break) + Template.register_tag('break'.freeze, Break) end diff --git a/lib/liquid/tags/capture.rb b/lib/liquid/tags/capture.rb index 9acb22b..4bcd118 100644 --- a/lib/liquid/tags/capture.rb +++ b/lib/liquid/tags/capture.rb @@ -27,7 +27,7 @@ module Liquid output = super context.scopes.last[@to] = output context.increment_used_resources(:assign_score_current, output) - '' + ''.freeze end def blank? @@ -35,5 +35,5 @@ module Liquid end end - Template.register_tag('capture', Capture) + Template.register_tag('capture'.freeze, Capture) end diff --git a/lib/liquid/tags/case.rb b/lib/liquid/tags/case.rb index c63548e..1cc48d8 100644 --- a/lib/liquid/tags/case.rb +++ b/lib/liquid/tags/case.rb @@ -10,7 +10,7 @@ module Liquid if markup =~ Syntax @left = $1 else - raise SyntaxError.new(options[:locale].t("errors.syntax.case")) + raise SyntaxError.new(options[:locale].t("errors.syntax.case".freeze)) end end @@ -21,9 +21,9 @@ module Liquid def unknown_tag(tag, markup, tokens) @nodelist = [] case tag - when 'when' + when 'when'.freeze record_when_condition(markup) - when 'else' + when 'else'.freeze record_else_condition(markup) else super @@ -53,12 +53,12 @@ module Liquid while markup # Create a new nodelist and assign it to the new block if not markup =~ WhenSyntax - raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_when")) + raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_when".freeze)) end markup = $2 - block = Condition.new(@left, '==', $1) + block = Condition.new(@left, '=='.freeze, $1) block.attach(@nodelist) @blocks.push(block) end @@ -66,7 +66,7 @@ module Liquid def record_else_condition(markup) if not markup.strip.empty? - raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_else")) + raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_else".freeze)) end block = ElseCondition.new @@ -75,5 +75,5 @@ module Liquid end end - Template.register_tag('case', Case) + Template.register_tag('case'.freeze, Case) end diff --git a/lib/liquid/tags/comment.rb b/lib/liquid/tags/comment.rb index 099310e..3e6bb69 100644 --- a/lib/liquid/tags/comment.rb +++ b/lib/liquid/tags/comment.rb @@ -1,7 +1,7 @@ module Liquid class Comment < Block def render(context) - '' + ''.freeze end def unknown_tag(tag, markup, tokens) @@ -12,5 +12,5 @@ module Liquid end end - Template.register_tag('comment', Comment) + Template.register_tag('comment'.freeze, Comment) end diff --git a/lib/liquid/tags/continue.rb b/lib/liquid/tags/continue.rb index 0512a7d..9c81ec2 100644 --- a/lib/liquid/tags/continue.rb +++ b/lib/liquid/tags/continue.rb @@ -1,5 +1,4 @@ module Liquid - # Continue tag to be used to break out of a for loop. # # == Basic Usage: @@ -10,12 +9,10 @@ module Liquid # {% endfor %} # class Continue < Tag - def interrupt ContinueInterrupt.new end - end - Template.register_tag('continue', Continue) + Template.register_tag('continue'.freeze, Continue) end diff --git a/lib/liquid/tags/cycle.rb b/lib/liquid/tags/cycle.rb index c86f8a1..c3c7d87 100644 --- a/lib/liquid/tags/cycle.rb +++ b/lib/liquid/tags/cycle.rb @@ -25,7 +25,7 @@ module Liquid @variables = variables_from_string(markup) @name = "'#{@variables.to_s}'" else - raise SyntaxError.new(options[:locale].t("errors.syntax.cycle")) + raise SyntaxError.new(options[:locale].t("errors.syntax.cycle".freeze)) end end diff --git a/lib/liquid/tags/decrement.rb b/lib/liquid/tags/decrement.rb index e9b6606..d604750 100644 --- a/lib/liquid/tags/decrement.rb +++ b/lib/liquid/tags/decrement.rb @@ -34,5 +34,5 @@ module Liquid private end - Template.register_tag('decrement', Decrement) + Template.register_tag('decrement'.freeze, Decrement) end diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index 3cd9e97..6d29b64 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -61,7 +61,7 @@ module Liquid end def unknown_tag(tag, markup, tokens) - return super unless tag == 'else' + return super unless tag == 'else'.freeze @nodelist = @else_block = [] end @@ -74,13 +74,13 @@ module Liquid # Maintains Ruby 1.8.7 String#each behaviour on 1.9 return render_else(context) unless iterable?(collection) - from = if @attributes['offset'] == 'continue' + from = if @attributes['offset'.freeze] == 'continue'.freeze context.registers[:for][@name].to_i else - context[@attributes['offset']].to_i + context[@attributes['offset'.freeze]].to_i end - limit = context[@attributes['limit']] + limit = context[@attributes['limit'.freeze]] to = limit ? limit.to_i + from : nil segment = Utils.slice_collection(collection, from, to) @@ -99,15 +99,16 @@ module Liquid context.stack do segment.each_with_index do |item, index| context[@variable_name] = item - context['forloop'] = { - 'name' => @name, - 'length' => length, - 'index' => index + 1, - 'index0' => index, - 'rindex' => length - index, - 'rindex0' => length - index - 1, - 'first' => (index == 0), - 'last' => (index == length - 1) } + context['forloop'.freeze] = { + 'name'.freeze => @name, + 'length'.freeze => length, + 'index'.freeze => index + 1, + 'index0'.freeze => index, + 'rindex'.freeze => length - index, + 'rindex0'.freeze => length - index - 1, + 'first'.freeze => (index == 0), + 'last'.freeze => (index == length - 1) + } result << render_all(@for_block, context) @@ -135,22 +136,22 @@ module Liquid @attributes[key] = value end else - raise SyntaxError.new(options[:locale].t("errors.syntax.for")) + raise SyntaxError.new(options[:locale].t("errors.syntax.for".freeze)) end end def strict_parse(markup) p = Parser.new(markup) @variable_name = p.consume(:id) - raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_in")) unless p.id?('in') + raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_in".freeze)) unless p.id?('in'.freeze) @collection_name = p.expression @name = "#{@variable_name}-#{@collection_name}" - @reversed = p.id?('reversed') + @reversed = p.id?('reversed'.freeze) @attributes = {} while p.look(:id) && p.look(:colon, 1) - unless attribute = p.id?('limit') || p.id?('offset') - raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_attribute")) + unless attribute = p.id?('limit'.freeze) || p.id?('offset'.freeze) + raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_attribute".freeze)) end p.consume val = p.expression @@ -161,14 +162,14 @@ module Liquid private - def render_else(context) - return @else_block ? [render_all(@else_block, context)] : '' - end + def render_else(context) + return @else_block ? [render_all(@else_block, context)] : ''.freeze + end - def iterable?(collection) - collection.respond_to?(:each) || Utils.non_blank_string?(collection) - end + def iterable?(collection) + collection.respond_to?(:each) || Utils.non_blank_string?(collection) + end end - Template.register_tag('for', For) + Template.register_tag('for'.freeze, For) end diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index ad48bb5..60bd634 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -17,7 +17,7 @@ module Liquid def initialize(tag_name, markup, options) super @blocks = [] - push_block('if', markup) + push_block('if'.freeze, markup) end def nodelist @@ -25,7 +25,7 @@ module Liquid end def unknown_tag(tag, markup, tokens) - if ['elsif', 'else'].include?(tag) + if ['elsif'.freeze, 'else'.freeze].include?(tag) push_block(tag, markup) else super @@ -39,14 +39,14 @@ module Liquid return render_all(block.attachment, context) end end - '' + ''.freeze end end private def push_block(tag, markup) - block = if tag == 'else' + block = if tag == 'else'.freeze ElseCondition.new else parse_with_selected_parser(markup) @@ -58,17 +58,17 @@ module Liquid def lax_parse(markup) expressions = markup.scan(ExpressionsAndOperators).reverse - raise(SyntaxError.new(options[:locale].t("errors.syntax.if"))) unless expressions.shift =~ Syntax + raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.shift =~ Syntax condition = Condition.new($1, $2, $3) while not expressions.empty? operator = (expressions.shift).to_s.strip - raise(SyntaxError.new(options[:locale].t("errors.syntax.if"))) unless expressions.shift.to_s =~ Syntax + raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.shift.to_s =~ Syntax new_condition = Condition.new($1, $2, $3) - raise(SyntaxError.new(options[:locale].t("errors.syntax.if"))) unless BOOLEAN_OPERATORS.include?(operator) + raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless BOOLEAN_OPERATORS.include?(operator) new_condition.send(operator, condition) condition = new_condition end @@ -81,7 +81,7 @@ module Liquid condition = parse_comparison(p) - while op = (p.id?('and') || p.id?('or')) + while op = (p.id?('and'.freeze) || p.id?('or'.freeze)) new_cond = parse_comparison(p) new_cond.send(op, condition) condition = new_cond @@ -102,5 +102,5 @@ module Liquid end end - Template.register_tag('if', If) + Template.register_tag('if'.freeze, If) end diff --git a/lib/liquid/tags/ifchanged.rb b/lib/liquid/tags/ifchanged.rb index c18a5c5..03a9fb0 100644 --- a/lib/liquid/tags/ifchanged.rb +++ b/lib/liquid/tags/ifchanged.rb @@ -10,11 +10,11 @@ module Liquid context.registers[:ifchanged] = output output else - '' + ''.freeze end end end end - Template.register_tag('ifchanged', Ifchanged) + Template.register_tag('ifchanged'.freeze, Ifchanged) end diff --git a/lib/liquid/tags/include.rb b/lib/liquid/tags/include.rb index 34b2b0b..d831932 100644 --- a/lib/liquid/tags/include.rb +++ b/lib/liquid/tags/include.rb @@ -31,7 +31,7 @@ module Liquid end else - raise SyntaxError.new(options[:locale].t("errors.syntax.include")) + raise SyntaxError.new(options[:locale].t("errors.syntax.include".freeze)) end end @@ -51,7 +51,7 @@ module Liquid context[key] = context[value] end - context_variable_name = @template_name[1..-2].split('/').last + context_variable_name = @template_name[1..-2].split('/'.freeze).last if variable.is_a?(Array) variable.collect do |var| context[context_variable_name] = var @@ -94,5 +94,5 @@ module Liquid end end - Template.register_tag('include', Include) + Template.register_tag('include'.freeze, Include) end diff --git a/lib/liquid/tags/increment.rb b/lib/liquid/tags/increment.rb index 251bd77..4592d1c 100644 --- a/lib/liquid/tags/increment.rb +++ b/lib/liquid/tags/increment.rb @@ -31,5 +31,5 @@ module Liquid end end - Template.register_tag('increment', Increment) + Template.register_tag('increment'.freeze, Increment) end diff --git a/lib/liquid/tags/raw.rb b/lib/liquid/tags/raw.rb index eafe6d3..0980ee1 100644 --- a/lib/liquid/tags/raw.rb +++ b/lib/liquid/tags/raw.rb @@ -7,7 +7,7 @@ module Liquid @nodelist.clear while token = tokens.shift if token =~ FullTokenPossiblyInvalid - @nodelist << $1 if $1 != "" + @nodelist << $1 if $1 != "".freeze if block_delimiter == $2 end_tag return @@ -18,5 +18,5 @@ module Liquid end end - Template.register_tag('raw', Raw) + Template.register_tag('raw'.freeze, Raw) end diff --git a/lib/liquid/tags/table_row.rb b/lib/liquid/tags/table_row.rb index 771042d..25457a3 100644 --- a/lib/liquid/tags/table_row.rb +++ b/lib/liquid/tags/table_row.rb @@ -12,21 +12,21 @@ module Liquid @attributes[key] = value end else - raise SyntaxError.new(options[:locale].t("errors.syntax.table_row")) + raise SyntaxError.new(options[:locale].t("errors.syntax.table_row".freeze)) end end def render(context) - collection = context[@collection_name] or return '' + collection = context[@collection_name] or return ''.freeze - from = @attributes['offset'] ? context[@attributes['offset']].to_i : 0 - to = @attributes['limit'] ? from + context[@attributes['limit']].to_i : nil + from = @attributes['offset'.freeze] ? context[@attributes['offset'.freeze]].to_i : 0 + to = @attributes['limit'.freeze] ? from + context[@attributes['limit'.freeze]].to_i : nil collection = Utils.slice_collection(collection, from, to) length = collection.length - cols = context[@attributes['cols']].to_i + cols = context[@attributes['cols'.freeze]].to_i row = 1 col = 0 @@ -36,19 +36,19 @@ module Liquid collection.each_with_index do |item, index| context[@variable_name] = item - context['tablerowloop'] = { - 'length' => length, - 'index' => index + 1, - 'index0' => index, - 'col' => col + 1, - 'col0' => col, - 'index0' => index, - 'rindex' => length - index, - 'rindex0' => length - index - 1, - 'first' => (index == 0), - 'last' => (index == length - 1), - 'col_first' => (col == 0), - 'col_last' => (col == cols - 1) + context['tablerowloop'.freeze] = { + 'length'.freeze => length, + 'index'.freeze => index + 1, + 'index0'.freeze => index, + 'col'.freeze => col + 1, + 'col0'.freeze => col, + 'index0'.freeze => index, + 'rindex'.freeze => length - index, + 'rindex0'.freeze => length - index - 1, + 'first'.freeze => (index == 0), + 'last'.freeze => (index == length - 1), + 'col_first'.freeze => (col == 0), + 'col_last'.freeze => (col == cols - 1) } @@ -69,5 +69,5 @@ module Liquid end end - Template.register_tag('tablerow', TableRow) + Template.register_tag('tablerow'.freeze, TableRow) end diff --git a/lib/liquid/tags/unless.rb b/lib/liquid/tags/unless.rb index e561481..eb8a731 100644 --- a/lib/liquid/tags/unless.rb +++ b/lib/liquid/tags/unless.rb @@ -1,7 +1,6 @@ require File.dirname(__FILE__) + '/if' module Liquid - # Unless is a conditional just like 'if' but works on the inverse logic. # # {% unless x < 0 %} x is greater than zero {% end %} @@ -23,11 +22,10 @@ module Liquid end end - '' + ''.freeze end end end - - Template.register_tag('unless', Unless) + Template.register_tag('unless'.freeze, Unless) end diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 4ca7961..13748d4 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -110,7 +110,7 @@ module Liquid # filters and tags and might be useful to integrate liquid more with its host application # def render(*args) - return '' if @root.nil? + return ''.freeze if @root.nil? context = case args.first when Liquid::Context diff --git a/lib/liquid/utils.rb b/lib/liquid/utils.rb index cb96524..cfa675a 100644 --- a/lib/liquid/utils.rb +++ b/lib/liquid/utils.rb @@ -10,7 +10,7 @@ module Liquid end def self.non_blank_string?(collection) - collection.is_a?(String) && collection != '' + collection.is_a?(String) && collection != ''.freeze end def self.slice_collection_using_each(collection, from, to) diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 3992d2a..9a9e387 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -19,7 +19,6 @@ module Liquid @markup = markup @name = nil @options = options || {} - case @options[:error_mode] || Template.error_mode when :strict then strict_parse(markup) @@ -63,7 +62,7 @@ module Liquid @filters = [] p = Parser.new(markup) # Could be just filters with no input - @name = p.look(:pipe) ? '' : p.expression + @name = p.look(:pipe) ? ''.freeze : p.expression while p.consume?(:pipe) filtername = p.consume(:id) filterargs = p.consume?(:colon) ? parse_filterargs(p) : [] @@ -86,7 +85,7 @@ module Liquid end def render(context) - return '' if @name.nil? + return ''.freeze if @name.nil? @filters.inject(context[@name]) do |output, filter| filterargs = [] keyword_args = {} diff --git a/lib/liquid/version.rb b/lib/liquid/version.rb index d27c6d3..879cd12 100644 --- a/lib/liquid/version.rb +++ b/lib/liquid/version.rb @@ -1,4 +1,4 @@ # encoding: utf-8 module Liquid - VERSION = "3.0.0" + VERSION = "3.0.0".freeze end