Class: TypedRb::Model::TmCaseWhen

Inherits:
Expr show all
Defined in:
lib/typed/model/tm_case_when.rb

Instance Attribute Summary collapse

Attributes inherited from Expr

#col, #line, #node, #type

Instance Method Summary collapse

Constructor Details

#initialize(node, case_statement, when_statements, default_statement) ⇒ TmCaseWhen

Returns a new instance of TmCaseWhen.



8
9
10
11
12
13
# File 'lib/typed/model/tm_case_when.rb', line 8

def initialize(node, case_statement, when_statements, default_statement)
  super(node, nil)
  @case_statement = case_statement
  @when_statements = when_statements
  @default_statement = default_statement
end

Instance Attribute Details

#case_statementObject (readonly)

Returns the value of attribute case_statement.



7
8
9
# File 'lib/typed/model/tm_case_when.rb', line 7

def case_statement
  @case_statement
end

#default_statementObject (readonly)

Returns the value of attribute default_statement.



7
8
9
# File 'lib/typed/model/tm_case_when.rb', line 7

def default_statement
  @default_statement
end

#when_statementsObject (readonly)

Returns the value of attribute when_statements.



7
8
9
# File 'lib/typed/model/tm_case_when.rb', line 7

def when_statements
  @when_statements
end

Instance Method Details

#check_type(context) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/typed/model/tm_case_when.rb', line 15

def check_type(context)
  conditions = build_conditionals(case_statement, when_statements)
  conditions = conditions.reduce([]) do |acc, (node, condition, then_statement)|
    next_condition = TmIfElse.new(node, condition, then_statement, nil)
    prev_condition = acc.last
    prev_condition.else_expr = next_condition unless prev_condition.nil?
    acc << next_condition
  end
  conditions.last.else_expr = default_statement if default_statement
  conditions.first.check_type(context)
end