Class: RsUserPolicy::UserAssignments::JsonUserAssignments
- Inherits:
-
Object
- Object
- RsUserPolicy::UserAssignments::JsonUserAssignments
- Includes:
- UserAssignments
- Defined in:
- lib/rs_user_policy/user_assignments/json_user_assignments.rb
Instance Method Summary collapse
-
#[](email) ⇒ Hash
Returns a hash which represents the user specified by the email address specified If the user does not exist the (see #add_user) method will be called and the user will be created.
-
#add_user(email, options = {}) ⇒ Hash
Adds a user to user_assignments.
-
#delete(email) ⇒ Object
Deletes a user from the user assignments.
-
#get_roles(email) ⇒ Array<String>
Returns the roles assigned to the user.
-
#initialize(options = {}) ⇒ JsonUserAssignments
constructor
Initializes a new UserAssignments.
-
#length ⇒ Int
The number of users in the user assignments object.
-
#list ⇒ Array<String>
Returns a list of all user emails which have a user assignment in the source.
-
#serialize(options = {}) ⇒ Object
Commits any changes made to the UserAssignments object back to it’s original data store.
-
#size ⇒ Int
The number of users in the user assignments object.
Constructor Details
#initialize(options = {}) ⇒ JsonUserAssignments
Initializes a new UserAssignments
If more than one source is passed into options, the order of preference will be
- :json, :json_str, :filename
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 39 def initialize(={}) begin if .has_key?(:json) @user_assignments = [:json] elsif .has_key?(:json_str) @user_assignments = JSON.parse([:json_str]) elsif .has_key?(:filename) @user_assignments = JSON.parse(File.read([:filename])) else @user_assignments = {} end rescue Errno::ENOENT, JSON::ParserError @user_assignments = {} end validate() end |
Instance Method Details
#[](email) ⇒ Hash
Returns a hash which represents the user specified by the email address specified If the user does not exist the (see #add_user) method will be called and the user will be created.
113 114 115 |
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 113 def [](email) add_user(email) end |
#add_user(email, options = {}) ⇒ Hash
Adds a user to user_assignments. If the user already exists the existing record will be returned. Otherwise the user will be created with a single role of “immutable”
125 126 127 128 |
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 125 def add_user(email, ={}) = {"roles" => ["immutable"]}.merge() @user_assignments[email] || @user_assignments[email] = end |
#delete(email) ⇒ Object
Deletes a user from the user assignments
82 83 84 |
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 82 def delete(email) @user_assignments.delete(email) end |
#get_roles(email) ⇒ Array<String>
Returns the roles assigned to the user. If the user does not exist they should be automatically created with the role “immutable”
73 74 75 76 77 |
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 73 def get_roles(email) # TODO: This seems expensive to do in an accessor? add_user(email) @user_assignments[email]['roles'] end |
#length ⇒ Int
Returns The number of users in the user assignments object.
58 59 60 |
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 58 def length @user_assignments.length end |
#list ⇒ Array<String>
Returns a list of all user emails which have a user assignment in the source
102 103 104 |
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 102 def list @user_assignments.keys end |
#serialize(options = {}) ⇒ Object
Commits any changes made to the UserAssignments object back to it’s original data store. This is an opportunity to perform DB flushes or write back to a source file.
94 95 96 97 |
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 94 def serialize(={}) raise ArgumentError, "You must specify the :filename option" unless .has_key?(:filename) File.open([:filename], 'w') {|f| f.write(JSON.pretty_generate(@user_assignments))} end |
#size ⇒ Int
Returns The number of users in the user assignments object.
63 64 65 |
# File 'lib/rs_user_policy/user_assignments/json_user_assignments.rb', line 63 def size self.length end |