Class: Checken::Schema
- Inherits:
-
Object
- Object
- Checken::Schema
- Defined in:
- lib/checken/schema.rb
Class Attribute Summary collapse
-
.instance ⇒ Object
This can be used for storing a global instance of a schema for an application that may require such a thing.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#root_group ⇒ Object
readonly
Returns the value of attribute root_group.
Instance Method Summary collapse
-
#check_permission!(permission_path, user_proxy, object = nil, strict: true) ⇒ Object
Does the given user have the appropriate permissions to handle?.
-
#configure(&block) ⇒ Object
Add configuration for this schema.
-
#initialize ⇒ Schema
constructor
Create a new schema.
-
#load_from_directory(path) ⇒ Boolean
Load a set of schema files from a given directory.
-
#logger ⇒ Logger
Return the logger.
-
#reload ⇒ void
Reload the schema from the directory if possible.
-
#schema ⇒ Hash
Return the schema sorted alphabetically by key.
-
#update_schema(entry) ⇒ Hash
Update the schema with the given entry.
Constructor Details
#initialize ⇒ Schema
Create a new schema
19 20 21 22 23 |
# File 'lib/checken/schema.rb', line 19 def initialize @root_group = PermissionGroup.new(self, nil) @config = Config.new @schema = {} end |
Class Attribute Details
.instance ⇒ Object
This can be used for storing a global instance of a schema for an application that may require such a thing.
11 12 13 |
# File 'lib/checken/schema.rb', line 11 def instance @instance end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/checken/schema.rb', line 15 def config @config end |
#root_group ⇒ Object (readonly)
Returns the value of attribute root_group.
14 15 16 |
# File 'lib/checken/schema.rb', line 14 def root_group @root_group end |
Instance Method Details
#check_permission!(permission_path, user_proxy, object = nil, strict: true) ⇒ Object
Does the given user have the appropriate permissions to handle?
36 37 38 39 40 41 42 |
# File 'lib/checken/schema.rb', line 36 def (, user_proxy, object = nil, strict: true) if strict # permission(s) for the path are expected to be defined within the Checken Schema (, user_proxy, object) else (, user_proxy) end end |
#configure(&block) ⇒ Object
Add configuration for this schema
26 27 28 29 |
# File 'lib/checken/schema.rb', line 26 def configure(&block) block.call(@config) set_namespace if @config.namespace end |
#load_from_directory(path) ⇒ Boolean
Load a set of schema files from a given directory
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/checken/schema.rb', line 48 def load_from_directory(path) # Store the load path for future reload @load_path = path # If the path doesn't exist, just return false. We won't load anything # if the directory hasnt' been loaded yet. unless File.exist?(path) return false end # Check that the directory is a a directory unless File.directory?(path) raise Error, "Path to directory must be a directory. #{path} is not a directory." end # Read all the files and pass them through the DSL for the root schema. # Each directory is a group. Everything in the root will be at the root. Dir[File.join(path, "**", "*.rb")].each do |path| contents = File.read(path) dsl = DSL::GroupDSL.new(@root_group) dsl.instance_eval(contents, path) end logger.info "Loaded permission schema from #{path}" true end |
#logger ⇒ Logger
Return the logger
92 93 94 |
# File 'lib/checken/schema.rb', line 92 def logger @config.logger end |
#reload ⇒ void
This method returns an undefined value.
Reload the schema from the directory if possible
79 80 81 82 83 84 85 86 87 |
# File 'lib/checken/schema.rb', line 79 def reload if @load_path @root_group = PermissionGroup.new(self, nil) load_from_directory(@load_path) true else raise Error, "Cannot reload a schema that wasn't loaded from a directory" end end |
#schema ⇒ Hash
Return the schema sorted alphabetically by key
107 108 109 |
# File 'lib/checken/schema.rb', line 107 def schema @schema.sort.to_h end |
#update_schema(entry) ⇒ Hash
Update the schema with the given entry
100 101 102 |
# File 'lib/checken/schema.rb', line 100 def update_schema(entry) @schema.merge!(entry) end |