module Blockify

Constants

VERSION

Public Instance Methods

blockify_elements(&block) click to toggle source
# 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) click to toggle source
# File lib/blockify.rb, line 241
def blockify_elements!(&block)
  replace blockify_elements &block
end
circular?(path = [], stack = {}, rtn = [false]) click to toggle source

add path later to see where things break down

# 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() click to toggle source
# File lib/blockify.rb, line 257
def deep_duplify
  blockify_elements do |t|
    t.dup rescue t
  end
end
extractifirstify(cls) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/blockify.rb, line 199
def flattenify
  rtn = []
  scan_elements { |key, inst| rtn.push inst[key] }
  return rtn
end
includify?(search_string) click to toggle source
# 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() click to toggle source
# File lib/blockify.rb, line 250
def inspectify_elements
  blockify_elements {|t| t.inspect}
end
inspectify_elements!() click to toggle source
# File lib/blockify.rb, line 253
def inspectify_elements!
  replace inspectify_elements
end
path_get(path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

read elements and yield everything

# 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() click to toggle source
# File lib/blockify.rb, line 263
def self_or_empty_as_nil
  return nil if empty?
  self
end
stringify_elements() click to toggle source
# File lib/blockify.rb, line 244
def stringify_elements
  blockify_elements {|t| t.to_s}
end
stringify_elements!() click to toggle source
# File lib/blockify.rb, line 247
def stringify_elements!
  replace stringify_elements
end
unloopify() click to toggle source
# 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