Class: Ryan::Condition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sexp) ⇒ Condition

Returns a new instance of Condition.



8
9
10
11
12
# File 'lib/ryan/condition.rb', line 8

def initialize(sexp)
  @sexp = sexp
  @nested_conditions = create_nested_conditions
  @parts = load_parts
end

Instance Attribute Details

#nested_conditionsObject (readonly)

Returns the value of attribute nested_conditions.



6
7
8
# File 'lib/ryan/condition.rb', line 6

def nested_conditions
  @nested_conditions
end

#partsObject (readonly)

Returns the value of attribute parts.



6
7
8
# File 'lib/ryan/condition.rb', line 6

def parts
  @parts
end

#sexpObject (readonly)

Returns the value of attribute sexp.



6
7
8
# File 'lib/ryan/condition.rb', line 6

def sexp
  @sexp
end

Instance Method Details

#create_nested_conditionsObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ryan/condition.rb', line 89

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

#edit_return_text(txt) ⇒ Object

Private



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ryan/condition.rb', line 60

def edit_return_text(txt)
  txt = txt.to_s
  txt = txt.sub /^return\b/, 'returns'
  txt = txt.sub /^returns\s*$/, 'returns nil'
  if txt.include?(' = ')
    txt = "assigns #{txt}"
  elsif !txt.empty? and txt !~ /^return/
    txt = "returns #{txt.strip}"
  end
  txt.strip
end

#edit_statement(txt) ⇒ Object



72
73
74
75
76
# File 'lib/ryan/condition.rb', line 72

def edit_statement(txt)
  txt = txt[/(.+)\n?/, 1] # take first line
  txt.prepend 'unless ' if sexp[2].nil? # this is an unless statement
  txt
end

#else_sexpObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ryan/condition.rb', line 44

def else_sexp
  if sexp.compact[3].nil?
    nil
  elsif sexp[3].first == :block
    sexp[3].last
  elsif sexp[3].first == :if
    nil
  else
    sexp[3]
  end
end

#else_textObject



26
27
28
# File 'lib/ryan/condition.rb', line 26

def else_text
  edit_return_text Ruby2Ruby.new.process(else_sexp.deep_clone)
end

#eql?(other) ⇒ Boolean

Set comparison operators

Returns:

  • (Boolean)


106
107
108
# File 'lib/ryan/condition.rb', line 106

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

#full_statementObject



14
15
16
# File 'lib/ryan/condition.rb', line 14

def full_statement
  @full_statement ||= Ruby2Ruby.new.process(sexp.deep_clone)
end

#hashObject



110
111
112
# File 'lib/ryan/condition.rb', line 110

def hash
  sexp.object_id
end

#if_sexpObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ryan/condition.rb', line 30

def if_sexp
  if sexp[2].nil?
    sexp.last
  elsif sexp[2].first == :if
    nil
  elsif sexp[2].first == :block
    sexp[2].last
  elsif sexp[2].first == :rescue
    sexp[2][1].last
  else
    sexp[2]
  end
end

#if_textObject



22
23
24
# File 'lib/ryan/condition.rb', line 22

def if_text
  edit_return_text Ruby2Ruby.new.process(if_sexp.deep_clone)
end

#load_partsObject



79
80
81
82
83
84
85
86
87
# File 'lib/ryan/condition.rb', line 79

def load_parts
  condition_parts = Set.new
  sexp.each_sexp do |s|
    if s.first == :if
      condition_parts << self.class.new(s)
    end
  end
  condition_parts.to_a
end

#statementObject



18
19
20
# File 'lib/ryan/condition.rb', line 18

def statement
  edit_statement Ruby2Ruby.new.process(sexp[1].deep_clone)
end