Class: Estreet::BlockStatement

Inherits:
Statement show all
Defined in:
lib/estreet/block_statement.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#source_location

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Statement

#to_statement

Methods inherited from Node

#as_json, #loc, #type

Constructor Details

#initialize(*statements) ⇒ BlockStatement

Returns a new instance of BlockStatement.



3
4
5
6
7
8
# File 'lib/estreet/block_statement.rb', line 3

def initialize(*statements)
  @statements = statements.map do |st|
    st.to_statement
    # raise TypeError, "Not a statement: #{st}" unless st.is_a? Statement
  end
end

Instance Attribute Details

#statementsObject (readonly)

Returns the value of attribute statements.



35
36
37
# File 'lib/estreet/block_statement.rb', line 35

def statements
  @statements
end

Class Method Details

.flatten(*statements) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/estreet/block_statement.rb', line 23

def self.flatten(*statements)
  new(*statements.flat_map do |stmt|
    if stmt.is_a? self
      stmt.statements
    else
      stmt.to_statement
    end
  end)
rescue NoMethodError => e
  raise TypeError, "Can't convert to statement: #{statements.inspect}"
end

Instance Method Details

#<<(statement) ⇒ Object

Add another statement to the end of the receiver block



19
20
21
# File 'lib/estreet/block_statement.rb', line 19

def <<(statement)
  self.class.flatten(self, statement)
end

#attributesObject



10
11
12
# File 'lib/estreet/block_statement.rb', line 10

def attributes
  super.merge(body: @statements)
end

#to_blockObject



14
15
16
# File 'lib/estreet/block_statement.rb', line 14

def to_block
  self
end