Class: Kindergarten::HeadGoverness
- Inherits:
-
Object
- Object
- Kindergarten::HeadGoverness
- Includes:
- CanCan::Ability
- Defined in:
- lib/kindergarten/governesses/head_governess.rb
Overview
The Governess keeps an eye on the child in the sandbox and makes sure she plays nicely and within the bounds of legality
Direct Known Subclasses
Instance Attribute Summary collapse
-
#child ⇒ Object
readonly
Returns the value of attribute child.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
The governess is empty when no rules have been defined.
-
#governed(method, &block) ⇒ Object
Perform a sandbox method within the care of the governess.
-
#guard(action, target, opts = {}) ⇒ Object
Check to see if the child can do something, increments @guard_count.
- #guarded? ⇒ Boolean
-
#initialize(child) ⇒ HeadGoverness
constructor
A new instance of HeadGoverness.
-
#rinse(attributes, untaint_opts) ⇒ RinsedHash
Scrub a hash of any key that is not specified.
-
#scrub(attributes, *list) ⇒ ScrubbedHash
Scrub a hash of any key that is not specified.
-
#unguarded(&block) ⇒ Object
When a block is given, set the Governess to unguarded during the execution of the block.
- #unguarded? ⇒ Boolean
Constructor Details
#initialize(child) ⇒ HeadGoverness
Returns a new instance of HeadGoverness.
9 10 11 12 13 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 9 def initialize(child) @child = child @unguarded = false @rules = [] end |
Instance Attribute Details
#child ⇒ Object (readonly)
Returns the value of attribute child.
7 8 9 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 7 def child @child end |
Instance Method Details
#empty? ⇒ Boolean
The governess is empty when no rules have been defined
16 17 18 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 16 def empty? @rules.empty? end |
#governed(method, &block) ⇒ Object
Perform a sandbox method within the care of the governess.
The HeadGoverness does nothing with it.
28 29 30 31 32 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 28 def governed(method, &block) raise "You must specify a block" unless block_given? return yield end |
#guard(action, target, opts = {}) ⇒ Object
Check to see if the child can do something, increments @guard_count
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 50 def guard(action, target, opts={}) if guarded? && cannot?(action, target) raise Kindergarten::AccessDenied.new(action, target, opts) end # to allow # def project(id) # project = Project.find(id) # guard(:view, project) # end # return target end |
#guarded? ⇒ Boolean
77 78 79 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 77 def guarded? !unguarded? end |
#rinse(attributes, untaint_opts) ⇒ RinsedHash
Scrub a hash of any key that is not specified
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 131 def rinse(attributes, untaint_opts) untaint_opts.symbolize_keys! scrubbed = scrub(attributes, *untaint_opts.keys) scrubbed.each do |key, value| untaint = untaint_opts[key] next if untaint == :pass match = "#{value}".match(untaint) if match.nil? scrubbed.delete key else scrubbed[key] = match[1] end end return Kindergarten::RinsedHash[scrubbed] end |
#scrub(attributes, *list) ⇒ ScrubbedHash
Scrub a hash of any key that is not specified
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 91 def scrub(attributes, *list) list.map!(&:to_sym) if attributes.respond_to?(:symbolize_keys!) attributes.symbolize_keys! else attributes = attributes.symbolize_keys end forbidden = Kindergarten::Governesses.forbidden_keys Kindergarten::ScrubbedHash[ attributes.delete_if do |key,value| forbidden.include?(key) || !list.include?(key) end ] end |
#unguarded(&block) ⇒ Object
When a block is given, set the Governess to unguarded during the execution of the block
67 68 69 70 71 72 73 74 75 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 67 def unguarded(&block) if block_given? before = @unguarded @unguarded = true yield @unguarded = before end end |
#unguarded? ⇒ Boolean
81 82 83 |
# File 'lib/kindergarten/governesses/head_governess.rb', line 81 def unguarded? !!@unguarded end |