Class: RBlade::CompilesStatements::CompilesLoops
- Inherits:
-
Object
- Object
- RBlade::CompilesStatements::CompilesLoops
- Defined in:
- lib/rblade/compiler/statements/compiles_loops.rb
Instance Method Summary collapse
- #compile_break(args) ⇒ Object
- #compile_each(args) ⇒ Object
- #compile_each_else(args) ⇒ Object
- #compile_each_with_index(args) ⇒ Object
- #compile_each_with_index_else(args) ⇒ Object
- #compile_empty(args) ⇒ Object
- #compile_for(args) ⇒ Object
- #compile_for_else(args) ⇒ Object
- #compile_next(args) ⇒ Object
- #compile_until(args) ⇒ Object
- #compile_while(args) ⇒ Object
-
#initialize ⇒ CompilesLoops
constructor
A new instance of CompilesLoops.
Constructor Details
#initialize ⇒ CompilesLoops
Returns a new instance of CompilesLoops.
6 7 8 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 6 def initialize @loop_else_counter = 0 end |
Instance Method Details
#compile_break(args) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 10 def compile_break(args) if args&.count&.> 1 raise RBladeTemplateError.new "Break statement: wrong number of arguments (given #{args.count}, expecting 0 or 1)" end if args.nil? "break;" else "if #{args[0]};break;end;" end end |
#compile_each(args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 22 def compile_each(args) if args.nil? || args.count > 2 raise RBladeTemplateError.new "Each statement: wrong number of arguments (given #{args&.count || 0}, expecting 1 or 2)" end last_arg, collection = args.pop.split(" in ") args.push(last_arg) if collection.nil? raise RBladeTemplateError.new "Each statement: collection not found (expecting 'in')" end "#{collection}.each do |#{args.join(",")}|;" end |
#compile_each_else(args) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 56 def compile_each_else(args) if args.nil? || args.count > 2 raise RBladeTemplateError.new "Each else statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)" end last_arg, collection = args.pop.split(" in ") args.push(last_arg) if collection.nil? raise RBladeTemplateError.new "Each else statement: collection not found (expecting 'in')" end @loop_else_counter += 1 "_looped_#{@loop_else_counter}=false;#{collection}.each do |#{args.join(",")}|;_looped_#{@loop_else_counter}=true;" end |
#compile_each_with_index(args) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 36 def compile_each_with_index(args) if args.nil? || args.count > 3 raise RBladeTemplateError.new "Each with index statement: wrong number of arguments (given #{args&.count || 0}, expecting 1 to 3)" end last_arg, collection = args.pop.split(" in ") args.push(last_arg) if collection.nil? raise RBladeTemplateError.new "Each with index statement: collection not found (expecting 'in')" end # Special case for 3 arguments: the first 2 arguments are the key/value pair in a Hash, and # the third is the index if args.count == 3 "#{collection}.each_with_index do |_ivar,#{args[2]}|;#{args[0]},#{args[1]}=_ivar;" else "#{collection}.each_with_index do |#{args.join(",")}|;" end end |
#compile_each_with_index_else(args) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 72 def compile_each_with_index_else(args) if args.nil? || args.count > 3 raise RBladeTemplateError.new "Each with index statement: wrong number of arguments (given #{args&.count || 0}, expecting 1 to 3)" end last_arg, collection = args.pop.split(" in ") args.push(last_arg) if collection.nil? raise RBladeTemplateError.new "Each with index statement: collection not found (expecting 'in')" end @loop_else_counter += 1 # Special case for 3 arguments: the first 2 arguments are the key/value pair in a Hash, and # the third is the index if args.count == 3 "_looped_#{@loop_else_counter}=false;#{collection}.each_with_index do |_ivar,#{args[2]}|;#{args[0]},#{args[1]}=_ivar;_looped_#{@loop_else_counter}=true;" else "_looped_#{@loop_else_counter}=false;#{collection}.each_with_index do |#{args.join(",")}|;_looped_#{@loop_else_counter}=true;" end end |
#compile_empty(args) ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 111 def compile_empty(args) unless args.nil? raise RBladeTemplateError.new "Empty statement: wrong number of arguments (given #{args.count}, expecting 0)" end @loop_else_counter -= 1 "end;if !_looped_#{@loop_else_counter + 1};" end |
#compile_for(args) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 94 def compile_for(args) if args&.count != 1 raise RBladeTemplateError.new "For statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)" end "for #{args[0]};" end |
#compile_for_else(args) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 102 def compile_for_else(args) if args&.count != 1 raise RBladeTemplateError.new "For else statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)" end @loop_else_counter += 1 "_looped_#{@loop_else_counter}=false;for #{args[0]};_looped_#{@loop_else_counter}=true;" end |
#compile_next(args) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 121 def compile_next(args) if args&.count&.> 1 raise RBladeTemplateError.new "Next statement: wrong number of arguments (given #{args.count}, expecting 0 or 1)" end if args.nil? "next;" else "if #{args[0]};next;end;" end end |
#compile_until(args) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 133 def compile_until(args) if args&.count != 1 raise RBladeTemplateError.new "Until statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)" end "until #{args[0]};" end |
#compile_while(args) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 141 def compile_while(args) if args&.count != 1 raise RBladeTemplateError.new "While statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)" end "while #{args[0]};" end |