Class: Safemode::Jail

Inherits:
Blankslate show all
Defined in:
lib/safemode/jail.rb

Direct Known Subclasses

Haml::Buffer::Jail

Instance Method Summary collapse

Methods inherited from Blankslate

allow, allowed?, allowed_methods, inherited, init_allowed_methods, method_added

Constructor Details

#initialize(source = nil) ⇒ Jail

Returns a new instance of Jail.



3
4
5
# File 'lib/safemode/jail.rb', line 3

def initialize(source = nil)
  @source = source
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/safemode/jail.rb', line 15

def method_missing(method, *args, &block)
  unless self.class.allowed?(method)
    raise Safemode::NoMethodError.new(method, self.class.name, @source.class.name) 
  end
  
  # As every call to an object in the eval'ed string will be jailed by the
  # parser we don't need to "proactively" jail arrays and hashes. Likewise we
  # don't need to jail objects returned from a jail. Doing so would provide
  # "double" protection, but it also would break using a return value in an if
  # statement, passing them to a Rails helper etc.
  @source.send(method, *args, &block)
end

Instance Method Details

#to_jailObject



7
8
9
# File 'lib/safemode/jail.rb', line 7

def to_jail
  self
end

#to_sObject



11
12
13
# File 'lib/safemode/jail.rb', line 11

def to_s
  @source.to_s
end