Class: RedGrape::Pipe::Context
Instance Attribute Summary collapse
-
#grouping_items ⇒ Object
readonly
Returns the value of attribute grouping_items.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#it ⇒ Object
Returns the value of attribute it.
-
#loops ⇒ Object
Returns the value of attribute loops.
-
#marks ⇒ Object
readonly
Returns the value of attribute marks.
Instance Method Summary collapse
- #accumulate(key, obj, &block) ⇒ Object
- #accumulating?(key = nil) ⇒ Boolean
- #aggregate(key, val, next_pipe) ⇒ Object
- #aggregated_items(key) ⇒ Object
- #aggregating? ⇒ Boolean
- #aggregating_items(key) ⇒ Object
- #clear_accumulating(key = nil) ⇒ Object
- #count(obj, next_pipe) ⇒ Object
- #counting? ⇒ Boolean
- #eval(args = {}, &block) ⇒ Object
- #gather(obj, next_pipe) ⇒ Object
- #gathering? ⇒ Boolean
- #group(next_pipe, key, val = key) ⇒ Object
- #grouping? ⇒ Boolean
-
#initialize ⇒ Context
constructor
A new instance of Context.
- #mark(label) ⇒ Object
- #mark!(label, val = nil) ⇒ Object
- #order(obj, next_pipe) ⇒ Object
- #ordering? ⇒ Boolean
- #push_history(obj, &block) ⇒ Object
- #resume_from_accumulating(key = nil, should_clear = true) ⇒ Object
- #resume_from_aggregating ⇒ Object
- #resume_from_counting ⇒ Object
- #resume_from_gathering ⇒ Object
- #resume_from_grouping ⇒ Object
- #resume_from_ordering ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/red_grape/pipe/context.rb', line 7 def initialize @history = [] @marks = {} @aggregating_items = {} @aggregated_items = {} @grouping_items = {} @accumulated_items = {} @loops = 1 end |
Instance Attribute Details
#grouping_items ⇒ Object (readonly)
Returns the value of attribute grouping_items.
5 6 7 |
# File 'lib/red_grape/pipe/context.rb', line 5 def grouping_items @grouping_items end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
5 6 7 |
# File 'lib/red_grape/pipe/context.rb', line 5 def history @history end |
#it ⇒ Object
Returns the value of attribute it.
4 5 6 |
# File 'lib/red_grape/pipe/context.rb', line 4 def it @it end |
#loops ⇒ Object
Returns the value of attribute loops.
4 5 6 |
# File 'lib/red_grape/pipe/context.rb', line 4 def loops @loops end |
#marks ⇒ Object (readonly)
Returns the value of attribute marks.
5 6 7 |
# File 'lib/red_grape/pipe/context.rb', line 5 def marks @marks end |
Instance Method Details
#accumulate(key, obj, &block) ⇒ Object
93 94 95 96 97 |
# File 'lib/red_grape/pipe/context.rb', line 93 def accumulate(key, obj, &block) items = (@accumulated_items[key] ||= {}) (items[:objects] ||= []) << obj items[:resume_block] = block if block end |
#accumulating?(key = nil) ⇒ Boolean
99 100 101 102 103 104 105 |
# File 'lib/red_grape/pipe/context.rb', line 99 def accumulating?(key=nil) if key not @accumulated_items[key].nil? else @accumulated_items.any?{|k, _| accumulating? k} end end |
#aggregate(key, val, next_pipe) ⇒ Object
43 44 45 46 |
# File 'lib/red_grape/pipe/context.rb', line 43 def aggregate(key, val, next_pipe) self.aggregating_items(key) << [val, next_pipe] val end |
#aggregated_items(key) ⇒ Object
35 36 37 |
# File 'lib/red_grape/pipe/context.rb', line 35 def aggregated_items(key) @aggregated_items[key.to_s.to_sym] ||= [] end |
#aggregating? ⇒ Boolean
48 49 50 |
# File 'lib/red_grape/pipe/context.rb', line 48 def aggregating? not @aggregating_items.empty? end |
#aggregating_items(key) ⇒ Object
39 40 41 |
# File 'lib/red_grape/pipe/context.rb', line 39 def aggregating_items(key) @aggregating_items[key.to_s.to_sym] ||= [] end |
#clear_accumulating(key = nil) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/red_grape/pipe/context.rb', line 107 def clear_accumulating(key=nil) if key @accumulated_items[key] = nil if @accumulated_items[key] else @accumulated_items.clear end end |
#count(obj, next_pipe) ⇒ Object
153 154 155 156 157 |
# File 'lib/red_grape/pipe/context.rb', line 153 def count(obj, next_pipe) accumulate :count, obj do |objs| objs.size end end |
#counting? ⇒ Boolean
159 160 161 |
# File 'lib/red_grape/pipe/context.rb', line 159 def counting? accumulating? :count end |
#eval(args = {}, &block) ⇒ Object
181 182 183 184 |
# File 'lib/red_grape/pipe/context.rb', line 181 def eval(args={}, &block) args.each {|k, v| self.send "#{k}=", v} instance_eval(&block) end |
#gather(obj, next_pipe) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/red_grape/pipe/context.rb', line 130 def gather(obj, next_pipe) accumulate :gather, obj do |objs| objs = objs.dup objs.should_pass_through_whole = true if next_pipe push_history objs do |ctx| next_pipe.prev = objs next_pipe.take ctx end else objs end end end |
#gathering? ⇒ Boolean
145 146 147 |
# File 'lib/red_grape/pipe/context.rb', line 145 def gathering? accumulating? :gather end |
#group(next_pipe, key, val = key) ⇒ Object
73 74 75 76 |
# File 'lib/red_grape/pipe/context.rb', line 73 def group(next_pipe, key, val=key) (@grouping_items[key] ||= []) << [val, next_pipe] val end |
#grouping? ⇒ Boolean
78 79 80 |
# File 'lib/red_grape/pipe/context.rb', line 78 def grouping? not @grouping_items.empty? end |
#mark(label) ⇒ Object
31 32 33 |
# File 'lib/red_grape/pipe/context.rb', line 31 def mark(label) @marks[label] end |
#mark!(label, val = nil) ⇒ Object
27 28 29 |
# File 'lib/red_grape/pipe/context.rb', line 27 def mark!(label, val=nil) @marks[label] = val || @history.last end |
#order(obj, next_pipe) ⇒ Object
167 168 169 170 171 |
# File 'lib/red_grape/pipe/context.rb', line 167 def order(obj, next_pipe) accumulate :order, obj do |objs| objs.sort end end |
#ordering? ⇒ Boolean
173 174 175 |
# File 'lib/red_grape/pipe/context.rb', line 173 def ordering? accumulating? :order end |
#push_history(obj, &block) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/red_grape/pipe/context.rb', line 20 def push_history(obj, &block) @history.push obj ret = block.call self @history.pop ret end |
#resume_from_accumulating(key = nil, should_clear = true) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/red_grape/pipe/context.rb', line 115 def resume_from_accumulating(key=nil, should_clear=true) ret = nil if key if items = @accumulated_items[key] ret = items[:resume_block].call(items[:objects]) if items[:resume_block] end @accumulated_items[key] = nil if should_clear else @accumulated_items.each do |k, _| ret = resume_from_accumulating k end end ret end |
#resume_from_aggregating ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/red_grape/pipe/context.rb', line 52 def resume_from_aggregating ret = [] aggregating = @aggregating_items @aggregating_items = {} aggregating.each do |key, obj_and_pipes| obj_and_pipes.each do |obj_and_pipe| (@aggregated_items[key] ||= []) << obj_and_pipe.first end end aggregating.each do |key, obj_and_pipes| obj_and_pipes.each do |obj_and_pipe| obj, pipe = *obj_and_pipe push_history obj do |ctx| ret << obj.pass_through(pipe, ctx) end end end ret.normalize_for_graph end |
#resume_from_counting ⇒ Object
163 164 165 |
# File 'lib/red_grape/pipe/context.rb', line 163 def resume_from_counting resume_from_accumulating :count end |
#resume_from_gathering ⇒ Object
149 150 151 |
# File 'lib/red_grape/pipe/context.rb', line 149 def resume_from_gathering resume_from_accumulating :gather end |
#resume_from_grouping ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/red_grape/pipe/context.rb', line 82 def resume_from_grouping obj, pipe = *@grouping_items.values.first.first # TODO: '.first' is used to get next_pipe. if pipe push_history obj do |ctx| obj.pass_through pipe, ctx end else obj end end |
#resume_from_ordering ⇒ Object
177 178 179 |
# File 'lib/red_grape/pipe/context.rb', line 177 def resume_from_ordering resume_from_accumulating :order end |