Class: Kindergarten::HeadGoverness

Inherits:
Object
  • Object
show all
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

EasyGoverness, StrictGoverness

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childObject (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

Returns:

  • (Boolean)


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.

Parameters:

  • method (Symbol)

    The name of the method that will be executed (for logging, record-keeping, raising, etc.)

Returns:

  • The result of the block



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

Parameters:

  • action

    Action to take

  • target

    On given target

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :message (String)

    The message on access denied

Returns:

  • The given target to allow def project(id)

    project = Project.find(id)
    guard(:view, project)
    

    end

Raises:



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

Returns:

  • (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

Examples:

Untaint

rinse(attributes, :name => /^([a-Z0-9]+)/, :description => /([\w\s-]+)/mg)

Pass on a Date attribute

rinse(attributes, :name => /([\w\s-]+)/, :date => :pass)

Bad practice

# beware of the dot-star!
rinse(attributes, :name => /(.*)/)

Parameters:

  • attributes (Hash)

    An attributes-hash to scrub

  • untaint_opts (Hash)

    Specify a Regexp for each key. The value from the attributes will be matched against the regexp and replaced with the first result.

    specify :pass instead of a Regexp to let the value pass without matching (usefull for non strings, etc.)

Returns:

  • (RinsedHash)

    a hash with only allowed keys and untainted values



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

Parameters:

  • attributes (Hash)

    An attributes-hash to scrub

  • list (Symbol)

    A list of allowed attributes

Returns:



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

Returns:

  • (Boolean)


81
82
83
# File 'lib/kindergarten/governesses/head_governess.rb', line 81

def unguarded?
  !!@unguarded
end