Class: SyntaxTree::Elsif
Overview
Elsif represents another clause in an if or unless chain.
if variable
elsif other_variable
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#consequent ⇒ Object
readonly
- nil | Elsif | Else
-
the next clause in the chain.
-
#predicate ⇒ Object
readonly
- Node
-
the expression to be checked.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to be executed.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(predicate: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(predicate:, statements:, consequent:, location:) ⇒ Elsif
constructor
A new instance of Elsif.
Methods inherited from Node
#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid
Constructor Details
#initialize(predicate:, statements:, consequent:, location:) ⇒ Elsif
Returns a new instance of Elsif.
4893 4894 4895 4896 4897 4898 4899 |
# File 'lib/syntax_tree/node.rb', line 4893 def initialize(predicate:, statements:, consequent:, location:) @predicate = predicate @statements = statements @consequent = consequent @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
4891 4892 4893 |
# File 'lib/syntax_tree/node.rb', line 4891 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Elsif | Else
-
the next clause in the chain
4888 4889 4890 |
# File 'lib/syntax_tree/node.rb', line 4888 def consequent @consequent end |
#predicate ⇒ Object (readonly)
- Node
-
the expression to be checked
4882 4883 4884 |
# File 'lib/syntax_tree/node.rb', line 4882 def predicate @predicate end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
4885 4886 4887 |
# File 'lib/syntax_tree/node.rb', line 4885 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
4957 4958 4959 4960 |
# File 'lib/syntax_tree/node.rb', line 4957 def ===(other) other.is_a?(Elsif) && predicate === other.predicate && statements === other.statements && consequent === other.consequent end |
#accept(visitor) ⇒ Object
4901 4902 4903 |
# File 'lib/syntax_tree/node.rb', line 4901 def accept(visitor) visitor.visit_elsif(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
4905 4906 4907 |
# File 'lib/syntax_tree/node.rb', line 4905 def child_nodes [predicate, statements, consequent] end |
#copy(predicate: nil, statements: nil, consequent: nil, location: nil) ⇒ Object
4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 |
# File 'lib/syntax_tree/node.rb', line 4909 def copy(predicate: nil, statements: nil, consequent: nil, location: nil) node = Elsif.new( predicate: predicate || self.predicate, statements: statements || self.statements, consequent: consequent || self.consequent, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
4924 4925 4926 4927 4928 4929 4930 4931 4932 |
# File 'lib/syntax_tree/node.rb', line 4924 def deconstruct_keys(_keys) { predicate: predicate, statements: statements, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 |
# File 'lib/syntax_tree/node.rb', line 4934 def format(q) q.group do q.group do q.text("elsif ") q.nest("elsif".length - 1) { q.format(predicate) } end unless statements.empty? q.indent do q.breakable_force q.format(statements) end end if consequent q.group do q.breakable_force q.format(consequent) end end end end |