Module: Maromi::Helpers::SmartAuthorizerMarshal

Included in:
Authorization, Request
Defined in:
lib/maromi/helpers/smart_authorizer_marshal.rb

Overview

A little helper for storing authorizers on requests and authorizations that knows how to deal with AR and DM objects

Instance Method Summary collapse

Instance Method Details

#authorizerObject

Intelligently plucks objects stored using SAM.authorizer=(obj).

Returns:

  • (Object)

    object previously stored in the datamapper resource



24
25
26
27
28
29
30
31
32
33
# File 'lib/maromi/helpers/smart_authorizer_marshal.rb', line 24

def authorizer
  authorizer, klass = attribute_get(:authorizer), attribute_get(:authorizer_class)
  return nil if authorizer.nil?
  return authorizer if authorizer = Marshal.load(attribute_get(:authorizer)) and ( klass.nil? || klass == '' )
  if defined? ActiveRecord and (klass = eval(klass)).ancestors.include? ActiveRecord::Base
    return klass.find(authorizer)
  else
    return klass.get!(authorizer)
  end
end

#authorizer=(obj) ⇒ Object

Sets Datamapper attributes properly in the case that obj is an AR or DM object

Parameters:

  • object (Object)

    the value to attach to the datamapper resource



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/maromi/helpers/smart_authorizer_marshal.rb', line 9

def authorizer=(obj)
  if defined? ActiveRecord and obj.is_a? ActiveRecord::Base
    attribute_set(:authorizer, Marshal.dump(obj.send(obj.class.primary_key)))
    attribute_set(:authorizer_class, obj.class)
    return obj
  elsif obj.class.included_modules.include? DataMapper::Resource
    attribute_set(:authorizer, Marshal.dump(obj.key[0]))
    attribute_set(:authorizer_class, obj.class)
  else
    attribute_set(:authorizer, Marshal.dump(obj))
  end
end