Module: Inform::Genealogical
- Included in:
- Object, System::Object
- Defined in:
- lib/runtime/tree.rb
Overview
The Genealogical module
Instance Method Summary collapse
- #>(other) ⇒ Object
- #all?(*args, &block) ⇒ Boolean
- #any?(*args, &block) ⇒ Boolean
- #branch ⇒ Object
-
#child(o = self, prop = :@children) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity.
-
#children(o = nil, prop = :@children) ⇒ Object
rubocop: disable Metrics/AbcSize.
- #collect(*args, &block) ⇒ Object
- #concat(*args, &block) ⇒ Object
- #count(*args, &block) ⇒ Object
- #delete_if(*args, &block) ⇒ Object
- #each(*args, &block) ⇒ Object
- #each_with_object(*args, &block) ⇒ Object
- #find(*args, &block) ⇒ Object
- #find_all(*args, &block) ⇒ Object
- #having(method) ⇒ Object
- #in?(*others) ⇒ Boolean
-
#include?(other) ⇒ Boolean
TODO: Refactor all of these to use Ruby Sequel dataset methods.
- #map(*args, &block) ⇒ Object
- #notin?(*others) ⇒ Boolean
- #reject(*args, &block) ⇒ Object
- #select(*args, &block) ⇒ Object
-
#sibling(o = self) ⇒ Object
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/MethodLength rubocop: enable Metrics/PerceivedComplexity.
- #siblings(orphanage = false) ⇒ Object
- #within?(*others) ⇒ Boolean
Instance Method Details
#>(other) ⇒ Object
27 28 29 |
# File 'lib/runtime/tree.rb', line 27 def >(other) self << other end |
#all?(*args, &block) ⇒ Boolean
71 72 73 |
# File 'lib/runtime/tree.rb', line 71 def all?(*args, &block) self.children.all?(*args, &block) end |
#any?(*args, &block) ⇒ Boolean
75 76 77 |
# File 'lib/runtime/tree.rb', line 75 def any?(*args, &block) self.children.any?(*args, &block) end |
#branch ⇒ Object
67 68 69 |
# File 'lib/runtime/tree.rb', line 67 def branch [self] + self.descendants end |
#child(o = self, prop = :@children) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity
147 148 149 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 |
# File 'lib/runtime/tree.rb', line 147 def child(o = self, prop = :@children) if o.instance_variable_defined?(prop) # The idea here is that o may be an Inform::System::Object. children = o.instance_variable_get(prop) return nil unless children.respond_to?(:first) children.first elsif o.respond_to?(:children_dataset) # It may be necessary to find some way of caching # the parent-children relationship. Therefore, # the following benchmarking is a way to gather # data so that it might be determined whether # enabling caching makes any difference in # execution speed. begin # TODO: Remove: if defined?(DEBUG) && @parser_trace >= 12 log.debug "#{indent}#{self}.children_dataset execution -- BEGIN" end # TODO: Remove: start = Time.now return o.children_dataset.first ensure # TODO: Remove # TODO: Remove elapsed = Time.now - start if defined? DEBUG # TODO: Remove: if defined?(DEBUG) && @parser_trace >= 12 log.debug "#{indent}#{self}.children_dataset executed in #{elapsed.round(6)} milliseconds" end end else log.warn "Object #{o} <#{o.identity}> (#{o.class}) has no children accessor" nil end end |
#children(o = nil, prop = :@children) ⇒ Object
rubocop: disable Metrics/AbcSize
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/runtime/tree.rb', line 128 def children(o = nil, prop = :@children) # TODO: Remove unless o.nil? log.warn "In genealogical.children(#{o})" log.warn " #{o}.respond_to? :children_dataset #=> #{o.respond_to? :children_dataset}" log.warn " #{o}.instance_variable_defined?(#{prop}) #=> #{o.respond_to? :children_dataset}" end self.safe_refresh if self.respond_to?(:safe_refresh) o.safe_refresh if o.respond_to?(:safe_refresh) return children.length if o.respond_to? :children_dataset return o.instance_variable_get(prop).length if o.instance_variable_defined?(prop) super() end |
#collect(*args, &block) ⇒ Object
91 92 93 |
# File 'lib/runtime/tree.rb', line 91 def collect(*args, &block) self.children.collect(*args, &block) end |
#concat(*args, &block) ⇒ Object
119 120 121 |
# File 'lib/runtime/tree.rb', line 119 def concat(*args, &block) self.children.concat(*args, &block) end |
#count(*args, &block) ⇒ Object
103 104 105 |
# File 'lib/runtime/tree.rb', line 103 def count(*args, &block) self.children.count(*args, &block) end |
#delete_if(*args, &block) ⇒ Object
99 100 101 |
# File 'lib/runtime/tree.rb', line 99 def delete_if(*args, &block) self.children.delete_if(*args, &block) end |
#each(*args, &block) ⇒ Object
79 80 81 |
# File 'lib/runtime/tree.rb', line 79 def each(*args, &block) self.children.each(*args, &block) end |
#each_with_object(*args, &block) ⇒ Object
83 84 85 |
# File 'lib/runtime/tree.rb', line 83 def each_with_object(*args, &block) self.children.each_with_object(*args, &block) end |
#find(*args, &block) ⇒ Object
111 112 113 |
# File 'lib/runtime/tree.rb', line 111 def find(*args, &block) self.children.find(*args, &block) end |
#find_all(*args, &block) ⇒ Object
115 116 117 |
# File 'lib/runtime/tree.rb', line 115 def find_all(*args, &block) self.children.find_all(*args, &block) end |
#having(method) ⇒ Object
123 124 125 |
# File 'lib/runtime/tree.rb', line 123 def having(method) self.children.having(method) end |
#in?(*others) ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/runtime/tree.rb', line 39 def in?(*others) return false unless self.respond_to?(:parent) self.refresh if self.respond_to?(:refresh) x = self.parent x.refresh if x.respond_to?(:refresh) return others.include?(x) if others.length > 1 y = others.first return true if self.parent == y return false unless y.respond_to?(:include?) y.include?(self) end |
#include?(other) ⇒ Boolean
TODO: Refactor all of these to use Ruby Sequel dataset methods.
33 34 35 36 37 |
# File 'lib/runtime/tree.rb', line 33 def include?(other) other.refresh if other.respond_to?(:refresh) return true if other.parent == self self.children.include?(other) end |
#map(*args, &block) ⇒ Object
107 108 109 |
# File 'lib/runtime/tree.rb', line 107 def map(*args, &block) self.children.map(*args, &block) end |
#notin?(*others) ⇒ Boolean
63 64 65 |
# File 'lib/runtime/tree.rb', line 63 def notin?(*others) !self.in?(*others) end |
#reject(*args, &block) ⇒ Object
95 96 97 |
# File 'lib/runtime/tree.rb', line 95 def reject(*args, &block) self.children.reject(*args, &block) end |
#select(*args, &block) ⇒ Object
87 88 89 |
# File 'lib/runtime/tree.rb', line 87 def select(*args, &block) self.children.select(*args, &block).to_a end |
#sibling(o = self) ⇒ Object
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/CyclomaticComplexity rubocop: enable Metrics/MethodLength rubocop: enable Metrics/PerceivedComplexity
186 187 188 189 190 191 192 193 194 |
# File 'lib/runtime/tree.rb', line 186 def sibling(o = self) o.refresh if o.respond_to? :refresh return nil if o.parent.nil? parent = o.parent return nil unless parent.respond_to?(:children) children = o.parent.children return nil if children.nil? || children.empty? children[children.index(o) + 1] end |
#siblings(orphanage = false) ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/runtime/tree.rb', line 196 def siblings(orphanage = false) if self.parent.nil? return orphanage ? Inform::Object.roots : [] end self.refresh if self.respond_to? :refresh self.parent.children - [self] end |
#within?(*others) ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/runtime/tree.rb', line 51 def within?(*others) x = self x.refresh if x.respond_to?(:refresh) loop do x = x.parent break if x.nil? return true if others.include?(x) x.refresh if x.respond_to?(:refresh) end false end |