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
# File 'lib/drg/ruby/condition.rb', line 7

def initialize(sexp)
  @sexp = sexp
  @statement = Ruby2Ruby.new.process(sexp.deep_clone)
  @nested_conditions = create_nested_conditions
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

#create_nested_conditionsObject

Private



62
63
64
65
66
67
68
69
70
71
# File 'lib/drg/ruby/condition.rb', line 62

def create_nested_conditions
  nc = Set.new
  s = sexp.drop(1)
  s.flatten.include?(:if) && s.deep_each do |exp|
    DRG::Decorators::SexpDecorator.new(exp).each_sexp_condition do |node|
      nc << self.class.new(node)
    end
  end
  nc.to_a
end

#edit(txt) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/drg/ruby/condition.rb', line 35

def edit(txt)
  txt.sub! /^return\s*/, 'returns '
  txt.sub! /^returns\s*$/, 'returns nil'
  if txt.split(/\s/).length == 1
    txt = "returns #{txt}"
  elsif txt.include?(' = ')
    txt = "assigns #{txt}"
  end
  txt.strip
end

#eql?(other) ⇒ Boolean

Set related stuff

Returns:

  • (Boolean)


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

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

#find_return_valueObject



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

def find_return_value
  if @statement =~ /\s+\?\s+(.*?)(:|$)/
    $1.strip
  elsif @statement[/then\n(.+)\nend/]
    $1.strip
  else
    @statement[/(.*?)(unless|if)/, 1].to_s.strip
  end
end

#hashObject



54
55
56
# File 'lib/drg/ruby/condition.rb', line 54

def hash
  sexp.object_id
end

#return_valueObject



21
22
23
# File 'lib/drg/ruby/condition.rb', line 21

def return_value
  edit find_return_value
end

#short_statementObject



13
14
15
16
17
18
19
# File 'lib/drg/ruby/condition.rb', line 13

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