Class: Genghis::Guardian

Inherits:
Object
  • Object
show all
Includes:
ProxyMethods
Defined in:
lib/genghis.rb

Direct Known Subclasses

ArrayProxy

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ProxyMethods

included

Constructor Details

#initialize(*args) ⇒ Guardian

Returns a new instance of Guardian.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/genghis.rb', line 281

def initialize(* args)

  opts = args.last.is_a?(Hash) ? args.last : {}
  if opts.empty?
    if args.empty?
      what = self.old_class.protected_class.new
    else
      what = args.first
    end
  else
    what = self.old_class.protected_class.new(opts)
  end

  @protected = what
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



328
329
330
331
332
333
# File 'lib/genghis.rb', line 328

def method_missing(method, * args, & block)
  return true if method == :safe?
  self.old_class.protect_from_exception do
    Guardian.make_safe(@protected.__send__(method, * args, & block))
  end
end

Class Method Details

.add(clazz) ⇒ Object



301
302
303
# File 'lib/genghis.rb', line 301

def self.add(clazz)
  protected_classes << clazz
end

.add_protected_class(subclass, protected_class) ⇒ Object



272
273
274
275
# File 'lib/genghis.rb', line 272

def self.add_protected_class(subclass, protected_class)
  @@protected_mappings                  ||= {}
  @@protected_mappings[protected_class] = subclass
end

.classes_under_protectionObject



309
310
311
# File 'lib/genghis.rb', line 309

def self.classes_under_protection
  protected_classes
end

.make_safe(o) ⇒ Object



313
314
315
316
317
318
319
320
321
# File 'lib/genghis.rb', line 313

def self.make_safe(o)

  if o.is_a? Array
    Guardian.protecting?(o.first.class) ? ArrayProxy.new(o) : o
  else
    class_providing_protection = protected_mappings[o.class] || Guardian
    Guardian.protecting?(o.class) ? class_providing_protection.new(o) : o
  end
end

.protected_classesObject



297
298
299
# File 'lib/genghis.rb', line 297

def self.protected_classes
  @@protected_classes ||= Set.new
end

.protected_mappingsObject



277
278
279
# File 'lib/genghis.rb', line 277

def self.protected_mappings
  @@protected_mappings
end

.protecting?(clazz) ⇒ Boolean Also known as: under_protection?

Returns:

  • (Boolean)


305
306
307
# File 'lib/genghis.rb', line 305

def self.protecting?(clazz)
  protected_classes.include?(clazz)
end

Instance Method Details

#unprotected_objectObject



323
324
325
# File 'lib/genghis.rb', line 323

def unprotected_object
  @protected
end