File: array_buffer.rb

package info (click to toggle)
ruby-temple 0.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 476 kB
  • sloc: ruby: 3,347; makefile: 6
file content (30 lines) | stat: -rw-r--r-- 753 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true
module Temple
  module Generators
    # Just like Array, but calls #join on the array.
    #
    #   _buf = []
    #   _buf << "static"
    #   _buf << dynamic
    #   _buf.join("")
    #
    # @api public
    class ArrayBuffer < Array
      def call(exp)
        case exp.first
        when :static
          [save_buffer, "#{buffer} = #{exp.last.inspect}", restore_buffer].compact.join('; ')
        when :dynamic
          [save_buffer, "#{buffer} = (#{exp.last}).to_s", restore_buffer].compact.join('; ')
        else
          super
        end
      end

      def return_buffer
        freeze = options[:freeze_static] ? '.freeze' : ''
        "#{buffer} = #{buffer}.join(\"\"#{freeze})"
      end
    end
  end
end