Module: Blockify

Defined in:
lib/blockify.rb,
lib/blockify/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

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



131
132
133
# File 'lib/blockify.rb', line 131

def blockify_elements!(&block)
  replace blockify_elements &block
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 if done.first
        path.pop
      else
        if block.call(val)
          path.push key
          done[0]=true
          return path
        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 if done.first
        path.pop
      else
        if block.call(val)
          path.push idx
          done[0]=true
          return path
        end
      end
      idx += 1
    end
  end # if-else
  return path
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

#includify?(search_string) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
# File 'lib/blockify.rb', line 126

def includify?(search_string)
  path = find_element_path { |elm| elm.to_s.include? search_string }
  !path.empty?
end

#inspectify_elementsObject



140
141
142
# File 'lib/blockify.rb', line 140

def inspectify_elements
  blockify_elements {|t| t.inspect}
end

#inspectify_elements!Object



143
144
145
# File 'lib/blockify.rb', line 143

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

#stringify_elementsObject



134
135
136
# File 'lib/blockify.rb', line 134

def stringify_elements
  blockify_elements {|t| t.to_s}
end

#stringify_elements!Object



137
138
139
# File 'lib/blockify.rb', line 137

def stringify_elements!
  replace stringify_elements
end