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
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::WRAPPER_HASH

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Methods

loop_detected?, repeated, resolve_helpers, wrap_method, wrappers

Class Method Details

.call_virality(parent, value, *args) ⇒ Object



257
258
259
260
261
262
263
264
# File 'lib/collapsium/viral_capabilities.rb', line 257

def call_virality(parent, value, *args)
  # The parent class can define its own virality function.
  if parent.respond_to?(:virality)
    value = parent.virality(value, *args)
  end

  return value
end

.copy_mods(parent, value) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/collapsium/viral_capabilities.rb', line 235

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.



100
101
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
# File 'lib/collapsium/viral_capabilities.rb', line 100

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



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
188
189
# File 'lib/collapsium/viral_capabilities.rb', line 162

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 call_virality(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.



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
228
229
230
231
232
233
# File 'lib/collapsium/viral_capabilities.rb', line 195

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 call_virality(parent, value, *args)
end

.enhance_value(parent, value, *args) ⇒ Object

Enhance Hash or Array value



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/collapsium/viral_capabilities.rb', line 131

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.
  needs_wrapping = true
  READ_METHODS.each do |method_name|
    wrappers = ::Collapsium::Support::Methods.wrappers(value, method_name)
    # rubocop:disable Style/Next
    if wrappers.include?(@@read_block)
      # all done
      needs_wrapping = false
      break
    end
    # rubocop:enable Style/Next
  end

  # If we have a Hash or Array value that needs enhancing still, let's
  # do that.
  if needs_wrapping and (value.is_a? Array or value.is_a? Hash)
    enhance(value)
  end

  return value
end

.extended(base) ⇒ Object



82
83
84
# File 'lib/collapsium/viral_capabilities.rb', line 82

def extended(base)
  enhance(base)
end

.included(base) ⇒ Object



78
79
80
# File 'lib/collapsium/viral_capabilities.rb', line 78

def included(base)
  enhance(base)
end

.prepended(base) ⇒ Object

When prepended, included or extended, enhance the base.



74
75
76
# File 'lib/collapsium/viral_capabilities.rb', line 74

def prepended(base)
  enhance(base)
end

.set_ancestors(parent, value) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/collapsium/viral_capabilities.rb', line 301

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



65
66
67
# File 'lib/collapsium/viral_capabilities.rb', line 65

def extended(base)
  ViralCapabilities.enhance(base)
end

#included(base) ⇒ Object



61
62
63
# File 'lib/collapsium/viral_capabilities.rb', line 61

def included(base)
  ViralCapabilities.enhance(base)
end

#prepended(base) ⇒ Object

When prepended, included or extended, enhance the base.



57
58
59
# File 'lib/collapsium/viral_capabilities.rb', line 57

def prepended(base)
  ViralCapabilities.enhance(base)
end