Class: ScopedAttrAccessible::Sanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/scoped_attr_accessible/sanitizer.rb

Instance Method Summary collapse

Constructor Details

#initializeSanitizer

Returns a new instance of Sanitizer.



6
7
8
9
10
11
12
13
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 6

def initialize
  @accessible_attributes = Hash.new { |h,k| h[k] = Set.new }
  @protected_attributes  = Hash.new { |h,k| h[k] = Set.new }
  # Scope recognizers return a boolean, with a hash key
  @scope_recognizers     = Hash.new { |h,k| h[k] = [] }
  # Returns a scope symbol.
  @scope_converters      = []
end

Instance Method Details

#allow?(attribute, scope = :default) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 60

def allow?(attribute, scope = :default)
  attribute_assignable_with_scope?(attribute, scope)
end

#attribute_assignable_with_scope?(attribute, scope) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 64

def attribute_assignable_with_scope?(attribute, scope)
  attribute = attribute.to_s.gsub(/\(.+/, '')
  scope     = scope.to_sym
  scope_protected  = @protected_attributes[scope]  + @protected_attributes[:all]
  scope_accessible = @accessible_attributes[scope] + @accessible_attributes[:all]
  if scope_protected.include? attribute
    return false
  elsif scope_accessible.include?('all') || scope_accessible.include?(attribute)
    return true
  elsif !scope_accessible.empty?
    return false
  else
    return true
  end
end

#define_converter(&blk) ⇒ Object



44
45
46
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 44

def define_converter(&blk)
  @scope_converters << blk
end

#define_recognizer(scope, &blk) ⇒ Object



40
41
42
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 40

def define_recognizer(scope, &blk)
  @scope_recognizers[scope.to_sym] << blk
end

#deny?(attribute, scope = :default) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 56

def deny?(attribute, scope = :default)
  !attribute_assignable_with_scope?(attribute, scope)
end

#make_accessible(attribute, scope = :default) ⇒ Object



52
53
54
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 52

def make_accessible(attribute, scope = :default)
  @accessible_attributes[scope.to_sym] << attribute.to_s
end

#make_protected(attribute, scope = :default) ⇒ Object



48
49
50
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 48

def make_protected(attribute, scope = :default)
  @protected_attributes[scope.to_sym] << attribute.to_s
end

#normalize_scope(object, context) ⇒ Object

Looks up a scope name from the registered recognizers and then from the converters.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 16

def normalize_scope(object, context)
  return object if object.is_a?(Symbol)
  # 1. Process recognizers, looking for a match.
  @scope_recognizers.each_pair do |name, recognizers|
    return name if recognizers.any? { |r| lambda(&r).call(context, object) }
  end
  # 2. Process converters, finding a result.
  @scope_converters.each do |converter|
    scope = lambda(&converter).call(context, object)
    return normalize_scope(scope, converter) unless scope.nil?
  end
  # 3. Fall back to default
  return :default
end

#sanitize(attributes, context = Object.new) ⇒ Object



31
32
33
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 31

def sanitize(attributes, context = Object.new)
  sanitize_with_scope attributes, :default, context
end

#sanitize_with_scope(attributes, scope, context) ⇒ Object



35
36
37
38
# File 'lib/scoped_attr_accessible/sanitizer.rb', line 35

def sanitize_with_scope(attributes, scope, context)
  scope = normalize_scope scope, context
  attributes.reject { |k, v| deny? k, scope }
end