File: assignment.rb

package info (click to toggle)
ruby-unparser 0.6.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 936 kB
  • sloc: ruby: 7,691; sh: 6; makefile: 4
file content (76 lines) | stat: -rw-r--r-- 1,319 bytes parent folder | download
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true

module Unparser
  class Emitter

    # Base class for assignment emitters
    class Assignment < self
      BINARY_OPERATOR = %i[and or].freeze

      def symbol_name
        true
      end

      def emit_heredoc_reminders
        return unless right

        emitter(right).emit_heredoc_reminders
      end

    private

      def dispatch
        emit_left
        emit_right
      end

      def emit_right
        return unless right

        write(' = ')

        if BINARY_OPERATOR.include?(right.type)
          writer_with(Writer::Binary, right).emit_operator
        else
          visit(right)
        end
      end

      abstract_method :emit_left

      # Variable assignment emitter
      class Variable < self

        handle :lvasgn, :ivasgn, :cvasgn, :gvasgn

        children :name, :right

      private

        def emit_left
          write(name.to_s)
        end

      end # Variable

      # Constant assignment emitter
      class Constant < self

        handle :casgn

        children :base, :name, :right

      private

        def emit_left
          if base
            visit(base)
            write('::') unless n_cbase?(base)
          end
          write(name.to_s)
        end

      end # Constant
    end # Assignment
  end # Emitter
end # Unparser