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

- (DSLReader) initialize



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

- (Object) auth_rules_reader (readonly)

:nodoc:



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

def auth_rules_reader
  @auth_rules_reader
end

- (Object) privileges_reader (readonly)

:nodoc:



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

def privileges_reader
  @privileges_reader
end

Class Method Details

+ (Object) factory(obj)

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.


71
72
73
74
75
76
77
78
# File 'lib/declarative_authorization/reader.rb', line 71

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

+ (Object) load(dsl_files)

Loads and parses DSL files and returns a new reader



105
106
107
108
109
110
111
112
113
# File 'lib/declarative_authorization/reader.rb', line 105

def self.load (dsl_files)
  # TODO cache reader in production mode?
  reader = new
  dsl_files = [dsl_files].flatten
  dsl_files.each do |file|
    reader.load(file)
  end
  reader
end

Instance Method Details

- (Object) initialize_copy(from)

:nodoc:



62
63
64
65
# File 'lib/declarative_authorization/reader.rb', line 62

def initialize_copy (from) # :nodoc:
  @privileges_reader = from.privileges_reader.clone
  @auth_rules_reader = from.auth_rules_reader.clone
end

- (Object) load(dsl_file)

Load and parse a DSL from the given file name.



93
94
95
# File 'lib/declarative_authorization/reader.rb', line 93

def load (dsl_file)
  parse(File.read(dsl_file), dsl_file) if File.exist?(dsl_file)
end

- (Object) load!(dsl_file)

Load and parse a DSL from the given file name. Raises Authorization::Reader::DSLFileNotFoundError if the file cannot be found.



99
100
101
102
# File 'lib/declarative_authorization/reader.rb', line 99

def load! (dsl_file)
  raise ::Authorization::Reader::DSLFileNotFoundError, "Error reading authorization rules file with path '#{dsl_file}'!  Please ensure it exists and that it is accessible." unless File.exist?(dsl_file)
  load(dsl_file)
end

- (Object) parse(dsl_data, file_name = nil)

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



82
83
84
85
86
87
88
89
90
# File 'lib/declarative_authorization/reader.rb', line 82

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