Class: Eggshell::Bundles::Basic::ControlLoopMacros
- Inherits:
-
Object
- Object
- Eggshell::Bundles::Basic::ControlLoopMacros
- Includes:
- MacroHandler
- Defined in:
- lib/eggshell/bundles/basics.rb
Overview
Provides iteration and conditional functionality.
Defined Under Namespace
Classes: WhileLoopWrapper
Constant Summary
Constants included from MacroHandler
MacroHandler::COLLECT_NORMAL, MacroHandler::COLLECT_RAW, MacroHandler::COLLECT_RAW_MACRO
Instance Method Summary collapse
- #process(name, args, lines, out, call_depth = 0) ⇒ Object
-
#set_processor(proc, opts = nil) ⇒ Object
pre.
Methods included from MacroHandler
Instance Method Details
#process(name, args, lines, out, call_depth = 0) ⇒ Object
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 |
# File 'lib/eggshell/bundles/basics.rb', line 741 def process(name, args, lines, out, call_depth = 0) macname = name.to_sym st = @state[call_depth] if !@state[call_depth] st = {:type => macname} @state[call_depth] = st # erase nested state @state[call_depth+1] = nil end p0 = args[0] if macname == :for || macname == :loop || macname == :while p1 = args[1] || 'raw' looper = nil loop_is_map = false if macname == :while if p0 looper = WhileLoopWrapper.new(@eggshell, p0) end else p0 ||= 'true' st[:iter] = p0['items'] || nil st[:item] = p0['item'] || 'item' st[:var] = p0['var'] st[:start] = p0['start'] st[:stop] = p0['stop'] st[:step] = p0['step'] || 1 st[:counter] = p0['counter'] || 'counter' if st[:iter].is_a?(Array) st[:start] = 0 if !st[:start] st[:stop] = st[:iter].length - 1 if !st[:stop] st[:step] = 1 if !st[:step] looper = Range.new(st[:start], st[:stop]).step(st[:step]).to_a elsif st[:iter].respond_to?(:each) looper = st[:iter] loop_is_map = true end end collector = out raw = p1 == 'raw' if looper counter = -1 looper.each do |i1, i2| counter += 1 break if @eggshell.vars[:loop_max_limit] == counter # for maps, use key as the counter val = nil if loop_is_map @eggshell.vars[st[:counter]] = i1 val = i2 else val = i1 @eggshell.vars[st[:counter]] = counter end # inject value into :item -- if it's an expression, evaluate first @eggshell.vars[st[:item]] = val.is_a?(Array) && val[0].is_a?(Symbol) ? @eggshell.expr_eval(val) : val # if doing raw, pass through block lines with variable expansion. preserve object type (e.g. Line or String); # sub-macros will get passed collector var as output to assemble(). # otherwise, call assemble() on all lines if raw lines.each do |unit| if unit.is_a?(Array) if unit[0] == :block unit[Eggshell::ParseTree::IDX_LINES].each do |line| nline = line if line.is_a?(String) nline = @eggshell.(line) else # rather than expand_expr on raw, assume line.line is a subset of line.raw _raw = line.raw _line = @eggshell.(line.line) _raw = _raw.gsub(line.line, _line) if _raw nline = line.replace(_line, _raw) end collector << nline end else @eggshell.assemble([unit], call_depth + 1, {:out => collector}) end else collector << @eggshell.(line.to_s) end end else collector << @eggshell.assemble(lines, call_depth + 1) end break if st[:break] end end # clear state @state[call_depth] = nil # elsif macname == :while # raw = args[1] # while @eggshell.expr_eval(p0) # process_lines(lines, buffer, depth + 1, raw) # break if st[:break] # end elsif macname == :if || macname == :elsif || macname == :else cond = p0 st[:if] = true if macname == :if if st[:cond_count] == nil || macname == :if st[:cond_count] = 0 st[:cond_eval] = false st[:cond_met] = false end last_action = st[:last_action] st[:last_action] = macname.to_sym # @todo more checks (e.g. no elsif after else, no multiple else, etc.) if !st[:if] || (macname != :else && !cond) # @todo exception? return end if macname != :else if !st[:cond_eval] cond_struct = Eggshell::ExpressionEvaluator.struct(cond) st[:cond_eval] = @eggshell.expr_eval(cond_struct) end else st[:cond_eval] = true end if st[:cond_eval] && !st[:cond_met] st[:cond_met] = true #process_lines(lines, buffer, depth + 1) @eggshell.assemble(lines, call_depth + 1, {:out => out}) end elsif macname == :break lvl = p0 || 1 i = call_depth - 1 # set breaks at each found loop until # of levels reached while i >= 0 st = @state[i] i -= 1 next if !st if st[:type] == :for || st[:type] == :while || st[:type] == :loop lvl -= 1 st[:break] = true break if lvl <= 0 end end elsif macname == :next lvl = p0 || 1 i = depth - 1 # set breaks at each found loop until # of levels reached while i >= 0 st = @state[i] i -= 1 next if !st if st[:type] == :for || st[:type] == :while || st[:type] == :loop lvl -= 1 st[:next] = true break if lvl <= 0 end end end end |
#set_processor(proc, opts = nil) ⇒ Object
pre. @if(“expression”) {} @elsif(“expression”) {} else {}
#! modes: #! ‘raw’ (default): collects all generated lines as raw (unless there are sub-macros, which would just collect the output as as-is) #! ‘eval’: evaluates each unit via Eggshell::Processor.assemble #! note that interpolated expressions are expanded in raw mode # @for(0, ‘stop’: var, ‘step’: 1, ‘items’: …, ‘item’: ‘varname’, ‘counter’: ‘varname’[, mode])
729 730 731 732 733 734 735 736 737 738 739 |
# File 'lib/eggshell/bundles/basics.rb', line 729 def set_processor(proc, opts = nil) opts = {} if !opts @opts = opts @eggshell = proc @eggshell.add_macro_handler(self, *%w(if elsif else loop for while break next)) @vars = @eggshell.vars @eggshell.vars[:loop_max_limit] = 1000 @state = [] # @todo set loop limits from opts or defaults end |