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.



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

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

Instance Attribute Details

#auth_rules_readerObject (readonly)

:nodoc:



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

def auth_rules_reader
  @auth_rules_reader
end

#privileges_readerObject (readonly)

:nodoc:



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

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.


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

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

.load(dsl_files) ⇒ Object

Loads and parses DSL files and returns a new reader



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

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

#initialize_copy(from) ⇒ Object

:nodoc:



60
61
62
63
# File 'lib/declarative_authorization/reader.rb', line 60

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

#load(dsl_file) ⇒ Object

Load and parse a DSL from the given file name.



91
92
93
# File 'lib/declarative_authorization/reader.rb', line 91

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

#load!(dsl_file) ⇒ Object

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



97
98
99
100
# File 'lib/declarative_authorization/reader.rb', line 97

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

#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.



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

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