Class: DRG::Ruby::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/drg/ruby/condition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sexp) ⇒ Condition

Returns a new instance of Condition.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/drg/ruby/condition.rb', line 7

def initialize(sexp)
  @sexp = sexp
  @statement = Ruby2Ruby.new.process(sexp.deep_clone)
  @nested_conditions = Set.new
  sexp.drop(1).flatten.include?(:if) && sexp.drop(1).deep_each do |exp|
    DRG::Decorators::SexpDecorator.new(exp).each_sexp_condition do |node|
      @nested_conditions << self.class.new(node)
    end
  end
  @nested_conditions = @nested_conditions.to_a
end

Instance Attribute Details

#nested_conditionsObject (readonly)

Returns the value of attribute nested_conditions.



5
6
7
# File 'lib/drg/ruby/condition.rb', line 5

def nested_conditions
  @nested_conditions
end

#sexpObject (readonly)

Returns the value of attribute sexp.



5
6
7
# File 'lib/drg/ruby/condition.rb', line 5

def sexp
  @sexp
end

#statementObject (readonly)

Returns the value of attribute statement.



5
6
7
# File 'lib/drg/ruby/condition.rb', line 5

def statement
  @statement
end

Instance Method Details

#eql?(other) ⇒ Boolean

Set related stuff

Returns:

  • (Boolean)


45
46
47
# File 'lib/drg/ruby/condition.rb', line 45

def eql?(other)
  hash == other.hash
end

#hashObject



49
50
51
# File 'lib/drg/ruby/condition.rb', line 49

def hash
  sexp.object_id
end

#return_valueObject



27
28
29
30
31
32
33
# File 'lib/drg/ruby/condition.rb', line 27

def return_value
  if @statement =~ /\s+\?\s+(.*?)(:|$)/
    $1.strip
  else
    translate @statement[/(.*?)(unless|if)/, 1].to_s.strip
  end
end

#short_statementObject



19
20
21
22
23
24
25
# File 'lib/drg/ruby/condition.rb', line 19

def short_statement
  if @statement =~ /(unless|if)(.+)$/
    ($1 << $2).strip
  elsif @statement =~ /(.*?)\s+\?\s+/
    $1.strip
  end
end

#translate(txt) ⇒ Object



35
36
37
38
39
# File 'lib/drg/ruby/condition.rb', line 35

def translate(txt)
  txt.sub! /^return\s*/, 'returns '
  txt.sub! /^returns\s*$/, 'returns nil'
  txt.strip
end