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.



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

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

Instance Attribute Details

#auth_rules_readerObject (readonly)

:nodoc:



51
52
53
# File 'lib/declarative_authorization/reader.rb', line 51

def auth_rules_reader
  @auth_rules_reader
end

#privileges_readerObject (readonly)

:nodoc:



51
52
53
# File 'lib/declarative_authorization/reader.rb', line 51

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.


62
63
64
65
66
67
68
69
# File 'lib/declarative_authorization/reader.rb', line 62

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.



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/declarative_authorization/reader.rb', line 84

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.



73
74
75
76
77
78
79
80
81
# File 'lib/declarative_authorization/reader.rb', line 73

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