Class: Authoreyes::Parser::DSLParser

Inherits:
Object
  • Object
show all
Defined in:
lib/authoreyes/parser/dsl_parser.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

#initializeDSLParser

Returns a new instance of DSLParser.



11
12
13
14
# File 'lib/authoreyes/parser/dsl_parser.rb', line 11

def initialize
  @privileges_reader = PrivilegesReader.new
  @auth_rules_reader = AuthorizationRulesParser.new
end

Instance Attribute Details

#auth_rules_readerObject (readonly)

:nodoc:



9
10
11
# File 'lib/authoreyes/parser/dsl_parser.rb', line 9

def auth_rules_reader
  @auth_rules_reader
end

#privileges_readerObject (readonly)

:nodoc:



9
10
11
# File 'lib/authoreyes/parser/dsl_parser.rb', line 9

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.


26
27
28
29
30
31
32
33
# File 'lib/authoreyes/parser/dsl_parser.rb', line 26

def self.factory(obj)
  case obj
  when Parser::DSLParser
    obj
  when String, Array
    load(obj)
  end
end

.load(dsl_files) ⇒ Object

Loads and parses DSL files and returns a new reader



61
62
63
64
65
66
67
68
69
# File 'lib/authoreyes/parser/dsl_parser.rb', line 61

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:



16
17
18
19
# File 'lib/authoreyes/parser/dsl_parser.rb', line 16

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.



48
49
50
# File 'lib/authoreyes/parser/dsl_parser.rb', line 48

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.



55
56
57
58
# File 'lib/authoreyes/parser/dsl_parser.rb', line 55

def load!(dsl_file)
  raise ::Authoreyes::Parser::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 an authorization DSL specification from the string given in dsl_data. Raises DSLSyntaxError if errors occur on parsing.



37
38
39
40
41
42
43
44
45
# File 'lib/authoreyes/parser/dsl_parser.rb', line 37

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