Module: Blockify
- Defined in:
- lib/blockify.rb,
lib/blockify/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Method Summary collapse
-
#blockify_elements(&block) ⇒ Object
blockify.
- #blockify_elements!(&block) ⇒ Object
-
#circular?(path = [], stack = {}, rtn = [false]) ⇒ Boolean
add path later to see where things break down.
- #deep_duplify ⇒ Object
- #extractifirstify(cls) ⇒ Object
- #extractify(cls) ⇒ Object
- #extractistacktify(cls) ⇒ Object
- #find_element_path(path = [], done = [false], &block) ⇒ Object
- #find_element_paths(path = [], paths = [], &block) ⇒ Object
- #flattenify ⇒ Object
- #includify?(search_string) ⇒ Boolean
- #inspectify_elements ⇒ Object
- #inspectify_elements! ⇒ Object
- #path_get(path) ⇒ Object
- #path_put(val, path) ⇒ Object
- #paths_get(paths) ⇒ Object
-
#scan_elements(&block) ⇒ Object
read elements and yield everything.
- #self_or_empty_as_nil ⇒ Object
- #stringify_elements ⇒ Object
- #stringify_elements! ⇒ Object
- #unloopify ⇒ Object
Instance Method Details
#blockify_elements(&block) ⇒ Object
blockify
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/blockify.rb', line 4 def blockify_elements(&block) # blockify if self.respond_to? :each_pair hash = {} self.each_pair do |key,val| if val.respond_to? :each hash[key] = val.blockify_elements &block else hash[key] = block.call(val) end end return hash elsif self.respond_to? :each ary = [] self.each do |val| if val.respond_to? :each ary.push val.blockify_elements &block else ary.push block.call(val) end end return ary end return self # should never get here end |
#blockify_elements!(&block) ⇒ Object
241 242 243 |
# File 'lib/blockify.rb', line 241 def blockify_elements!(&block) replace blockify_elements &block end |
#circular?(path = [], stack = {}, rtn = [false]) ⇒ Boolean
add path later to see where things break down
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/blockify.rb', line 150 def circular? (path = [], stack = {}, rtn = [false]) my_id = self.object_id stack.incrementify(my_id) if self.respond_to? :each_pair self.each_pair do |key,val| if val.respond_to? :each x_id = val.object_id if stack[x_id] rtn[0]=true return rtn.first else path.push key return rtn.first if val.circular?(path, stack, rtn) path.pop end end end elsif self.respond_to? :each ii = 0 self.each do |val| if val.respond_to? :each x_id = val.object_id if stack[x_id] rtn[0]=true path.push ii return rtn.first else path.push ii return rtn.first if val.circular?(path, stack, rtn) path.pop end end ii += 1 end end stack.decrementify(my_id) rtn.first end |
#deep_duplify ⇒ Object
257 258 259 260 261 |
# File 'lib/blockify.rb', line 257 def deep_duplify blockify_elements do |t| t.dup rescue t end end |
#extractifirstify(cls) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/blockify.rb', line 213 def extractifirstify(cls) col = cls.new scan_elements do |key, inst| if (inst.kind_of? cls) if col[key].nil? col[key] = inst[key] end end end return col end |
#extractify(cls) ⇒ Object
205 206 207 208 209 210 211 |
# File 'lib/blockify.rb', line 205 def extractify(cls) col = cls.new scan_elements do |key, inst| col[key] = inst[key] if (inst.kind_of? cls) end return col end |
#extractistacktify(cls) ⇒ Object
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/blockify.rb', line 225 def extractistacktify(cls) col = cls.new scan_elements do |key, inst| if (inst.kind_of? cls) col[key] ||= [] col[key].push inst[key] end end return col end |
#find_element_path(path = [], done = [false], &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/blockify.rb', line 29 def find_element_path(path=[],done=[false], &block) if self.respond_to? :each_pair self.each_pair do |key,val| if val.respond_to? :each path.push key val.find_element_path(path, done, &block) return path.self_or_empty_as_nil if done.first path.pop else if block.call(val) path.push key done[0]=true return path.self_or_empty_as_nil end end end elsif self.respond_to? :each idx = 0 self.each do |val| if val.respond_to? :each path.push idx val.find_element_path(path, done, &block) return path.self_or_empty_as_nil if done.first path.pop else if block.call(val) path.push idx done[0]=true return path.self_or_empty_as_nil end end idx += 1 end end # if-else return path.self_or_empty_as_nil end |
#find_element_paths(path = [], paths = [], &block) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/blockify.rb', line 66 def find_element_paths(path=[],paths=[], &block) if self.respond_to? :each_pair self.each_pair do |key,val| if val.respond_to? :each path.push key val.find_element_paths(path, paths, &block) path.pop else if block.call(val) path.push key paths.push path.dup path.pop end end end elsif self.respond_to? :each idx = 0 self.each do |val| if val.respond_to? :each path.push idx val.find_element_paths(path, paths, &block) path.pop else if block.call(val) path.push idx paths.push path.dup path.pop end end idx += 1 end end # if-else return paths end |
#flattenify ⇒ Object
199 200 201 202 203 |
# File 'lib/blockify.rb', line 199 def flattenify rtn = [] scan_elements { |key, inst| rtn.push inst[key] } return rtn end |
#includify?(search_string) ⇒ Boolean
236 237 238 239 |
# File 'lib/blockify.rb', line 236 def includify?(search_string) path = find_element_path { |elm| elm.to_s.include? search_string } !path.nil? end |
#inspectify_elements ⇒ Object
250 251 252 |
# File 'lib/blockify.rb', line 250 def inspectify_elements blockify_elements {|t| t.inspect} end |
#inspectify_elements! ⇒ Object
253 254 255 |
# File 'lib/blockify.rb', line 253 def inspectify_elements! replace inspectify_elements end |
#path_get(path) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/blockify.rb', line 101 def path_get(path) item = self path.each do |idx| item = item[idx] end return item end |
#path_put(val, path) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/blockify.rb', line 117 def path_put(val,path) idx = path.pop con = path_get(path) rtn = con[idx] con[idx]=val path.push idx # put back return rtn end |
#paths_get(paths) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/blockify.rb', line 109 def paths_get(paths) rtn = [] paths.each do |path| rtn.push path_get(path) end return rtn end |
#scan_elements(&block) ⇒ Object
read elements and yield everything
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/blockify.rb', line 127 def scan_elements(&block) if self.respond_to? :each_pair self.each_pair do |key,val| if val.respond_to? :each val.scan_elements(&block) else block.call(key,self) end end elsif self.respond_to? :each ii = 0 self.each do |val| if val.respond_to? :each val.scan_elements(&block) else block.call(ii,self) end ii += 1 end end end |
#self_or_empty_as_nil ⇒ Object
263 264 265 266 |
# File 'lib/blockify.rb', line 263 def self_or_empty_as_nil return nil if empty? self end |
#stringify_elements ⇒ Object
244 245 246 |
# File 'lib/blockify.rb', line 244 def stringify_elements blockify_elements {|t| t.to_s} end |
#stringify_elements! ⇒ Object
247 248 249 |
# File 'lib/blockify.rb', line 247 def stringify_elements! replace stringify_elements end |
#unloopify ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/blockify.rb', line 189 def unloopify path = [] while circular? path do obj = path_get path nobj = CircularObjectContainer.new obj path_put(nobj, path) path = [] end end |