Class: Ryan::Condition
- Inherits:
-
Object
- Object
- Ryan::Condition
- Defined in:
- lib/ryan/condition.rb
Instance Attribute Summary collapse
-
#nested_conditions ⇒ Object
readonly
Returns the value of attribute nested_conditions.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#sexp ⇒ Object
readonly
Returns the value of attribute sexp.
Instance Method Summary collapse
- #create_nested_conditions ⇒ Object
-
#edit_return_text(txt) ⇒ Object
Private.
- #edit_statement(txt) ⇒ Object
- #else_sexp ⇒ Object
- #else_text ⇒ Object
-
#eql?(other) ⇒ Boolean
Set comparison operators.
- #full_statement ⇒ Object
- #hash ⇒ Object
- #if_sexp ⇒ Object
- #if_text ⇒ Object
-
#initialize(sexp) ⇒ Condition
constructor
A new instance of Condition.
- #load_parts ⇒ Object
- #statement ⇒ Object
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_conditions ⇒ Object (readonly)
Returns the value of attribute nested_conditions.
6 7 8 |
# File 'lib/ryan/condition.rb', line 6 def nested_conditions @nested_conditions end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
6 7 8 |
# File 'lib/ryan/condition.rb', line 6 def parts @parts end |
#sexp ⇒ Object (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_conditions ⇒ Object
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_sexp ⇒ Object
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_text ⇒ Object
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
106 107 108 |
# File 'lib/ryan/condition.rb', line 106 def eql?(other) hash == other.hash end |
#full_statement ⇒ Object
14 15 16 |
# File 'lib/ryan/condition.rb', line 14 def full_statement @full_statement ||= Ruby2Ruby.new.process(sexp.deep_clone) end |
#hash ⇒ Object
110 111 112 |
# File 'lib/ryan/condition.rb', line 110 def hash sexp.object_id end |
#if_sexp ⇒ Object
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_text ⇒ Object
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_parts ⇒ Object
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 |
#statement ⇒ Object
18 19 20 |
# File 'lib/ryan/condition.rb', line 18 def statement edit_statement Ruby2Ruby.new.process(sexp[1].deep_clone) end |