Class: SyntaxTree::Case
Overview
Case represents the beginning of a case chain.
case value
when 1
"one"
when 2
"two"
else
"number"
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#consequent ⇒ Object
readonly
- In | When
-
the next clause in the chain.
-
#keyword ⇒ Object
readonly
- Kw
-
the keyword that opens this expression.
-
#value ⇒ Object
readonly
- nil | untyped
-
optional value being switched on.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(keyword: nil, value: nil, consequent: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(keyword:, value:, consequent:, location:) ⇒ Case
constructor
A new instance of Case.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(keyword:, value:, consequent:, location:) ⇒ Case
Returns a new instance of Case.
3097 3098 3099 3100 3101 3102 3103 |
# File 'lib/syntax_tree/node.rb', line 3097 def initialize(keyword:, value:, consequent:, location:) @keyword = keyword @value = value @consequent = consequent @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
3095 3096 3097 |
# File 'lib/syntax_tree/node.rb', line 3095 def comments @comments end |
#consequent ⇒ Object (readonly)
- In | When
-
the next clause in the chain
3092 3093 3094 |
# File 'lib/syntax_tree/node.rb', line 3092 def consequent @consequent end |
#keyword ⇒ Object (readonly)
- Kw
-
the keyword that opens this expression
3086 3087 3088 |
# File 'lib/syntax_tree/node.rb', line 3086 def keyword @keyword end |
#value ⇒ Object (readonly)
- nil | untyped
-
optional value being switched on
3089 3090 3091 |
# File 'lib/syntax_tree/node.rb', line 3089 def value @value end |
Instance Method Details
#===(other) ⇒ Object
3155 3156 3157 3158 |
# File 'lib/syntax_tree/node.rb', line 3155 def ===(other) other.is_a?(Case) && keyword === other.keyword && value === other.value && consequent === other.consequent end |
#accept(visitor) ⇒ Object
3105 3106 3107 |
# File 'lib/syntax_tree/node.rb', line 3105 def accept(visitor) visitor.visit_case(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
3109 3110 3111 |
# File 'lib/syntax_tree/node.rb', line 3109 def child_nodes [keyword, value, consequent] end |
#copy(keyword: nil, value: nil, consequent: nil, location: nil) ⇒ Object
3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 |
# File 'lib/syntax_tree/node.rb', line 3113 def copy(keyword: nil, value: nil, consequent: nil, location: nil) node = Case.new( keyword: keyword || self.keyword, value: value || self.value, consequent: consequent || self.consequent, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end |
#deconstruct_keys(_keys) ⇒ Object
3128 3129 3130 3131 3132 3133 3134 3135 3136 |
# File 'lib/syntax_tree/node.rb', line 3128 def deconstruct_keys(_keys) { keyword: keyword, value: value, consequent: consequent, location: location, comments: comments } end |
#format(q) ⇒ Object
3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 |
# File 'lib/syntax_tree/node.rb', line 3138 def format(q) q.group do q.format(keyword) if value q.text(" ") q.format(value) end q.breakable_force q.format(consequent) q.breakable_force q.text("end") end end |