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_class_method, allow_instance_method, allowed_class_method?, allowed_class_methods, allowed_instance_method?, allowed_instance_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
27
28
29
30
31
32
# File 'lib/safemode/jail.rb', line 15

def method_missing(method, *args, &block)
  if @source.is_a?(Class)
    unless self.class.allowed_class_method?(method)
      raise Safemode::NoMethodError.new(".#{method}", self.class.name, @source.name)
    end
  else
    unless self.class.allowed_instance_method?(method)
      raise Safemode::NoMethodError.new("##{method}", self.class.name, @source.class.name)
    end
  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

#respond_to?(method) ⇒ Boolean

needed for compatibility with 1.8.7; remove this method once 1.8.7 support has been dropped

Returns:

  • (Boolean)


35
36
37
# File 'lib/safemode/jail.rb', line 35

def respond_to?(method, *)
  respond_to_missing?(method)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/safemode/jail.rb', line 39

def respond_to_missing?(method_name, include_private = false)
  self.class.allowed_instance_method?(method_name)
end

#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