Class: Authorization::Reader::DSLReader

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_authorization/reader.rb

Overview

Top-level reader, parses the methods privileges and authorization. authorization takes a block with authorization rules as described in AuthorizationRulesReader. The block to privileges defines privilege hierarchies, as described in PrivilegesReader.

Defined Under Namespace

Classes: DSLMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSLReader

Returns a new instance of DSLReader.



57
58
59
60
# File 'lib/declarative_authorization/reader.rb', line 57

def initialize ()
  @privileges_reader = PrivilegesReader.new
  @auth_rules_reader = AuthorizationRulesReader.new
end

Instance Attribute Details

#auth_rules_readerObject (readonly)

:nodoc:



55
56
57
# File 'lib/declarative_authorization/reader.rb', line 55

def auth_rules_reader
  @auth_rules_reader
end

#privileges_readerObject (readonly)

:nodoc:



55
56
57
# File 'lib/declarative_authorization/reader.rb', line 55

def privileges_reader
  @privileges_reader
end

Class Method Details

.factory(obj) ⇒ Object

ensures you get back a DSLReader if you provide a:

DSLReader - you will get it back.
String or Array - it will treat it as if you have passed a path or an array of paths and attempt to load those.


66
67
68
69
70
71
72
73
# File 'lib/declarative_authorization/reader.rb', line 66

def self.factory(obj)
  case obj
  when Reader::DSLReader
    obj
  when String, Array
    load(obj)
  end
end

.load(dsl_files) ⇒ Object

Loads and parses a DSL from the given file name.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/declarative_authorization/reader.rb', line 88

def self.load (dsl_files)
  # TODO cache reader in production mode?
  reader = new
  dsl_files = [dsl_files].flatten
  dsl_files.each do |file|
    begin
      reader.parse(File.read(file), file)
    rescue SystemCallError
      raise ::Authorization::Reader::DSLFileNotFoundError, "Error reading authorization rules file with path '#{file}'!  Please ensure it exists and that it is accessible."
    end
  end
  reader
end

Instance Method Details

#parse(dsl_data, file_name = nil) ⇒ Object

Parses a authorization DSL specification from the string given in dsl_data. Raises DSLSyntaxError if errors occur on parsing.



77
78
79
80
81
82
83
84
85
# File 'lib/declarative_authorization/reader.rb', line 77

def parse (dsl_data, file_name = nil)
  if file_name
    DSLMethods.new(self).instance_eval(dsl_data, file_name)
  else
    DSLMethods.new(self).instance_eval(dsl_data)
  end
rescue SyntaxError, NoMethodError, NameError => e
  raise DSLSyntaxError, "Illegal DSL syntax: #{e}"
end