| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | require "db/exceptions/invalid_record" | 
					
						
							|  |  |  | require "db/mongo/models/mongo_model" | 
					
						
							|  |  |  | require "json" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  | module Devops | 
					
						
							|  |  |  |   module Model | 
					
						
							|  |  |  |     class Key < MongoModel | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       SYSTEM = "system" | 
					
						
							|  |  |  |       USER = "user" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       attr_accessor :id, :path, :scope | 
					
						
							|  |  |  |       types :id => {:type => String, :empty => false}, | 
					
						
							|  |  |  |             :path => {:type => String, :empty => false}, | 
					
						
							|  |  |  |             :scope => {:type => String, :empty => false} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        set_validators ::Validators::Key::FileExistence, | 
					
						
							|  |  |  |         ::Validators::Key::Scope | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def initialize p={} | 
					
						
							|  |  |  |         self.id = p["id"] | 
					
						
							|  |  |  |         self.path = p["path"] | 
					
						
							|  |  |  |         self.scope = p["scope"] || USER | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.build_from_bson s | 
					
						
							|  |  |  |         key = Key.new s | 
					
						
							|  |  |  |         key.id = s["_id"] | 
					
						
							|  |  |  |         key | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def self.create_from_json json | 
					
						
							|  |  |  |         Key.new( JSON.parse(json) ) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def filename | 
					
						
							|  |  |  |         File.basename(self.path) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def to_hash_without_id | 
					
						
							|  |  |  |         o = { | 
					
						
							|  |  |  |           "path" => self.path, | 
					
						
							|  |  |  |           "scope" => self.scope | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         o | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | end |