Module: Collapsium::ViralCapabilities
- Extended by:
- Support::Methods
- Includes:
- Support::Methods
- Included in:
- RecursiveDup, RecursiveFetch, RecursiveMerge, RecursiveSort, UberHash
- Defined in:
- lib/collapsium/viral_capabilities.rb
Overview
Tries to make extended Hash capabilities viral, i.e. provides the same features to nested Hash structures as the Hash that includes this module.
Virality is ensured by changing the return value of various methods; if it is derived from Hash, it is attempted to convert it to the including class.
The module uses HashMethods and ArrayMethods to decide which methods to make viral in this manner.
There are two ways for using this module: a) in a ‘Class`, either include, prepend or extend it. b) in a `Module`, extend this module. The resulting module can be included,
prepended or extended in a `Class` again.
Defined Under Namespace
Modules: ViralAncestorTypes
Constant Summary collapse
- DEFAULT_ANCESTORS =
The default ancestor values for the accessors in ViralAncestorTypes are defined here. They’re also used for generating functions that should provide the best ancestor class.
{ hash_ancestor: Hash, array_ancestor: Array, }.freeze
- ENHANCED_MARKER =
"@__collapsium_viral_capabilities_marker".freeze
- READ_METHODS =
We want to wrap methods for Arrays and Hashes alike
( ::Collapsium::Support::HashMethods::READ_METHODS \ + ::Collapsium::Support::ArrayMethods::READ_METHODS ).uniq.freeze
- WRITE_METHODS =
( ::Collapsium::Support::HashMethods::WRITE_METHODS \ + ::Collapsium::Support::ArrayMethods::WRITE_METHODS ).uniq.freeze
Constants included from Support::Methods
Support::Methods::BUILTINS, Support::Methods::WRAPPER_HASH
Class Method Summary collapse
- .call_virality(parent, value, *args) ⇒ Object
- .copy_mods(parent, value) ⇒ Object
-
.enhance(base) ⇒ Object
Enhance the base by wrapping all READ_METHODS and WRITE_METHODS in a wrapper that uses enhance_value to, well, enhance Hash and Array results.
- .enhance_array_value(parent, value, *args) ⇒ Object
-
.enhance_hash_value(parent, value, *args) ⇒ Object
Given an outer Hash and a value, enhance Hash values so that they have the same capabilities as the outer Hash.
-
.enhance_value(parent, value, *args) ⇒ Object
Enhance Hash or Array value.
- .extended(base) ⇒ Object
- .included(base) ⇒ Object
-
.prepended(base) ⇒ Object
When prepended, included or extended, enhance the base.
- .set_ancestors(parent, value) ⇒ Object
Instance Method Summary collapse
- #extended(base) ⇒ Object
- #included(base) ⇒ Object
-
#prepended(base) ⇒ Object
When prepended, included or extended, enhance the base.
Methods included from Support::Methods
builtins, loop_detected?, repeated, resolve_helpers, wrap_method, wrappers
Class Method Details
.call_virality(parent, value, *args) ⇒ Object
251 252 253 254 255 256 257 258 |
# File 'lib/collapsium/viral_capabilities.rb', line 251 def (parent, value, *args) # The parent class can define its own virality function. if parent.respond_to?(:virality) value = parent.(value, *args) end return value end |
.copy_mods(parent, value) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/collapsium/viral_capabilities.rb', line 229 def copy_mods(parent, value) # We want to extend all the modules in self. That might be a # no-op due to the above block, but not necessarily so. value_mods = (class << value; self end).included_modules parent_mods = (class << parent; self end).included_modules parent_mods << ViralAncestorTypes mods_to_copy = (parent_mods - value_mods).uniq # Small fixup for JSON; this doesn't technically belong here, but let's # play nice. if value.is_a? Array mods_to_copy.delete(::JSON::Ext::Generator::GeneratorMethods::Hash) elsif value.is_a? Hash mods_to_copy.delete(::JSON::Ext::Generator::GeneratorMethods::Array) end # Copy mods. mods_to_copy.each do |mod| value.extend(mod) end end |
.enhance(base) ⇒ Object
Enhance the base by wrapping all READ_METHODS and WRITE_METHODS in a wrapper that uses enhance_value to, well, enhance Hash and Array results.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/collapsium/viral_capabilities.rb', line 102 def enhance(base) # rubocop:disable Style/ClassVars @@write_block ||= proc do |wrapped_method, *args, &block| arg_copy = args.map do |arg| enhance_value(wrapped_method.receiver, arg) end result = wrapped_method.call(*arg_copy, &block) next enhance_value(wrapped_method.receiver, result) end @@read_block ||= proc do |wrapped_method, *args, &block| result = wrapped_method.call(*args, &block) next enhance_value(wrapped_method.receiver, result) end # rubocop:enable Style/ClassVars # Minimally: add the ancestor functions to classes if base.is_a? Class base.extend(ViralAncestorTypes) end READ_METHODS.each do |method_name| wrap_method(base, method_name, raise_on_missing: false, &@@read_block) end WRITE_METHODS.each do |method_name| wrap_method(base, method_name, raise_on_missing: false, &@@write_block) end end |
.enhance_array_value(parent, value, *args) ⇒ Object
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 |
# File 'lib/collapsium/viral_capabilities.rb', line 156 def enhance_array_value(parent, value, *args) # If the value is not of the best ancestor type, make sure it becomes # that type. # XXX: DO NOT replace the loop with a simpler function - it could lead # to infinite recursion! enc_class = array_ancestor(parent, value) if value.class != enc_class new_value = enc_class.new value.each do |item| if not item.is_a? Hash and not item.is_a? Array new_value << item next end new_item = enhance_value(value, item) new_value << new_item end value = new_value end # Copy all modules from the parent to the value copy_mods(parent, value) # Set appropriate ancestors on the value set_ancestors(parent, value) return (parent, value, *args) end |
.enhance_hash_value(parent, value, *args) ⇒ Object
Given an outer Hash and a value, enhance Hash values so that they have the same capabilities as the outer Hash. Non-Hash values are returned unchanged.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/collapsium/viral_capabilities.rb', line 189 def enhance_hash_value(parent, value, *args) # If the value is not of the best ancestor type, make sure it becomes # that type. # XXX: DO NOT replace the loop with :merge! or :merge - those are # potentially wrapped write functions, leading to an infinite # recursion. enc_class = hash_ancestor(parent, value) if value.class != enc_class new_value = enc_class.new value.each do |key, item| if not item.is_a? Hash and not item.is_a? Array new_value[key] = item next end new_item = enhance_value(value, item) new_value[key] = new_item end value = new_value end # Copy all modules from the parent to the value copy_mods(parent, value) # If we have a default_proc and the value doesn't, we want to use our # own. This *can* override a perfectly fine default_proc with our own, # which might suck. if parent.respond_to?(:default_proc) # FIXME: need to inherit this for arrays, too? value.default_proc ||= parent.default_proc end # Set appropriate ancestors on the value set_ancestors(parent, value) return (parent, value, *args) end |
.enhance_value(parent, value, *args) ⇒ Object
Enhance Hash or Array value
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/collapsium/viral_capabilities.rb', line 133 def enhance_value(parent, value, *args) if value.is_a? Hash value = enhance_hash_value(parent, value, *args) elsif value.is_a? Array value = enhance_array_value(parent, value, *args) end # It's possible that the value is a Hash or an Array, but there's no # ancestor from which capabilities can be copied. We can find out by # checking whether any wrappers are defined for it. # Turns out that that's quite expensive at run-time, though, so instead # we resort to flagging enhanced values. if value.is_a? Array or value.is_a? Hash needs_wrapping = !value.instance_variable_get(ENHANCED_MARKER) if needs_wrapping enhance(value) value.instance_variable_set(ENHANCED_MARKER, true) end end return value end |
.extended(base) ⇒ Object
84 85 86 |
# File 'lib/collapsium/viral_capabilities.rb', line 84 def extended(base) enhance(base) end |
.included(base) ⇒ Object
80 81 82 |
# File 'lib/collapsium/viral_capabilities.rb', line 80 def included(base) enhance(base) end |
.prepended(base) ⇒ Object
When prepended, included or extended, enhance the base.
76 77 78 |
# File 'lib/collapsium/viral_capabilities.rb', line 76 def prepended(base) enhance(base) end |
.set_ancestors(parent, value) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/collapsium/viral_capabilities.rb', line 295 def set_ancestors(parent, value) DEFAULT_ANCESTORS.each do |getter, default| setter = "#{getter}=".to_sym ancestor = nil if parent.is_a? default ancestor = parent.class elsif parent.respond_to?(getter) ancestor = parent.send(getter) else ancestor = default end value.send(setter, ancestor) end end |
Instance Method Details
#extended(base) ⇒ Object
67 68 69 |
# File 'lib/collapsium/viral_capabilities.rb', line 67 def extended(base) ViralCapabilities.enhance(base) end |
#included(base) ⇒ Object
63 64 65 |
# File 'lib/collapsium/viral_capabilities.rb', line 63 def included(base) ViralCapabilities.enhance(base) end |
#prepended(base) ⇒ Object
When prepended, included or extended, enhance the base.
59 60 61 |
# File 'lib/collapsium/viral_capabilities.rb', line 59 def prepended(base) ViralCapabilities.enhance(base) end |